0

我想将联系人的个人资料图片写入文件。

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 ("<>")
}
4

2 回答 2

0

这是 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)  
于 2016-08-15T09:20:55.230 回答
0

按照foo 的建议,这是一个有效的 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)
于 2016-08-07T17:42:08.497 回答