Jump to content

Help to create a AppleScript that works to Play a playlist automatically


Saez

Recommended Posts

I'm working on this Alfred Workflow: https://github.com/gustavosaez/Alfred-Workflow-Apple-Music-Playlist

And today I'm looking for a day to "automate" the play music in background or hidden (to avoid open the application and click PLAY).

I found a way to set the mouse click on a screen position (specific on the play button), but it works only if the app is visible and if the resolution of screen is the same of mine.

I search about UI Elements for Music.app and didn't find anything.

The problem is:

  • When Alfred opens the chosen link, Music.app take a few seconds to open and load the playlist selected;
  • After this, space/play command is inactive, so I think to create a script that gets the current page and play the playlist in shuffle mode.

Any idea?

some ideas:

tell application "Music"
    -- tell application "System Events" to tell process "Music" to click at {620, 374}
    play current playlist
end tell

 

tell application "Music"
    -- tell application "System Events" to tell process "Music" to click at {620, 374}
    play {workflowPlaylistName}
end tell

tell application "System Events"
    keystroke "h" using command down
end tell

 

Link to comment
4 minutes ago, Saez said:

I found a way to set the mouse click on a screen position (specific on the play button), but it works only if the app is visible and if the resolution of screen is the same of mine.

I search about UI Elements for Music.app and didn't find anything.

 

This is the wrong way to go about it. Simulating user input (mouse clicks, keypresses) is a last resort because it's so unreliable.

 

You should always use an API where possible, and Music.app has one.

 

The AppleScript command to play a playlist is tell application "Music" to play playlist "name of playlist"

 

Link to comment
20 minutes ago, Saez said:

I never worked with API

 

Forget “API”, then. The acronym may be more confusing than helpful until you’ve done some coding and intuitively realise there’s nothing to it.


What you probably want to do right now is have your List Filter send the playlist name as the argument, then have a Run Script Action with language set to /usr/bin/osascript (AppleScript) and code (untested):


on run argv
  tell application "Music" to play playlist (item 1 of argv)
end run

Link to comment

Basically the way is like this:
952862590_AlfredPreferencesAlfredPreferences17_12.202112h10m08s.thumb.png.537bb96e54337a0c9e6010852a3c7de2.png

 

Args, means:
24397200_CleanShotCleanShot17_12.202112h10m40s@2x.thumb.png.a762c17cd6255da822b5187ed26162b7.png

 

I thought if there is a way to copy this Arg and make AppleScript read it (get this arg).

The way is:
1. Type "amusic"
2. Select what you want to listen
3. Music.app is opened in the page. (stops here and the user needs to click on Play to listen, except for radio stations).


I want to update to next steps:
4. Music app is hidden (I found how to do this)
5. Music starts to play without action of user (I'm in trouble of this)


After that, I think the way is "automated" to do everything in Alfred.

Link to comment
40 minutes ago, vitor said:

 

That’s the code I posted above.

Unfortunately it's not working.

 

[12:36:51.341] Apple Music - ALPHA[List Filter] Processing complete
[12:36:51.341] Apple Music - ALPHA[List Filter] Passing output 'https://music.apple.com/us/playlist/feeling-confident/pl.76e1b75c52404883bc58972d4ebb2c6f' to Open URL
[12:36:51.359] Apple Music - ALPHA[Open URL] Processing complete
[12:36:51.360] Apple Music - ALPHA[Open URL] Passing output 'https://music.apple.com/us/playlist/feeling-confident/pl.76e1b75c52404883bc58972d4ebb2c6f' to Run Script
[12:36:51.559] ERROR: Apple Music - ALPHA[Run Script] /Users/gustavosaez/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/8F6FE6A4-B99D-4DA4-8CAD-45D17ACC7135:42:72: execution error: Music got an error: Can’t make some data into the expected type. (-1700)

 

 

567095039_AlfredPreferencesAlfredPreferences17_12.202113h09m05s.thumb.png.0df98921627379e2116c39ada0b4087f.png

Link to comment
5 minutes ago, Saez said:

Unfortunately it's not working.

 

Well, it is doing what you asked but I don’t know if what you asked for is correct. I adapted Dean’s code above which is expecting a name, not a URL. Does Music support playing a playlist from a URL? I don’t know; I never touched Apple Music (neither the service nor the app).


So you need to figure out what input Music needs to play the playlist, which you can do by reading the dictionary or searching online. My code showed you how to pass the argument to your code but you need to figure out what the correct argument is.

Link to comment
2 hours ago, deanishe said:

If you want to open a playlist URL use open location, i.e. tell application "Music" to open location (item 1 of argv)

 

Hmmm that's works partially. Music.app recognize the command, open the playlist, connects to the destination but not starts play:
the link I'm testing: https://music.apple.com/us/playlist/feeling-confident/pl.76e1b75c52404883bc58972d4ebb2c6f


76087906_MusicMusic18_12.202110h01m38s.thumb.png.0247863bc7655ef0be54a92a015d7d7d.png

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...