如何使用AppleScript操作通讯簿图像?

时间:2010-11-20 07:42:22

标签: 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

1 个答案:

答案 0 :(得分:6)

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

您不需要错误处理程序。只需使用image of [somebody] is missing value,其中[somebody]是地址簿人员的说明符。

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

用法:

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

>你怎么解开它?

要删除某人的图片,请将其设为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

>来自网络,Facebook,gravatar?

您可以使用URL访问脚本(在/ System / Library / ScriptingAdditions / URL中访问Scripting.app,如果您希望view the scripting dictionary)将download图像添加到文件中,然后将其加载为上方。

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

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

AppleScript的Image Events可以读取PICT,Photoshop,BMP,QuickTime图像,GIF,JPEG,MacPaint,JPEG2,SGI,PSD,TGA,文本,PDF,PNG和TIFF格式。我希望read2)命令支持相同的命令。请注意,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