2017-02-28 1 views
0

미리 정의 된 텍스트가 포함 된 applescript로 plist 파일을 만들려고합니다. 이 텍스트를 "plistfile.plist"파일에 쓸 수 있어야하지만 내 스크립트를 실행할 때마다 "Can't get file (alias "MacintoshHD:Users:myusername:Desktop:plistfile.plist")"오류가 발생합니다. 내 스크립트는 다음과 같습니다 :스크립트 오류 : AppleScript로 파일을 가져올 수 없습니까?

set plistfilename to POSIX file "/Users/myusername/Desktop/plistfile.plist" as alias 

set ptext to "plist text" 

set plist to open for access file plistfilename & "plistfile.plist" with write permission 
write ptext to plist 
close access file plist 

저는 이것에 익숙하지 않지만이 의미가 있다고 생각했습니다. 나는 인터넷 검색을 해왔고 다른 곳에서는이 문제를 발견하지 못했습니다. 누구든지 내가 무엇을 놓치고 있는지 알려주면 정말 고맙겠습니다.

답변

1

파일이없는 경우 강제 변환 as alias이 오류를 발생시키기 때문에 코드가 실패합니다.

은이 파일에 텍스트를 작성하는 일반적인하고 신뢰할 수있는 방법입니다

set plistfilename to (path to desktop as text) & "plistfile.plist" 

set ptext to "plist text" 

try 
    set fileDescriptor to open for access file plistfilename with write permission 
    write ptext to fileDescriptor as «class utf8» 
    close access fileDescriptor 
on error 
    try 
     close access file plistfilename 
    end try 
end try 

편집 :

스크립트의 결과가 간단한 .txt 파일 당신이 .plist을 통과하더라도 것을 고려 신장.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>booleanKey</key> 
    <true/> 
    <key>stringKey</key> 
    <string>plist text</string> 
</dict> 
</plist> 
:

tell application "System Events" 
    set rootDictionary to make new property list item with properties {kind:record} 
    -- create new property list file using the empty dictionary list item as contents 
    set the plistfilename to "~/Desktop/plistfile.plist" 
    set plistFile to ¬ 
     make new property list file with properties {contents:rootDictionary, name:plistfilename} 
    make new property list item at end of property list items of contents of plistFile ¬ 
     with properties {kind:boolean, name:"booleanKey", value:true} 
    make new property list item at end of property list items of contents of plistFile ¬ 
     with properties {kind:string, name:"stringKey", value:"plist text"} 
end tell 

스크립트 프로퍼티리스트 파일을 생성합니다

System Events 당신이 사용할 수있는 실제 속성 목록 파일을 만들려면