Q: 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"
play playlist "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
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
Posted on Aug 31, 2013 3:34 AM