Mac How to tell iTunes to start playing?

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
I want to write a script (shell or apple script) to tell iTunes to start playing music. Specifically a apple music radio station. I found out that you can get a url of the radio station something like this https://itunes.apple.com/ca/station/smooth-jazz/idra.985496511

But the problem is if I use apple script to open that url, it will play the music but it will also open a safari tab. Is there a way to script the radio playback without launching safari?
 

LinusR

macrumors 6502
Jan 3, 2011
286
293
Maybe you could provide your code so that we can have a look at what you've done so far.
 

LinusR

macrumors 6502
Jan 3, 2011
286
293
The short answer to your question, iirc, is no. The link redirects from Safari to iTunes, i.e. telling Safari to quit before redirecting you would result in not hearing any music. If you try something like this though:

Code:
tell application "Safari"
    activate
    tell window 1
        make new tab with properties {URL:"https://itunes.apple.com/ca/station/smooth-jazz/idra.985496511"}
    end tell
    delay 5
    quit
end tell
EDIT: Typo
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
I'm sure this is 'dirty' in some way and there's a more straightforward way to handle this, but if you're on a recent version of OS X, AppleScriptObjC and WebKit would do the job for you:

Code:
use framework "WebKit"
property urlString : "https://itunes.apple.com/ca/station/smooth-jazz/idra.985496511"
if (current application's NSThread's isMainThread) then
    webView()
else
    its performSelectorOnMainThread:"webView" withObject:(missing value) waitUntilDone:true
end if

