[Pythonmac-SIG] appscript + iPhoto problem

has hengist.podd at virgin.net
Wed Jul 13 14:31:25 CEST 2005


Gábor Farkas wrote:

>>what i want to achieve is the following.
>>
>>i want to find a photo in iphoto (i can do that),
>>and then assign some keywords to it.
>[...]
>ok, now i know more ;)
>
>i can use:
>
>iphoto = appscript.app('iPhoto')
>photos = iphoto.photos.get()
>photo = photos[0]
>photo.title.set('newTitle')

Simpler:

iphoto = appscript.app('iPhoto')
iphoto.photos[1].title.set('newTitle')


>and this works.
>
>my other problem now is the keywords...
>
>which seems to be an array...

Actually a one-to-many relationship, called 'elements' in appscript/AppleScript jargon.

OK, quick summary since I've not finished writing this stuff up properly yet:

Mac application scripting is based on procedural RPC + relational queries. Appscript and AppleScript tend to obfuscate this mechanism under OO-like syntactic sugar, but don't let this fool you. The syntax is merely there to make it nicer to use; you'll see a similar sort of approach used in SQLObject.

Basically what you do is construct a query identifying the object or objects you want to manipulate, then send a command to that application with that query as [usually] its direct parameter. The application evaluates that query against its Apple Event Object Model, finds the object(s) to manipulate and performs the appropriate action on it/them.

The application's dictionary, which you can view with htmldoc or the built-in help() system, [inadequately] documents the application's AEOM and all the commands the application can respond to. There are various classes of object comprising the AEOM, with the root node being the 'application' object. Every object may have zero or more attributes (e.g. name, class, size, bounds, etc. properties), zero or more one-to-one relationships (e.g. Finder's home property, iTunes' current_album and current_track properties), and zero or more one-to-many relationships (e.g. Finder's items, containers, disks, folders, etc. elements).

Attributes usually contain simple values (strings, numbers, etc.) describing an object (e.g. its name, size, position or colour). One-to-many relationships usually (but not always) reflect the objects' containment structure (e.g. a Finder folder object can contain any number of folder, alias_file, application_file, document_file, etc. objects). One-to-one relationships often provide shortcuts to objects of special interest (e.g. iTunes' currently playing track).


The appscript documentation will keep you right on legal syntactic forms, special cases, etc., so make sure you read that too.


>i tried to simply get it's keywords, append a new keyword to the array, and set it, but it did not work:

Hopefully the above makes clear why array manipulation doesn't (and can't) work.


>i get an "Application error 8 (NSInternalScriptError)"

Cocoa apps tend to provide spectacularly bad error messages. (Feel free to file lots of irritatedly impatient feature requests on this.) This is one of those areas where most application's documentation is sorely inadequate, and you have to resort to educated guesswork based on general knowledge of how the AEOM works. In general, new elements are created using the 'make' command, so try something like:

iphoto = appscript.app('iPhoto')
iphoto.photos[1].keywords.end.make(new=k.keyword, with_properties={k.name: 'some name'})

Can't test that here since I accidentally trashed my copy of iPhoto 2 on my last system reinstall and refuse to pony up $50 for iLife just to replace it. But I reckon it should be close. If not, post back.

HTH

has
-- 
http://freespace.virgin.net/hamish.sanderson/


More information about the Pythonmac-SIG mailing list