如何使用AppleScript操作地址簿图像?

Zyg*_*unt 3 applescript addressbook

你如何检查一个人是否有头像?你怎么解开它?你如何从文件加载头像?来自网络,Facebook,gravatar?AppleScript可以读取哪些图像类型?是否可以使用AppleScript将现有头像复制到剪贴板?

编辑

好的,我知道如何检查一个人是否有头像

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
Run Code Online (Sandbox Code Playgroud)

out*_*tis 7

>你如何检查一个人是否有头像?

您不应该需要错误处理程序.只需使用image of [somebody] is missing value,[somebody]地址簿人员的说明符在哪里.

tell application "Address Book"
    image of some person is missing value
end tell
Run Code Online (Sandbox Code Playgroud)

用法:

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
Run Code Online (Sandbox Code Playgroud)

>你怎么解开它?

要删除某人的图像,请将其设置为missing value.

>如何从文件中加载头像?

要从文件加载图像,请将其作为TIFF数据读取:

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
Run Code Online (Sandbox Code Playgroud)

>来自网络,Facebook,gravatar?

您可以使用URL访问脚本(在/ System/Library/ScriptingAdditions/URL Access Scripting.app中,如果您希望查看脚本字典)将图像下载到文件,然后按上面的方式加载它.

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
Run Code Online (Sandbox Code Playgroud)

> AppleScript可以读取哪些图像类型?

AppleScript的图像事件可以读取PICT,Photoshop,BMP,QuickTime图像,GIF,JPEG,MacPaint,JPEG2,SGI,PSD,TGA,文本,PDF,PNG和TIFF格式.我希望read(2)命令支持相同.请注意,read命令中的图像类型指定为OSTypes.地址簿仅支持TIFF.

>是否可以使用AppleScript将现有头像复制到剪贴板?

您可以将剪贴板设置为几乎任何使用set the clipboard to,但您可能需要一个as子句来将剪贴板设置为内容而不是引用.

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