Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/10139397 to your computer and use it in GitHub Desktop.
Save iloveitaly/10139397 to your computer and use it in GitHub Desktop.
Select iTunes DJ Playlist. For older versions (pre iTunes 11). Useful Applescript UI scripting debugging
on select_dj_playlist(playlist_name)
try
with timeout of 10 seconds
tell application "Finder"
set os_version to version
end tell
set errorString to ""
tell application "System Events"
tell application process "iTunes"
set visible to true
if version of application "iTunes" contains "10.0" or version of application "iTunes" contains "10.1" or version of application "iTunes" contains "10.2" or version of application "iTunes" contains "10.3" or version of application "iTunes" contains "10.4" or version of application "iTunes" contains "10.5" or version of application "iTunes" contains "10.6" or version of application "iTunes" contains "10.7" then
set playlist_popup to pop up button 1 of window "iTunes"
else if version of application "iTunes" contains "9.1" or version of application "iTunes" contains "9.0" or version of application "iTunes" contains "9.2" then
set playlist_popup to pop up button 1 of splitter group 1 of window "iTunes"
else
set playlist_popup to UI element 3 of UI element 3 of splitter group 1 of window "iTunes"
end if
set switchingSuccess to false
repeat 3 times
try
-- the way carbon menus (at least the iTunes carbon menu) are structured has changed in 10.6
if os_version contains "10.5" then
click playlist_popup
pick menu item playlist_name of UI element 2
end if
if os_version contains "10.7" or os_version contains "10.8" then
set frontmost to true
tell window "iTunes"
set focused of pop up button 1 to true
set value of pop up button 1 to playlist_name
-- this 'clicks' the button; bad hack for iTunes DJ
delay 0.5
keystroke " "
delay 0.5
keystroke " "
delay 0.5
click button "Refresh"
end tell
else
--return name of every menu item
--tell application "System Events"
-- key code 125 using option down
--end tell
--return every UI element
--return menu 1 of window "iTunes"
--click menu item playlist_name of menu of window "iTunes"
--click menu item playlist_name
end if
set switchingSuccess to true
exit repeat
on error errorMessage number errorNumber
set errorString to errorString & " " & errorMessage & "(" & errorNumber & ")"
delay 1
end try
end repeat
--some useful debugging things
--return name of every UI element
--perform action "AXPress" of playlist_popup
--return value of attribute "AXChildren" of UI element 2
set visible to false
end tell
end tell
refresh_dj_playlist()
if length of errorString > 1 then
if switchingSuccess then
do shell script "/usr/bin/logger " & quoted form of errorString
else
return "Error: " & errorString
end if
end if
end timeout
on error errorMessage number errorNumber
return "Error: setting dj playlist: " & errorMessage & " " & errorNumber
end try
end select_dj_playlist
on normalize_itunes_browser()
try
with timeout of 10 seconds
tell application "iTunes"
if (view of front browser window) is playlist "iTunes DJ" then
set view of front browser window to playlist "Music"
end if
set view of front browser window to playlist "iTunes DJ"
end tell
end timeout
on error
return "Error: refreshing dj playlist"
end try
end normalize_itunes_browser
on dump_itunes_interface()
tell application "System Events"
tell process "iTunes"
return entire contents
end tell
end tell
end dump_itunes_interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment