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

发布于 2024-10-03 16:01:36 字数 720 浏览 9 评论 0原文

如何检查一个人是否有头像?怎么取消设置呢?如何从文件加载头像?来自网络、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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-10 16:01:36

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

您不应该需要错误处理程序。只需使用image of [somebody] ismissing 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

>怎么取消设置呢?

要删除人物图像,请将其设置为缺失值

>如何从文件加载头像?

要从文件加载图像,请将其读取为 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 Access Scripting(在 /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

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

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

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

您可以使用 将剪贴板设置为,但您可能需要 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

> 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文