Geek Speak: Random Mac Stuff You Need to Know
An Introduction to AppleScript
by Matt Brewer - Copyright © 2007
Mac OS X has several scripting capabilities built right in. Basic shell scripting, Perl, Python, PHP, and Ruby are ones that come to mind most often, especially if you are coming from the Unix/Linux realm. However, there are two other important features to note that are deeply integrated in Mac OS X: AppleScript and Automator, with its introduction in Tiger. I’ll be talking about Automator in a future article.
Conceptual Overview
AppleScript has been around for ages and has support in most of the mature/full-featured applications today. AppleScript can be used to write applications complete with buttons, sliders and more, by creating a new “AppleScript Application” in XCode. For more information about AppleScript, visit http://developer.apple.com/applescript/ for Apple’s Documentation and The Apple Script Source Book with tons of examples at http://applescriptsourcebook.com/
Our First AppleScript Application
We are now going to get down and dirty with AppleScript. You can download the completed script here - http://www.macfanatic.net/software/samplecode/convert-itunes-applescript.zip
I firmly believe the best way to learn is to just get started, so with a few brief notes, we’ll get started AppleScript is an elegant and simple language. There are several different ways to accomplish the same simple task because of this. This will easily become apparent once we get started. If you’ve ever programmed using another language, this might take some time to get your head around, but you’ll live. For anyone wondering if you should take the time to learn AppleScript, I’d say yes! It was designed for the average computer user to create scripts to make their life easier. So, without further ado, let’s get scripting.
Problem Description
Your friend has just made the switch to Mac and is enjoying himself until he goes to import his music in iTunes. He’s never used an application like iTunes to organize his music before now, choosing to use cryptic filenames instead. To make matters worse, most of the songs are old and don’t contain lovely ID3 Tag info, such as artist information. This makes it a nightmare for iTunes for the following reason: all his song files are named something like “015 - Gwen Stefani - Rich Girl.mp3”. When you import a song like that into iTunes, the artist, album and all other fields are blank and the song’s name contain “015 - Gwen Stefani - Rich Girl”. This system was fine on Windows for your friend, but he really wants to use iTunes and grab artwork and more, all requiring the artist, album, and name fields to contain the right information. He could go through each song (about 800) and edit this via copy/paste, but that would take months. He turns to you for help. Let’s write an AppleScript! (Note: this story is fairly accurate)
First thing we need to do is open the Script Editor. You can find this in /Applications/AppleScript or just by using Spotlight for “Script Editor”. This is the program that we’re going to use to write our scripts. It will check for errors in our syntax (basic structure/use of the language) and let us run our scripts right from this window.
We are going to be doing all of our magic here by interacting with iTunes. The basic approach to tackling this problem is to get a list of selected songs from iTunes, and change the name and artist information for each song. So, to work with iTunes, the first thing we need to type is tell application “iTunes”. Anything following this and before the end tell will be commands given to iTunes. There are several commands available, such as pause/resume playback, creating a new playlist and more. However, we’re just interested in what songs the user has selected in iTunes. To get a list of the selected iTunes tracks, we’ll use set _songs to selection. This command assigns the variable _songs, to the currently selected iTunes tracks. This way, if the user changes the selection while our script is running, we’ll still have the original songs he wanted changed.
Now we’re going to make sure that our friend selected at least one song, because if he didn’t, we don’t have a thing to do. if _songs is {} then display dialog “Please select songs and run the script again.” will take care of that. If the selection was empty, our script will show an error window displaying that message.
Now we are going to count how many songs we have in our list (the ones our friend selected to be converted). We’ll do that by set num_songs to (count of _songs). This statement is a little more complex than our previous ones. We are creating a new variable called num_songs here (and anytime we use the set something to something). Instead of just setting it to a number, we’re going to ask _songs for the number of items it contains, by calling (count of _songs). Then we’ll set num_songs to that.
We now know how many songs we have and we’re going to set up a loop. A loop is a way of writing statements so that the same thing will be done each time, but to different pieces of data. In this case, for each song in our list, we’re going to change the name and artist. AppleScript has a beautiful loop structure. repeat with i from 1 to num_songs. This statement declares i (the letter i, like “eye”) and i will be 1 the first time, 2 the second time, and so on until i is num_songs. We’ll use i as a way to know when we’ve messed with every song in the list and to access each song in the list.
Here is a look at what we’ve done so far. I have placed the end repeat and end if and end tell at the end of each respective call. This acts just like curly braces do in most languages, to let the computer know you are done executing statements that pertained to the loop, or if() statement, etc.
We now need to know what’s in the “Name” field for each song because that’s what contains the information we’ll use to fill in the artist/name fields later. To get a nice “song”, we’ll use set _song to (item i of _songs). Every time we go through the loop, i will increment by 1. Therefore, i will let us access every item in the list of songs. So, every time we go through the loop, if we set our _song to the ith item in the list, we’ll be all set to do whatever we need to with that particular song.
Now we’re going to grab the actual name of the song we’re working with. This will be some text, particularly, “015 - Gwen Stefani - Rich Girl” in our case. set song_path to (name of _song) will give us that text we need. We’ll pick out the parts of that text we need for the name of the song and its artist in the next block of code.
I’m not going to walk through the code for parsing the text for your sake. However, I’ll give an overview of what the code is doing. We have song_path as the name of the song, once again, “015 - Gwen Stefani - Rich Girl” in our example. We don’t need the numbers at the front for anything, but we do need the artist name. The artist’s name is everything in between those two hyphens, with a space before and a space after, that we’ll remove. The code looks for the first hyphen, and then the second hyphen, and copies the text out of that to _artist.
Now we just need the song’s name. This is just everything after the second hyphen, and that gets copied to _name. To actually change the values back in iTunes, the magical code is set artist of _song to _artist as string and set name of _song to _name as string. Once all of the songs are done, we show a window telling the user that everything went okay and we’re done.
Conclusion
That certainly wasn’t an in-depth look at AppleScript by any means. There are several more possibilities if you learn your stuff and explore. Most applications will come with scripts available to use and there are tons built in to Mac OS X itself. Just fire up the AppleScript Utility and select the “Show Script Menu in MenuBar” to have a list of pre-installed scripts at your disposal.
With some tweaking you could make this little script more powerful by first making sure that the song being converted meets the criteria (this will crash if it gets a song with more hyphens, spaces, or more) and letting the user set some settings for the format. The latter suggestion could would make it easier for someone to use it if “Stephani, Gwen - Rich Girl” was the format of the names. You get the picture.
If you have any questions/suggestions about what you’ve read here today, then be sure to drop me an email at the one listed below. Thanks for reading! I also encourage you to visit the Mac Fanatic Message Boards at http://macfanatic.net/board/viewforum.php?f=11 and look for this article as a thread. Let me know if you’d like another article like this one soon! Then look around the message boards for more great advice and discussion.
Contact Info
You can always send me an email at mbrewer@maccompanion.com or visit my website at http://www.macfanatic.net for more information about me and my ramblings. I also produce a weekly audio podcast taking an in-depth review of cool and new Mac applications, along with tips, developer interviews, tutorials, and the occasional tutorial.