2016-08-07 9 views
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 ("<>") 
} 
+0

SE에서 볼 때'data' 변수의 실제 값은 무엇입니까? JXA가 자바 스크립트와 애플 이벤트 데이터 타입을 연결하는 데에는 여러 가지 결함이 있습니다. 1. JXA는'person.image()'를 호출 할 때 연락처에 의해 반환 된'typeTIFF' 디스크립터를 어떻게 다루는 지 알지 못합니다. 2. 후속'write' 명령으로 Apple 이벤트 디스크립터에 그 값을 다시 저장하는 방법을 알지 못합니다. 가장 좋은 방법은 먼저 AppleScript로 스크립트를 작성하는 것입니다.이 스크립트가 작동하면 JXA가 파손되고 쓰레기가된다는 사실을 알게됩니다. AS에서도 오류가 발생하면 문제는 아마도 다른 곳 (예 : 주소록)에 있습니다. – foo

+0

실제 값은 문자열 ""<> "'입니다 (이미지가 없으면 null입니다). AppleScript를 사용한 지 오래되었으므로 JXA에서 해결하기를 희망했습니다. – wivku

+0

BTW, 이미지 설명자 유형은 항상 IME를 사용하기에 약간의 janky 였으므로 AS에서도 오류가 발생하면 지나치게 놀라지 마십시오. 앱 개발자가 Apple의 schlonky API 및 쓰레기 워드 프로세서를 사용하여 표준 Apple 이벤트 기능을 올바르게 구현하는 것만으로는 충분하지 않습니다. 반쯤 구워진 후회는 신경 쓰지 마십시오.) – foo

답변

0

가 다음은 JavaScript versi입니다. on, 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) 
0

foo's advice에 따라 여기에 작동하는 애플 스크립트 버전 :

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 

및 자동화에 해당하는 자바 스크립트 (작동하지 않는) :

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)