Thursday, April 1, 2010

Sweetpea3 iTunes Script

I love the Sweetpea3. We got it for my 2.5 year old daughter many months ago, and she's been enjoying it ever since. The one bummer is that it's basically a USB file system, so it lacks any integration with iTunes. Things get even worse if most of your music is in AAC format, since Sweetpea3 only plays MP3s. You have to figure out what kind of format your song is in on iTunes, then maybe change it to MP3, find it on disk, and then copy it to the Sweetpea3 player. What a pain in the ass.

So, being a programmer on paternity leave, I figured I'd learn Applescript and automate the whole process. I wrote a simple script that will copy the highlighted songs in iTunes--automagically converting them to MP3 on-the-fly if needed--to the desired playlist on the Sweetpea3. It wipes all MP3s on the chosen playlist before it copies the new ones. If you've changed the name of your Sweetpea3 to something other than "SWEETPEA3", you're SOL because this little script hardcodes this device name.

Give it a go if you're a Mac user and a Sweetpea3 owner. Drop me a line and let me know how you like it.

My two cents on Applescript is that it's the opposite of Perl: it's easy to read and bloody impossible to write. It's massively frustrating especially if you actually know anything about computers, like file paths, or heaven forbid Unix. So coming from a Java, SQL and Unix background, I found Applescript a real nightmare. But I was able to bang out the core of this script over the course of a half dozen baby naps.

Here's the source. Paste this into the Script Editor application and save it as an "Application Bundle" in your Library/Scripts folder. Then you can access it in the little scripts icon on the toolbar.

It's not terribly elegant, but it makes life easier. 'Nuff said.



on f_exists(the_path)
try
get the_path as alias
return true
on error
return false
end try
end f_exists

if not f_exists(":Volumes:SWEETPEA3") then
display dialog "Sweetpea MP3 player is not attached."
return
end if

tell application "iTunes"
copy selection to selectedTracks

if length of selectedTracks is 0 then
display dialog "No tracks have been selected in iTunes. Please select tracks (not just a playlist) explicitly."
return
end if
end tell

set playList to choose from list {"Playlist 1", "Playlist 2", "Playlist 3"} with prompt "Which Playlist?"

if playList is "" then return

if playList is equal to {"Playlist 1"} then
set destinationDir to "/Volumes/SWEETPEA3"
else if playList is equal to {"Playlist 2"} then
set destinationDir to "/Volumes/SWEETPEA3/play2"
else if playList is equal to {"Playlist 3"} then
set destinationDir to "/Volumes/SWEETPEA3/play3"
else
display dialog "No playlist selected."
return
end if

tell application "iTunes"
copy selection to selectedTracks

if length of selectedTracks is 0 then
display dialog "No tracks selected in iTunes. Please select tracks (not just a playlist)."
return
end if

tell application "Finder"
do shell script "rm -f " & quoted form of destinationDir & "/*.mp3"
end tell

set numTracks to count selectedTracks

if numTracks is 0 then
display dialog "No tunes selected. Please select the tracks you'd like to copy."
quit
end if

repeat with t in selectedTracks

set trackLocation to get location of t
set pathName to POSIX path of trackLocation
set trackFormat to get kind of t

if trackFormat is "AAC audio file" then

set current encoder to encoder "MP3 Encoder"
set copiedTrack to item 1 of (convert t)
set copiedTrackPath to POSIX path of (get location of copiedTrack)

tell application "Finder"
do shell script "mv " & quoted form of copiedTrackPath & " " & quoted form of destinationDir
end tell
delete copiedTrack
else
tell application "Finder"
do shell script "cp -p " & quoted form of pathName & " " & quoted form of destinationDir
end tell
end if
end repeat

end tell

display dialog "Exported " & numTracks & " songs to SweetPea " & playList & "."

No comments:

Post a Comment