How to tell iTunes to start playing?

Discussion in 'Mac Programming' started by yalag, Jun 8, 2016.

  1. yalag macrumors 65816

    Joined:
    Nov 18, 2007
    #1
    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?
     
  2. LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
    #2
    Maybe you could provide your code so that we can have a look at what you've done so far.
     
  3. LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
    #4
    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
     
  4. jamesaberry macrumors newbie

    Joined:
    Jul 17, 2007
    Location:
    UK
    #5
  5. tkermit, Jun 11, 2016
    Last edited: Jun 11, 2016

    tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #6
    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
    
     
  6. Superhai macrumors 6502

    Superhai

    Joined:
    Apr 21, 2010
    #7
    Try this instead in an Applescript:
    Code:
    open location "itmss://itunes.apple.com/ca/station/smooth-jazz/idra.985496511?cmd=AddStation"
     
  7. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #8

    Awesome thanks, this did it! It's the combination of using itmss and also cmd=AddStation
     
  8. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #9
    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?
     
  9. Superhai macrumors 6502

    Superhai

    Joined:
    Apr 21, 2010
    #10
    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.
     
  10. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #11
    I've already tried both of those. It doesn't work. Apple Music playlist behaves completely different.
     
  11. tkermit, Jun 27, 2016
    Last edited: Jun 27, 2016

    tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #12
    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
     
  12. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #13
    You are a genius. Not trying to be too demanding here...but is there a way to play that playlist in shuffle?
     
  13. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #14
    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
     
  14. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #15
    I tried that, but I think shuffle is not longer settable in Itunes 12.
     
  15. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #16
    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.
     
  16. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #17
    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."
     
  17. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #18
    Did you make sure to include "use application "iTunes" on the first line" ?
     
  18. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #19
    Got it thanks!!!
     
  19. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
  20. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #21
    Help! Looks like this script is broken in Sierra. Any ideas why? It seems like it's stuck in the loop.
     
  21. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
  22. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #23
    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.
     
  23. yalag thread starter macrumors 65816

    Joined:
    Nov 18, 2007
    #24
    But these playslists are added to my library, yet the script still doesn't work. Any ideas?
     
  24. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #25
    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"
     

Share This Page