I come from a very musically inclined family. We do everything to music. From the very first second we open our eyes every morning (to groovy iPhone alarms) we work to music, study to music, eat/cook to music. I could go on, but I think you get the picture. Lately I realized that the first thing I do every day at work login to my MacBook, connect my iPhone, open iTunes, and then start playing my work playlist. I started to wonder whether this whole process could be made easier (yes, I’m incredibly lazy), and decided I’d try to automate the entire process.
If you want to just get at the code, and don’t much care about the thought process/learning curve, the code is at the bottom in green. Jump ahead and start coding for your particular needs.
Learning Automator wouldn’t help :(
The first thing I did, was try to get Automator to do it for me. I learnt how to do it for regular playlists on the mac. Its incredibly easy (I’ll add in a blog about that next), but you can’t get Automator to open up a specific playlist on the iOS device. Just not happening. I struggled for a bit, and then gave up.
I used to hate to do homework in school. But I LOVED the Mac, and I could sit in front of the screen for hours. My dad saw that as an excellent opportunity to get all the tiny tasks on the computer finally done. What tasks you say ? Well, like renaming files to use camel case, or changing file extensions from (say) .cwk to .doc
In any case, I think my 7th/8th grade homework was learning to use AppleScript from several tutorials he downloaded for me. It worked ! I was hooked to the Mac even more !
I hadn’t used AppleScript since the 11th grade though, so considering that a lot of people reading this are new to AppleScript too, I’m gonna add in a explanation of the code & process below too (just in case)
Btw, it took me 34 minutes flat to research, and code, and test and finally run the application :) Yayy me !!
Getting Started with AppleScript
- Goto Spotlight on the Mac, and search for AppleScript Editor. An application should come up. Hit Enter to launch the editor.
Alternatively : Goto :: Applications > Utilities > AppleScript Editor
- You don’t need to know much here, we’re basically going to use the Run and Compile commands throughout this tutorial. So lets jump into the code now ::
The Code ::
tell application “iTunes”
set myiPhone to some source whose kind is iPod
set mainPhonePlaylist to playlist “At Work” of myiPhone
set the view of the front browser window to mainPhonePlaylist
set the EQ enabled to true
set myRandomSong to random number from 1 to count of tracks of (get view of front window)
play track myRandomSong of mainPhonePlaylist
=> tell application “iTunes”
=> end tell
AppleScript tries its best to use plain English wherever possible. Most commands in AppleScript are located inside a “tell block”. To be absolutely, in the “tell block” you tell a particular application what to do. These lines above tells the application “iTunes” to follow all the instructions inside the “tell block”.
A simple example of a tell block would be (and I won’t explain this one, cos its that easy, run it and see in your open AppleScript editor if you wanna try)
tell application “Finder”
Back to our code
=> set myiPhone to some source whose kind is iPod
First we define a variable myiPhone to be our iOS device. iTunes uses sources to distinguish between the different ways it handles data. Other sources could be CDs, iPods, Radio, etc. You have to determine what kind of source it is before you can access the playlists associated with that source.
Here we find any source of kind iPhone and create a sort of pointer to that source with our variable myiPhone. (do not ask me what happens if multiple iOS devices are connected, try for yourself to find out)
=> set mainPhonePlaylist to playlist “At Work” of myiPhone
We have now located the desired playlist on the iOS device that we want to play from, and again associated a variable to point to our playlist. In this case, my playlist is called “At Work” on the iPhone.
=> set the view of the front browser window to mainPhonePlaylist
The main window of iTunes is called the “browser window”. Presumably you can have only one open at a time (unless of course you’re looking at say the artwork or a video, etc). Anyway, to be sure, we’ll call the front browser window. In that we will use the view property. The view is a property of both playlist window and the browser window. So to get the reference to the “front browser window” we’ll be incredibly specific, and use the entire sentence “view of the front browser window” in our code. (You see how like English AppleScript is ? We’re trying to avoid dangling modifiers of a coding sort — Grammar Nazis will love this)
We have now set the selected playlist to the view of the front browser window. Your iOS device is now selected in iTunes, and the corrected playlist is open. All that’s left is to play a song from it.
=> set EQ enabled to true
The Equalizer was turned on (this is for my benefit, I am picky about equalizer settings on songs, you can skip this)
=> set myRandomSong to random number from 1 to count of tracks of (get view of front window)
This is a nested method call (of sorts). First I got the view of the playlist. Since I had set it to the front window earlier, this becomes easy with the get method called.
Lets break it up ::
set tempPlaylistPointer to get view of front window //get the view as before
set countTracks to count of tracks of tempPlaylistPointer //find number of tracks in playlist
set myRandomSong to random number from 1 to countTracks //generate a random number
Those three lines above accomplish the same thing that the single line I used in the code does. And I hope its clear now. Finally, to play the random song we just selected we end with
=> play track myRandomSong of mainPhonePlaylist
Saving the application you just made
Yes you made an application for the Mac. Its tiny, and doesn’t do much, but it does what you wanted it to do and for now that’s good enough :)
Press Command+S on the Keyboard, choose the File Format to be Application. Make it “Run Only”, and then save it somewhere on your hard disk. Drag the newly saved application icon to your Dock.
Done – UTSL !!
Now connect your iPod/iPhone to the computer, click on your applet in the Dock, and voila a random song will play from your iOS playlist every time you run the applet. No more mundane app launching. You’re a geek now !!