代码失败,因为如果文件不存在,强制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
延期。
要创建您可以使用真正属性列表文件System Events
:
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
的脚本创建此属性列表文件:
<?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>