[Pythonmac-SIG] O'Reilly article, iPhoto scripting, Python?

has hengist.podd at virgin.net
Tue Mar 2 18:49:42 EST 2004


Laurent Pierron wrote:

>Oh Yes, very exciting, so the AppleScript is :
>
>tell application "iPhoto"
>	remove (photos of photo library album where height < 200 or 
>width < 200)
>end tell
>
>In Python, I'll like to write that :
>
>import iPhoto
>map(iPhoto.remove, [photo for photo in 
>iPhoto.photo_library_album.photos if photo.width < 200 or 
>photo.height < 200])
>
>Why isn't it possible ?

Two mistakes in the above: 1. there's no 'iPhoto' module, and 2. 
unlike in AppleScript, appscript does not automatically dereference 
object references. [1] Here's how you'd need to write your code:

    import appscript
    iPhoto = appscript.app('iPhoto.app')
    map(iPhoto.remove, [photo for photo in 
iPhoto.photo_library_album.photos.get() if photo.width.get() < 200 or 
photo.height.get() < 200])


Otherwise, what Bob says: if you can use a filter reference to do all 
the heavy lifting, it's simpler to code and much more efficient in 
use than getting a list of object references and individually 
filtering those yourself.

HTH

has

[1] While appscript adopts a Python-like syntax for ease of use, 
don't let that mislead you: it behaves according to the Apple Event 
Manager's rules, not Python's. See January's 'AppleScript' discussion 
for more info on how application object references and the Apple 
Event Manager object model operate.
-- 
http://freespace.virgin.net/hamish.sanderson/



More information about the Pythonmac-SIG mailing list