脚本错误:can';无法在Applescript中获取文件?
我正试图用applescript创建一个plist文件,它将包含预先确定的文本。我需要能够将此文本写入文件“脚本错误:can';无法在Applescript中获取文件?,applescript,Applescript,我正试图用applescript创建一个plist文件,它将包含预先确定的文本。我需要能够将此文本写入文件“plistfile.plist”,但每当我运行脚本时,就会出现错误“无法获取文件(别名“MacintoshHD:Users:myusername:Desktop:plistfile.plist”)”。我的剧本是: set plistfilename to POSIX file "/Users/myusername/Desktop/plistfile.plist" as alias set
plistfile.plist
”,但每当我运行脚本时,就会出现错误“无法获取文件(别名“MacintoshHD:Users:myusername:Desktop:plistfile.plist”)
”。我的剧本是:
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
我对这些都不太熟悉,但我认为这是有道理的。我在谷歌上搜索了一下,在其他任何地方都没有发现这个问题。如果有人能让我知道我遗漏了什么,我将不胜感激。代码失败,因为如果文件不存在,强制
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
扩展名
要创建real属性列表文件,可以使用系统事件
:
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>
布尔键
串键
plist文本