First post (in months)!
This week was the first time I’ve looked at AppleScript, Apple’s built-in scripting language that can be used for routine tasks, or controlling other applications. The reason I even bothered to investigate it was because when I moved all my work onto my laptop I also moved my music collection to an external hard drive and played it via the laptop (since it seemed a bit silly to have a whole other computer on just playing music). And as it goes, iTunes is _the_ music library/player for this platform, so I didn’t really have a huge choice in what to use. Granted, iTunes on OS X is a lot better than the Windows version, much like Safari Windows versus OS X Safari.
Anyway, one feature that that iTunes really lacks is the ability to find a track in your music library and hit “queue” to queue the selected song to play directly after the current song has finished. There is “Play this song next in Party Shuffle”, but it stops the song that is currently playing and jumps straight to the party shuffle playlist.
I decided to try write some code to add this feature in, and a very cool feature of AppleScript is that you can write something in the editor and then copy it to the application’s Script folder, and it’ll automatically appear in the Script menu (provided the application has an AS bridge) when you fire up the app (pretty much like a plugin). This is great, because it means I can write a helper script for a closed source program and have it installed in a matter of minutes. There’s no need to recompile the parent application, or find a suitable scripting bridge language, it all just works straight away.
I’m sure I have missed a simpler way of adding this functionality, but as far as I can tell because these scripts are external to the application, you cannot implement stateful operations without using a “Stay open” script, which is Apple-speak for a script that stays in the background until you tell it to quit (most scripts are once off operations that quit once finished).
To install, open ScriptEditor in your applications folder, paste the below code in, then Save as:
/Users/username/Library/iTunes/Scripts/Queue Next
Make sure you save the script as “application” type, with the Stay Open checkbox checked. Restart iTunes and there should be a new menu icon just before “Help” in the menu bar, it looks like a little scroll. Click it and you’ll find the name of your script. You can then open System Preferences - Keyboard & Mouse, and add a new keyboard shortcut to iTunes that calls the script.
global queuePlaylist
tell application "iTunes"
if not (exists playlist "Queued") then
make new user playlist with properties \
{name:"Queued", shuffle:false, song repeat:off}
end if
set queuePlaylist to playlist "Queued"
if current playlist is not queuePlaylist then
set curtrack to (duplicate current track to \
queuePlaylist)
else
set curtrack to {}
end if
if selection is not {} and current playlist is not \
queuePlaylist then
set sel to a reference to selection
duplicate sel to queuePlaylist
end if
if curtrack is not {} then
set playPosition to (player position + 1)
play curtrack
set player position to playPosition
end if
end tell
on idle
tell application "iTunes"
if player state is not stopped and current playlist \
is queuePlaylist then
return (duration of current track) - player position
else
play playlist "Music"
set view of front browser window to playlist "Music"
tell me to quit
end if
end tell
end idle
The original code for the queue script came from Mac OS X Hints.
Here’s a post explaining how to use Ruby to replace AppleScript.
This syntax highlighter doesn’t like long lines, and this new Wordpress keeps fucking my stuff up (I’ve marked wrapping lines with \’s in the code). Trying to fix.
Kosta Kontos on February 7th, 2008
wb ;-)
I’ve yet to take the plunge onto Mac / OS X. Been using Ubuntu on my laptop for the last year and am very happy with that.
Mind you, if I also scored a free Mac I wouldn’t exactly let it gather dust.
I polish my paper-weights :P
Chris M on March 7th, 2008
That’s pretty wicked, I’m still not convinced by Mac, although, if someone got me a Macbook air, I think I would convert quickly ;)