Saez Posted December 17, 2021 Share Posted December 17, 2021 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
deanishe Posted December 17, 2021 Share Posted December 17, 2021 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
vitor Posted December 17, 2021 Share Posted December 17, 2021 12 minutes ago, deanishe said: You should always use an API where possible, and Music.app has one. @Saez To find those, you open a new document in Script Editor then File → Open Dictionary… → chose the app in question. Link to comment
Saez Posted December 17, 2021 Author Share Posted December 17, 2021 @vitor @deanishe I though in something like copy Args and AppleScript get "args" (it means the link of playlist) to play. There is a way for that? I never worked with API and don't know how to start (yet). Link to comment
deanishe Posted December 17, 2021 Share Posted December 17, 2021 20 minutes ago, Saez said: copy Args and AppleScript get "args" (it means the link of playlist) to play. There is a way for that? I'm sorry, I have no idea what that's supposed to mean. Link to comment
vitor Posted December 17, 2021 Share Posted December 17, 2021 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
Saez Posted December 17, 2021 Author Share Posted December 17, 2021 Basically the way is like this: Args, means: 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
vitor Posted December 17, 2021 Share Posted December 17, 2021 1 minute ago, Saez said: I thought if there is a way to copy this Arg and make AppleScript read it (get this arg). That’s the code I posted above. Link to comment
Saez Posted December 17, 2021 Author Share Posted December 17, 2021 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) Link to comment
vitor Posted December 17, 2021 Share Posted December 17, 2021 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. Saez 1 Link to comment
deanishe Posted December 18, 2021 Share Posted December 18, 2021 If you want to open a playlist URL use open location, i.e. tell application "Music" to open location (item 1 of argv) Link to comment
Saez Posted December 18, 2021 Author Share Posted December 18, 2021 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 Link to comment
Saez Posted December 18, 2021 Author Share Posted December 18, 2021 When Clicked in "go to song" to see what's playing: Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now