"Miles, Monk, Mingus, Trane" (Choose random album from artists in macOS)

Wondering if more skillful automators than I can help me with a Music automation…

So, I “stole” an iOS shortcut from someone here that allows me to select all my music by particular artists, pick a random track from that list, then select the whole album that track comes from and play the album. But I have no idea how to do this in macOS!

Is there a way to tell iTunes / Music on macOS to pick a random album from within a set of criteria (in my case, I want an album by either Miles Davis, Theolonious Monk, Charles Mingus, or John Coltrane) and then play that album in album order?

Thanks!

Cheers,
Eric

Here’s a very basic version that can get you started with the necessary AppleScript:

tell application "iTunes"
	set theTracks to (every track whose album artist is "John Coltrane") & (every track whose album artist is "Miles Davis")
	set t to random number from 1 to length of theTracks
	set theAlbum to album of item t of theTracks
	play (every track of playlist "Library" whose album is theAlbum)
end tell

Note that as written the randomization is by track so it will be unevenly weighted depending on the # of tracks by album.

As a follow-up, make sure that you listen to episode 44, and visit dougscripts :wink:

You, fine person, are fantastic! THANK YOU! I will play with this and see what happens.

Not to be too gushy or anything, but this sort of thing is what I still love about “the interwebs,” despite all the grossness out there.

-Eric

1 Like

Dfay,

So I tried this as written, and it will only play one track, not an album, and it is usually choosing the last track of the album. If I try it too many times, I get this: error “iTunes got an error: An error of type -208 has occurred.” number -208

(I’m running 10.14.6 Mojave if that makes a difference.)

I also tried to figure out how to filter theTracks so that it only included tracks with a track number of 1, to eliminate the weighting by length of album, but I was unable to figure that out.

If you’ve got the time and inclination, any guidance would be appreciated. If not, no worries. Thanks for the start!

-Eric

I tinkered some, and came up with an alternate solution more at my level of technical competence. I created a short AppleScript:

tell application "iTunes"
	
	set shuffle mode to songs
	set shuffle enabled to yes

end tell

Then I created a KM macro to run this script and then to play a smart playlist I created in iTunes. The smart playlist is simply set to match “any of the following…” for the artist names I want. It doesn’t only play one album, but it plays a random album, which is close enough.

-Eric

Cool. I was thinking later that a playlist based solution would probably work better than hard-coding artist names.

Most pseudorandom number generators do have a tendency to introduce bias at the boundary values.

AppleScript has a seemingly-fair mechanism of choosing an item at random from a list with each item having equal probability of being chosen:

set theAlbum to album of some item of theTracks

It’s also very efficient. You can even do this:

set hat to {"John Coltrane", "Miles Davis", "Really Rich Italian Satanists", "Beethoven"}

tell application "iTunes"
        set pulledOutOfAHat to some item in the hat
	    set theAlbum to the album of some track whose album artist is pulledOutOfAHat
	    add (every track of playlist "Library" whose album is theAlbum) to ... (*a playlist*)
        (* etc. *)
end tell
1 Like

Cool. Never seen some before. I wouldn’t use AS random #s for pulling a research sample but it seemed fine for this application.

Sorry, I totally misread an earlier statement where you wrote:

Note that as written the randomization is by track so it will be unevenly weighted depending on the # of tracks by album.

Now I’ve re-read what the line properly, I see the issue of pseudorandom number bias is not terribly pertinent. I must have picked out the phrase “unevenly weighted” during a skim-read, and filled in the blanks with irrelevance.

Soz.