on webView()
    tell (current application's |WebView|'s alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 0, 0)) frameName:"WebKit Frame" groupName:"WebKit Group")
        its (mainFrame's loadRequest:(current application's NSURLRequest's requestWithURL:(current application's NSURL's URLWithString:urlString)))
        delay 5
        its |close|()
    end tell
end webView
 
Last edited:
  • Like
Reactions: LinusR

Superhai

macrumors 6502
Apr 21, 2010
457
360
Try this instead in an Applescript:
Code:
open location "itmss://itunes.apple.com/ca/station/smooth-jazz/idra.985496511?cmd=AddStation"
 
  • Like
Reactions: LinusR and tkermit

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
Try this instead in an Applescript:
Code:
open location "itmss://itunes.apple.com/ca/station/smooth-jazz/idra.985496511?cmd=AddStation"

Awesome thanks, this did it! It's the combination of using itmss and also cmd=AddStation
 

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
Try this instead in an Applescript:
Code:
open location "itmss://itunes.apple.com/ca/station/smooth-jazz/idra.985496511?cmd=AddStation"
Is there something similar for Apple Music playlists?

I tried open location "itmss://itunes.apple.com/ca/playlist/todays-hits/idpl.f4d106fed2bd41149aaacabb233eb5eb"

it will open the playlist but it won't play it. Is there a script that will play the playlist from Apple Music?
 

Superhai

macrumors 6502
Apr 21, 2010
457
360
Is there a script that will play the playlist from Apple Music?
I would imagine so. I don't have Apple Music so I can't test it, but if you can open it, then adding
Code:
tell app "iTunes" to play
After the open location may do it. Or maybe
Code:
tell app "iTunes" play the playlist named "Todays Hits"
If it doesn't work I can look it up.
 

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
I would imagine so. I don't have Apple Music so I can't test it, but if you can open it, then adding
Code:
tell app "iTunes" to play
After the open location may do it. Or maybe
Code:
tell app "iTunes" play the playlist named "Todays Hits"
If it doesn't work I can look it up.
I've already tried both of those. It doesn't work. Apple Music playlist behaves completely different.
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
I've already tried both of those. It doesn't work. Apple Music playlist behaves completely different.
The following works for me.

After executing the "open location" command, this will continually (once every second, for a maximum of 30 seconds) ask iTunes to resolve the reference to the playlist and attempt to play it .

Code:
use application "iTunes"

property playlistURL : "itmss://itunes.apple.com/ca/playlist/todays-hits/idpl.f4d106fed2bd41149aaacabb233eb5eb"
property playlistName : "Today's Hits"

tell application "iTunes" to stop

set playlistToPlay to a reference to subscription playlist playlistName of source "iTunes Store"
open location playlistURL

set i to 0
repeat while i < 30
    delay 1
    try
        tell playlistToPlay to if it exists then
            play it
            if player state is playing then return
        end if
    end try
    set i to i + 1
end repeat
 
Last edited:

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
The following works for me.

After executing the "open location" command, this will continually (once every second, for a maximum of 30 seconds) ask iTunes to resolve the reference to the playlist and attempt to play it .

Code:
use application "iTunes"

property playlistURL : "itmss://itunes.apple.com/ca/playlist/todays-hits/idpl.f4d106fed2bd41149aaacabb233eb5eb"
property playlistName : "Today's Hits"

set playlistToPlay to a reference to subscription playlist playlistName of source "iTunes Store"
open location playlistURL

set i to 0
repeat while i < 30
    delay 1
    try
        tell playlistToPlay to if it exists then
            play it
            if player state is playing then return
        end if
    end try
    set i to i + 1
end repeat
You are a genius. Not trying to be too demanding here...but is there a way to play that playlist in shuffle?
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
Sure thing. Just add the following to the script, after the property declarations. You may also want to make sure iTunes is stopped for the script to work correctly in all cases.
Code:
tell application "iTunes" to stop
set shuffle enabled to true
 

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
Sure thing. Just add the following to the script, after the property declarations. You may also want to make sure iTunes is stopped for the script to work correctly in all cases.
Code:
tell application "iTunes" to stop
set shuffle enabled to true
I tried that, but I think shuffle is not longer settable in Itunes 12.
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
I'm using iTunes 12.4.1.6 and it certainly works here. The only thing that should have changed (I think) is that shuffling is set as a global property of the application now, and not on individual playlists anymore.
 

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
I'm using iTunes 12.4.1.6 and it certainly works here. The only thing that should have changed (I think) is that shuffling is set as a global property of the application now, and not on individual playlists anymore.
Can you post the exact script? I pasted those 2 lines into the script editor and I got

shuffle enabled
"A identifier can’t go after this identifier."
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
Did you make sure to include "use application "iTunes" on the first line" ?
 

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
The following works for me.

After executing the "open location" command, this will continually (once every second, for a maximum of 30 seconds) ask iTunes to resolve the reference to the playlist and attempt to play it .

Code:
use application "iTunes"

property playlistURL : "itmss://itunes.apple.com/ca/playlist/todays-hits/idpl.f4d106fed2bd41149aaacabb233eb5eb"
property playlistName : "Today's Hits"

tell application "iTunes" to stop

set playlistToPlay to a reference to subscription playlist playlistName of source "iTunes Store"
open location playlistURL

set i to 0
repeat while i < 30
    delay 1
    try
        tell playlistToPlay to if it exists then
            play it
            if player state is playing then return
        end if
    end try
    set i to i + 1
end repeat
Help! Looks like this script is broken in Sierra. Any ideas why? It seems like it's stuck in the loop.
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
Hi - sorry, this is due to a change in the latest versions of iTunes that appears to make it even more difficult if not impossible to use AppleScript to refer to subscription playlists that haven't been added to your library. After using 'open location' to open the playlist, it now doesn't ever turn up as a child element of the "iTunes Store" source anymore as it did before, so the condition in the loop that checks for its existence will never become true.
 

yalag

macrumors 65816
Original poster
Nov 18, 2007
1,350
56
Hi - sorry, this is due to a change in the latest versions of iTunes that appears to make it even more difficult if not impossible to use AppleScript to refer to subscription playlists that haven't been added to your library. After using 'open location' to open the playlist, it now doesn't ever turn up as a child element of the "iTunes Store" source anymore as it did before, so the condition in the loop that checks for its existence will never become true.
But these playslists are added to my library, yet the script still doesn't work. Any ideas?
 

tkermit

macrumors 68040
Feb 20, 2004
3,450
2,490
If you've added them to your library then you don't need the script in the first place. In that case it is trivial to play them:
Code:
tell application "iTunes" to play playlist "Name of your playlist"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.