Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iTunes - Applescript Help

I'm looking for some assistance in finding Applescript code that will allow the "cycling of playlists that I created", excluding any defaults such as Library, Music, Movies, TV Shows...


Be warned I have never written an Applescript before, so your replies should be at beginner level, don't assume anything.


In my mind I would want something like these:



tell application "iTunes"

if current playlist is "Jazz" then

set current playlist to "Rock"

end if

if current playlist is "Rock" then

set current playlist to "Country"

end if

if current playlist is "Country" then

set current playlist to "Jazz"

end if

end tell


------------------------------------------------------------


tell application "iTunes"

if current playlist is "Jazz" then

play playlist "Rock"

end if

if current playlist is "Rock" then

play playlist "Country"

end if

if current playlist is "Country" then

play playlist "Jazz"

end if

end tell


The error for the above is "can't get current playlist"




tell application "iTunes"

if playlist is "Jazz" then

play playlist "Rock"

end if

if playlist is "Rock" then

play playlist "Country"

end if

if playlist is "Country" then

play playlist "Jazz"

end if

end tell


This version has no error, but also doesn't do anything


iTunes is smart enough to play an exact playlist:


tell application "iTunes"


playplaylist "Jazz"

end tell


So how do I get it to use if/then logic to select the appropriate playlist?


Many thanks in advance.


OS 10.8.4

iTunes 11.0.5

OS X Mountain Lion (10.8.4)

Posted on Aug 30, 2013 12:54 PM

Reply
4 replies

Aug 30, 2013 2:13 PM in response to SysGeek

Try something like this:


tell application "iTunes"

if (name of view of window "iTunes" is "Jazz") then

reveal (playlist 1 whose name is "Rock") -- to select the playlist

end if

end tell


or


tell application "iTunes"

if (name of view of window "iTunes" is "Jazz") then

play (playlist 1 whose name is "Rock") -- to play the playlist

end if

end tell


Message was edited by: Pierre L.

Aug 31, 2013 3:34 AM in response to SysGeek

Hello


You may try the following script which will rotate the given playlists. It will be terminated by error when specified playlist does not exist. Note that playlist name is treated in case-insensitive manner in this script and so you'd get into trouble if you have "Jazz" and "jazz" for instance. This case-related issue can be resolved by extra coding translating case-sensitive name to id, if necessary.


Regards,

H


set playlist_names to {"Jazz", "Rock", "Country"}
rotate_playlists(playlist_names)

on rotate_playlists(pp)
    (*
        list pp : list of name of playlists to rotate
    *)
    tell application "iTunes"
        set p to browser window 1's view's name
        set k to my bsearch(pp, p) -- current index
        set k1 to k mod (count pp) + 1 -- next index
        
        tell playlist (pp's item k1) -- playlist name is case insensitive
            reveal
            play
        end tell
    end tell
end rotate_playlists

on bsearch(xx, x)
    (*
        list xx : source list
        anything x : item to be searched in xx
        return integer : the first index of x in xx if {x} is in xx, or 0 if not.
    *)
    script o
        property aa : xx
        local i, j, k
        if {x} is not in my aa then return 0
        set i to 1
        set j to count my aa
        repeat while j > i
            set k to (i + j) div 2
            if {x} is in my aa's items i thru k then
                set j to k
            else
                set i to k + 1
            end if
        end repeat
        return i
    end script
    tell o to run
end bsearch

Sep 1, 2013 3:49 AM in response to SysGeek

Hello


Glad to know it helped.

In case, here's a revised, safer version which resolved the said case-related issue.


Cheers,

H


(*
    rotate playlists in iTunes
    v0.2
    
        * playlist name is treated in case-sensitive manner
        * non-exsistent playlist name in given list is ignored

*)
set playlist_names to {"Jazz", "Rock", "Country"}
rotate_playlists(playlist_names)

on rotate_playlists(aa)
    (*
        list aa : list of name of playlists to rotate
    *)
    script o
        property pp : aa
        property qq : {}
        property nn : {}
        property ii : {}
        
        -- get list of name and id of playlists
        tell application "iTunes"
            tell playlists
                set {nn, ii} to {name, id}
            end tell
            set q to browser window 1's view's id
        end tell
        
        -- translate name list (pp) to id list (qq)
        repeat with p in my pp
            set k to my bsearch(nn, p's contents)
            if k > 0 then -- ignore non-existent playlist in pp
                set end of my qq to my ii's item k
            end if
        end repeat
        
        -- rotate playlist based on id list
        if (count my qq) = 0 then error "None of the specified playlists exist." number 8000
        tell application "iTunes"
            set k to my bsearch(my qq, q) -- current index (may be 0)
            set k1 to k mod (count my qq) + 1 -- next index
            tell playlist id (my qq's item k1)
                reveal
                play
            end tell
        end tell
    end script
    considering case -- for case-sensitive search
        tell o to run
    end considering
end rotate_playlists

on bsearch(xx, x)
    (*
        list xx : source list
        anything x : item to be searched in xx
        return integer : the first index of x in xx if {x} is in xx, or 0 if not.
    *)
    script o
        property aa : xx
        local i, j, k
        if {x} is not in my aa then return 0
        set i to 1
        set j to count my aa
        repeat while j > i
            set k to (i + j) div 2
            if {x} is in my aa's items i thru k then
                set j to k
            else
                set i to k + 1
            end if
        end repeat
        return i
    end script
    tell o to run
end bsearch

iTunes - Applescript Help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.