2017-02-28 15 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を使用することができリアルプロパティリストファイルを作成するには

関連する問題