AppleScript,从文件读取/写入数据(脚本的配置文件)
看看Working with Property List Files,这是我要走的路。
以下示例 AppleScript 代码在macOS Catalina下的脚本编辑器中进行了测试> 并按原样正常工作,并展示了如何创建 ~/Desktop/Example.plist file 并将其读回。两行log ... 只是为了在回复 窗格中显示变量已被设置为不同的值 em> 在从 ~/Desktop/Example.plist 文件读回之前。
set RaidFolder to "/Volumes/QNAP-RAID/FILES"
set RaidSYM to "~/Desktop/RAID"
set Mount to "user:password@192.168.0.10"
tell application "System Events"
set theParentDictionary to ¬
make new property list item with properties {kind:record}
set thePropertyListFilePath to "~/Desktop/Example.plist"
set thePropertyListFile to ¬
make new property list file with properties ¬
{contents:theParentDictionary, name:thePropertyListFilePath}
tell property list items of thePropertyListFile
make new property list item at end with properties ¬
{kind:string, name:"RaidFolder", value:RaidFolder}
make new property list item at end with properties ¬
{kind:string, name:"RaidSYM", value:RaidSYM}
make new property list item at end with properties ¬
{kind:string, name:"Mount", value:Mount}
end tell
end tell
set RaidFolder to missing value
set RaidSYM to missing value
set Mount to missing value
log RaidFolder & linefeed & RaidSYM & linefeed & Mount
tell application "System Events"
tell property list file thePropertyListFilePath
set RaidFolder to value of property list item "RaidFolder"
set RaidSYM to value of property list item "RaidSYM"
set Mount to value of property list item "Mount"
end tell
end tell
log RaidFolder & linefeed & RaidSYM & linefeed & Mount
示例 XML Plist 文件 显示了作为 ~/Example.plist 文件 创建的内容。它是一个结构化 ACSII 文本 文件,可以在任何文本编辑器中进行编辑。
<?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>Mount</key>
<string>user:password@192.168.0.10</string>
<key>RaidFolder</key>
<string>/Volumes/QNAP-RAID/FILES</string>
<key>RaidSYM</key>
<string>~/Desktop/RAID</string>
</dict>
</plist>
如果你想使用 plain ACSII 文本 文件,而不是像 结构化 这样的 XML Plist file,然后看看:Reading and Writing Files
注意事项:
- RE: "Mount: user:password@192.168.0.10" -- 将 user name 和 password 存储在纯文本 文件中不是一个明智的主意。