Play Album in Apple Music

Hello, I am new to Keyboard Maestro and I am strugging with how I go about creating a macro to play a specific album in my library in Apple Music?

Using AppleScript I added . . .

tell application "Music"

play (every track of playlist "All Files" whose album is "Never Let Me Go")

end tell

. . . but it is only playing the first song off the album.

Thanks in advance!

Listed code becomes more legible if you "fence" it between:

  • Three backtick characters above,
  • and three backtick characters below
```
code here ...
```
tell application "Music"

    play (every track of playlist "All Files" whose album is "Never Let Me Go")

end tell

You can look up the play command in Script Editor under:

File > Open Dictionary > Music.app

Where you will see that it's defined only for application to a single track
(not for sequential application over a list, or over a compound reference to a set of tracks)

An easier approach might be to make a Music.app "playlist" of all the tracks which are to be heard in sequence, and then use the Keyboard Maestro Play Playlist action, applying it to the (exactly spelled) name of your playlist.

1 Like

Thanks very much for your time/reply. I was hoping to avoid creating a playlist but it does seem like the easy way to go. Much appreciated.

Creation of a new named playlist for all tracks with a given album string could be automated in AppleScript.

(As could its deletion, if you wanted the playlist to be temporary)


Perhaps roughly this kind of thing ?

Expand disclosure triangle to view AppleScript Source
tell application "Music"
    set albumName to "Never Let You Go"
    set playListName to "temporary"
    
    set refTracks to (a reference to (tracks where album = albumName))
    
    set nTracks to count of refTracks
    if 0 < nTracks then
        if exists (playlist playListName) then
            set refPlayList to playlist playListName
        else
            set refPlayList to (make new playlist with properties {name:playListName})
        end if
        
        duplicate refTracks to end of refPlayList
        ("Added " & nTracks as text) & " tracks to playlist: " & playListName
    else
        "No tracks found with album name: " & albumName
    end if
end tell
1 Like

Thanks very much. I will give it a go!

This is a good start

tell application "Music"

    play (every track of playlist "All Files" whose album is "Never Let Me Go")

end tell

But is there a way to shuffle the playlist?

Not a proper shuffle, but you could play random tracks -- this version is "sort of random" in that it won't play the same track twice in a row:

tell application "Music"
	set theTracks to every track whose album is "Back In Black"
	set thisTrack to 0
	repeat 10 times
		set lastTrack to thisTrack
		repeat until thisTrack ≠ lastTrack
			set thisTrack to (get random number from 1 to (length of theTracks))
		end repeat
		play item thisTrack of theTracks with once
		repeat while player state is playing
			delay 1
		end repeat
	end repeat
end tell

Plays 10 random tracks from an album named in the 2nd line -- change the name and the repeat 10 times to whatever suits. Not fully tested because my test machine is 10.14 and iTunes doesn't honour the with once option...

Maybe:

tell application "Music"
	set song repeat to off
	set shuffle enabled to true
	play (every track of playlist "All Files" whose album is "Never Let Me Go")
end tell

Does that work? The iTunes dictionary I looked at had it marked as "obsolete" so I didn't suggest it for the more recent Music (I know... I should know better than to trust the docs without trying for myself!).

1 Like

I did one test, and it worked Music version 1.3.0.138.

YMMV. Try it out.

1 Like

Ah -- I was looking at the playlist property, not realising shuffle is also a global app property. Nice!

1 Like

I tried that on 1.2.5.7 to no effect.

To get it to play anything, I had to change the script to

tell application "Music"
   set song repeat to off
   set shuffle enabled to true
   play (every track whose album is  "The Best of Mozart")
end tell

It plays a single but random track from that album, and proceeds to play a track from another album afterwards.

The format

play (every track of playlist "All Files" whose album is "The Best of Mozart")

doesn't play anything.

That's the problem I've always had with my older versions of iTunes/Music. But the script above works with every version I've tried -- does it not work for you?

YMMV.

Rather than beat a dead digital horse, I will recommend an alternative solution that works in my tests.

Using a playlist should work around whatever issue is causing issues on your end.

Make a playlist of your Album. In my sample script, my playlist is named: Tool—10,000 Days.

The tested script:

tell application "Music"
	stop
	set song repeat to off
	set shuffle enabled to true
	play playlist "Tool—10,000 Days"
end tell

I hope that helps!

If you don't want to make a playlist yourself, then ComplexPoint's script should work (you will have to set the album name in the script):

tell application "Music"
	stop
	set song repeat to off
	set shuffle enabled to true
	set albumName to "The Downward Spiral"
	set playListName to "temporary"
	
	delete tracks of playlist playListName
	
	set refTracks to (a reference to (tracks where album = albumName))
	
	set nTracks to count of refTracks
	if 0 < nTracks then
		if exists (playlist playListName) then
			set refPlayList to playlist playListName
		else
			set refPlayList to (make new playlist with properties {name:playListName})
		end if
		
		duplicate refTracks to end of refPlayList
		("Added " & nTracks as text) & " tracks to playlist: " & playListName
	else
		"No tracks found with album name: " & albumName
	end if
	play playlist playListName
end tell