代码之家  ›  专栏  ›  技术社区  ›  Zygmunt

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

  •  2
  • Zygmunt  · 技术社区  · 15 年前

    你如何检查一个人是否有化身?你如何使它不稳定?如何从文件加载虚拟人物?从网络、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 回复  |  直到 15 年前
        1
  •  6
  •   outis    15 年前

    >如何检查一个人是否有化身?

    您不需要错误处理程序。只是使用 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 access 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 image、gif、jpeg、macpaint、jpeg2、sgi、psd、tga、text、pdf、png和tiff格式。我希望 read ( 2 )命令支持相同的功能。请注意,图像类型 阅读 命令被指定为 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
    
    推荐文章