AppleScript stringWithFormat

The task today for Annoture is localization. I’ve been looking far and wide across the Internet for an AppleScript equivalent to Cocoa’s stringWithFormat method to no avail. The lack of such a native function makes localizing an AppleScript Studio application difficult, as you can’t easily create formatted localized strings such as:

“%d image annotations were copied from %s to %s”

The alternative of creating several strings that I would have to concatenate was not very palatable, so I ended up writing a simple function that uses AppleScript’s do shell script command to call printf.

on localized_formatted_string(key_string, parameters)
	set cmd to "printf " & quoted form of¬
		(localized string key_string from table "Localizable")

	repeat with i from 1 to count parameters
		set cmd to cmd & space &¬
			quoted form of ((item i of parameters) as string)
	end repeat

	return do shell script cmd
end localized_formatted_string

If you’re not doing localization but want printf functionality in AppleScript, take out the line localized string key_string from table "Localizable" and replace with simply key_string. Parameters is simply a list of parameters to the printf command.

Now we’ll see how long it takes Google to index this post so people in the future don’t have to struggle as much as I did!

3 thoughts on “AppleScript stringWithFormat

  1. Adam,

    Not related directly to this post, but as someone who’s plumbed the depths of Aperture’s AppleScript you might know this answer.

    There’s no way in Aperture to change the image date value Aperture stores in its database. We know it doesn’t have to be the same as the embedded image metadata value because if you import from iPhoto Aperture stores iPhoto’s user defined date value.

    Is there a Scriptable API for changing the value of the Aperture database date value?

  2. John:

    In the table zrkversion, there are numerous timestamp fields. The one you’re looking for is ZIMAGEDATE. The value looks to be in NSDate format. You can use the following AppleScript to convert it into an AppleScript date object:

    set theDate to call method "dateWithTimeIntervalSinceReferenceDate:" of class "NSDate" with parameter (theTimestamp as real)

    There’s no AppleScript API in Aperture’s dictionary to access this data, so you’ll need to go directly into the SQLite3 database of Aperture. Perhaps I’ll write an application that helps people edit the image date. Email me if you have any suggestions (I’ll poke over the Apple Discussion Forums for more details as well as posting a dedicated blog entry about this).

  3. Evan

    thanks for the code. I just needed to do some fancy printf formatting so I didn’t need the localization stuff. Can’t hurt to give the simplified code directly (though I really just followed your suggestion):

    on formatted_string(key_string, parameters)
    set cmd to "printf " & quoted form of key_string

    repeat with i from 1 to count parameters
    set cmd to cmd & space & quoted form of ((item i of parameters) as string)
    end repeat
    return do shell script cmd
    end formatted_string

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: