【问题标题】:save contact's profile picture using JavaScript for Automation使用 JavaScript 为自动化保存联系人的个人资料图片
【发布时间】:2016-08-15 17:20:55
【问题描述】:

我想将联系人的头像写入文件。

1) 如何读取 TIFF 图像?

字典描述图片属性:image (*TIFFPicture* or missing value) : Image for person.

在字典中 TIFFPicture 是一个链接,但是当我单击它时,没有其他信息。 阅读它时,它的值似乎是<>。 如何将其作为图像读取?

2)TIFF图像怎么写?

将其写入文件时,应指定什么格式? 字典上写着:[as: type class] : how to write the data: as text, data, list, etc.

如果写入图像需要as:,应该指定什么类型?当我使用例如as: 'data' 错误是“无法转换类型”。

示例:

var app = Application.currentApplication();
app.includeStandardAdditions = true;

myPeople = Application("Contacts").people.whose({ _and: [{lastName: "Doe" },{firstName: "John"}] });

for (i in myPeople) {
    person = myPeople[i];

    if (person.image() == null) continue;

    var data = person.image();

    var file = Path("/var/tmp/test.tiff");

    app.openForAccess(file, { writePermission: true });
    app.setEof(file, {to:0} ); // reset length
    app.write(data, {
        to: file,
        //as: 'data',
    });

    app.closeAccess(file);

    // result: file with 2 bytes ("<>")
}

【问题讨论】:

  • 在 SE 中查看 data 变量的实际值是多少? JXA 将 Apple 事件数据类型与 JavaScript 桥接有各种缺陷,因此可能 1. JXA 不知道如何处理您调用 person.image() 时联系人返回的 typeTIFF 描述符,或者 2. 它不知道如何在随后的 write 命令中将该值打包回 Apple 事件描述符中。最好的办法是先用 AppleScript 编写你的脚本:如果它在那里工作,你就知道它是 JXA 被破坏和废话;如果它在 AS 中也失败,则问题可能出在其他地方(例如联系人)。
  • 实际值为字符串"&lt;&gt;"(如果没有图像,则为null)。很久没用AppleScript了,希望能用JXA解决。
  • 顺便说一句,图像描述符类型在使用 IME 时总是有点麻烦,所以如果它在 AS 中也失败了,请不要过于惊讶。 (考虑到 Apple 的 schlonky API 和垃圾文档,应用程序开发人员很难正确实现标准的 Apple 事件功能,更不用说事后半途而废了。)
  • 天哪。 :-) 相关和/或不同的方法:个人资料图片是否存储在文件系统的某个地方?它曾经是~/Library/Application Support/AddressBook/Images,但现在似乎不再是这种情况了。
  • 不幸的是,JXA 是由 Apple 工程师编写的,他们不了解 Apple 事件的设计原理,更不用说现实世界的应用程序如何实际使用它们。 唯一支持正确工作的解决方案是 AppleScript,所以咬紧牙关尝试用它编写脚本。

标签: applescript javascript-automation


【解决方案1】:

foo's advice 之后,这是一个有效的 AppleScript 版本:

tell application "Contacts"
    set {name:personName, image:personImage} to (get my card)
    set filePath to (((path to desktop folder) as text) & personName & ".tif")
    set theFile to open for access file filePath with write permission

    set eof of theFile to 0
    write personImage to theFile
    close access theFile
end tell

以及用于自动化的等效 JavaScript(不工作):

var app = Application.currentApplication()
app.includeStandardAdditions = true

var person = Application("Contacts").myCard()
var filePath = Path(app.pathTo("desktop") +"/"+ person.name() +".tif")
var theFile = app.openForAccess(filePath, { writePermission: true })

app.setEof(theFile, { to:0} )
app.write(person.image(), { to: theFile })
app.closeAccess(theFile)

【讨论】:

    【解决方案2】:

    这里是 JavaScript 版本,使用 Objective C 桥和 AddressBook 框架。奖励:转换为 PNG。

    ObjC.import("AddressBook")
    ObjC.import("AppKit")
    
    person = $.ABAddressBook.addressBook.me
    filename = $("~/Desktop/"+ person.displayName.js +".png").stringByExpandingTildeInPath.js
    
    image = $.NSImage.alloc.initWithData(person.imageData)
    imageData = $.NSBitmapImageRep.imageRepWithData(image.TIFFRepresentation)
    png = imageData.representationUsingTypeProperties($.NSPNGFileType, $())
    png.writeToFileAtomically(filename, false)  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2020-01-23
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多