AppleScript aaaaaargh!

connect.pngI plan to use AppleScript for two small features within the current version of Mugshot: to set finder comments for a downloaded image, and to import a downloaded image into iPhoto.

Until a few weeks ago, I hadn’t ever used AppleScript. But, I found a need to use it as part of my build process (you can read some about the Flickr Service Library file that I use here) so, I bought this book and read it cover to cover. The syntax and grammar for the AppleScript language isn’t that difficult - and the book was very well written and descriptive - so picking up the basics was easy. What I find very difficult, however, is taking an application’s dictionary and trying to put those words together into a meaningful grammar. Image reading through an English language dictionary and then trying to have a conversation - it would be difficult without knowing more about how those words fit together.

The authors admit that this is the biggest challenge in AppleScript - figuring out how to put all the words from a given application together into useful sentences. Some applications - like OmniOutliner - were easy. But, I’m having a bear of a time figuring out how to do things with iPhoto.

Basically, I need to see if a given album exists, and if it doesn’t, create it. Then I need to import a list of images and set the appropriate properties (title, description, keywords, etc.). I can do the import, but so far, getting a specific album or creating an album has eluded me.

If you have any tips or suggestions - either for the general case of going from a dictionary to a useful grammar for a given App, or for the specific case of iPhoto, please drop me a line!

UPDATE: OK, so five minutes after posting this, I read through some of Apple’s iPhoto sample scripts and put together a partial solution:

To get the album - and create it if it doesn’t exist:

		if exists (album album_name) then
			set the_album to album album_name
		else
			set the_album to new album name album_name
		end if

To import (this one was pretty easy):

		import from the_photo_path to the_album

Once I’ve imported the image, I’m still not sure how to get a reference to that specific image so I can set its properties. All of Apple’s example scripts expect images to already be selected.

1 Response to “AppleScript aaaaaargh!”


  1. 1 Daniel Jalkut Dec 5th, 2005 at 4:27 pm

    Hi Blake. It’s definitely a slow, sometimes painful process. I’ve been learning for years and still feel vastly inferior to most scripters.

    First, I have nothing negative to say about the book you bought - I’ve never used it or seen it before. I bet it’s fine. But I have never vouched for any other book on AppleScript besides Matt Neuberg’s book. “AppleScript: the Definitive Guide.” If I could only have one book on AppleScript, that would be it. In fact, it is it.

    Here are some tips for, as you say, going from dictionary to language:

    1. Always start at the top. Look at the “Application” class in the dictionary. It’s tricky because it might be defined twice, as it is in iPhoto. Look for the definition inside the suite that looks most interesting. For instance, in iPhoto there is an application suite in the “iPhoto Suite”. This seems to be where the goodies are (I haven’t scripted iPhoto myself).

    2. From the top, you can play with getting references to things on a very incremental basis. For instance, looking at the application class, I see that application “contains” albums. This means you can ask for that collection of items from AppleScript:

    tell application “iPhoto”
    get albums
    end

    Run that in Script Editor and see what gets spit out in the returned value. My strategy is to build up a script based on what I can get to “return” from the script.

    How you specific a particular object in AppleScript is not always the same from application to application. Often they will mention in the dictionary what the various forms of reference are: “by id,” by name,” etc. In this case the dictionary doesn’t seem too helpful.

    But if you look at the output of “get albums”, you’ll see a bunch of references to albums by name. The format of the reference as returned by the application is often what you can use yourself to specifically identify an item.

    So in my iPhoto library I have an album called “MiscFriends”. I ask iPhoto for the album (omitting tell application block):

    get album “MiscFriends”

    And it returns basically the same reference as I saw in the list of all albums. How do I get details about this?

    You can look in the dictionary at the “album” class to see what elements and properties it (allegedly) contains. Sometimes the dictionary is incomplete or unhelpful. You can sometimes get information directly from the applicatiion through AppleScript by asking for the properties of the object directly. In this case it’s not too helpful, because there’s only one property: name.

    The album class contains other elements, though. So I can for instance ask for:

    get first photo of album “MiscFriends”

    And, looking again for information about the “photo” class in the dictionary, I see that I can ask for info about photos, too. Putting it all together, you could do something with a whose clause:

    get first photo of album “MiscFriends” whose comment is “Wild Party”

    Unfortunately I can’t help you with the “make a new album” part. It’s not immediately obvious to me either how this is done in iPhoto. Often you can use language like this:

    set myAlbum to make new album at end of albums

    But that doesn’t seem to work here.

Leave a Reply