Talk less, experience more.

Archives
About

Oh Apple, if only I could...

Posted by robert on September 10, 2008 at 11:43 PM

Geniussss

Update: save the script below as an application and place it in your iTunes scripts directory. Assign a keyboard shortcut to the genius script's menu item (in your keyboard system preference pane) and you can trigger the script from the DarwiinRemote application. Ultra geekness with a genius :-)
__________________________________________

and that's why I slapped together a less than elegant Applescript 'solution' to activate the Genius function on the currently playing track through an Applescript so I can try and activate it from my Wiimote (Genius needs direct, one-button access in order to be truly great). The problem is that Apple does not support GUI scripting very well in iTunes, with the result that you have to activate the iTunes window so that we can use a 'click' applescript command to activate the button. Here's the script:

-- initialise
global iTunesHidden
global frontApp

set iTunesHidden to 0
-- store the name of the active application (fast)
set frontApp to my getFrontApp()
on getFrontApp()
set colon to ":" as Unicode text
set dot to "." as Unicode text
set appPath to (path to frontmost application as Unicode text)
considering case
if (appPath ends with colon) then
set n to -2
else
set n to -1
end if
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to colon
set appname to text item n of appPath
if (appname contains dot) then
set AppleScript's text item delimiters to dot
set appname to text 1 thru text item -2 of appname
end if
set AppleScript's text item delimiters to astid
end considering

return appname
end getFrontApp

-- check if iTunes is hidden and active
tell application "System Events"
if visible of process "iTunes" is false then
set iTunesHidden to 1
end if
end tell
if frontApp is not "iTunes" then
tell application "iTunes"
activate
reveal current track
end tell
end if

-- activate Genius
tell application "System Events"
get properties
get every process
if UI elements enabled then
tell process "iTunes" to click button 10 of window "iTunes"
end if
--hide iTunes if it was hidden
if iTunesHidden is 1 then
set visible of process "iTunes" to false
end if
end tell

-- restore previous application
if frontApp is not "iTunes" then
tell application frontApp
activate
end tell
end if

Wonderful... Now I need to figure out how to integrate this into the iTunes script menu (which requires me to most likely remove half of the code in this masterpiece).