Search code examples
applescriptaddressbook

How do you manipulate Address Book images with AppleScript?


How do you check if a person has got an avatar? How do you unset it? How do you load an avatar from a file? From web, Facebook, gravatar? What image types can AppleScript read? Is it possible to copy an existing avatar to clipboard with AppleScript?

EDIT

OK, I know how to check if a person has got an avatar

on run
    using terms from application "Address Book"
        my hasImage(person (random number from 1 to length of (get people of application "Address Book") of application "Address Book") of application "Address Book")
    end using terms from
end run

on hasImage(myPerson)
    using terms from application "Address Book"
        try
            set garbage to image of myPerson
            garbage
            true
        on error number -2753
            false
        end try
    end using terms from
end hasImage

Solution

  • > How do you check if a person has got an avatar?

    You shouldn't need an error handler. Just use image of [somebody] is missing value, where [somebody] is a specifier for an Address Book person.

    tell application "Address Book"
        image of some person is missing value
    end tell
    

    Usage:

    tell application "Address Book"
        set somebody to some person
        if image of somebody is missing value then
            display dialog name of person & " doesn't have an image."
        else
            display dialog name of person & " has an image."
        end if
    end tell
    

    > How do you unset it?

    To remove a person's image, set it to missing value.

    > How do you load an avatar from a file?

    To load an image from a file, read it as TIFF data:

    tell application "System Events"
        set imgfile to file "AENewman.jpg" of folder "friends" of pictures folder
    end tell
    set imgfd to open for access imgfile
    set img to read imgfd as "TIFF"
    
    tell application "Address Book"
        set myFriend to item 1 of ¬
            (people whose first name is "Alfred" and last name is "Newman")
        set (image of myFriend) to img
    end tell
    
    close access imgfd
    

    > From web, Facebook, gravatar?

    You can use URL Access Scripting (in /System/Library/ScriptingAdditions/URL Access Scripting.app, should you wish to view the scripting dictionary) to download an image to a file, then load it as above.

    on setABImage for somebody from |url|
        tell application "System Events" to set tempFile to ¬
            make new file at end of temporary items folder of user domain
        set tempPath to path of tempFile
        tell application "URL Access Scripting" to download |url| ¬
            to tempPath replacing yes
        set imgfd to open for access tempFile
        tell application "Address Book" to set (image of somebody) ¬
            to (read imgfd as "TIFF")
        close access imgfd
        tell application "System Events" to delete tempFile
    end setABImageURL
    

    > What image types can AppleScript read?

    AppleScript's Image Events can read PICT, Photoshop, BMP, QuickTime Image, GIF, JPEG, MacPaint, JPEG2, SGI, PSD, TGA, Text, PDF, PNG, and TIFF formats. I expect the read command supports the same. Note that image types in a read command are specified as OSTypes. Address Book only supports TIFF.

    > Is it possible to copy an existing avatar to clipboard with AppleScript?

    You can set the clipboard to just about anything using set the clipboard to, though you might need an as clause to set the clipboard to the contents rather than a reference.

    tell application "Address Book" to set the clipboard to ¬
        the image of item 1 of (person whose name is "Alfred E Newman") as data