AppleScript,从文件读取/写入数据(脚本的配置文件)

AppleScript, read / write data from file (config file for script)

我正在创建一个引导脚本,该脚本将 运行 在多个不同位置的多台计算机上运行。 此脚本将同步字体、连接到 SMB 共享等。

不同的位置使用不同的 IP、文件夹结构等。所以我想要我的脚本 读取和写入自定义配置文件,我将存储在每个位置。

如何从外部文件读取和写入变量?

我需要读写的一些变量的例子

Raidfolder: Volumes/QNAP-RAID/FILES #POSIX path
RaidSYM: ~/Desktop/RAID
Mount: user:password@192.168.0.10

有没有一种聪明的方法可以从“Raidfolder”中读取信息并将其用作脚本中的变量。 我还想存储一些用户选择的文件夹 脚本将在第一次请求 运行

choose folder with prompt "Please choose a folder:" default location alias PrTemplates

我做到了,管理将属性写入 .sctp,但从 POSIX 路径收到 unicode 文本错误, 我也更喜欢明文格式的配置,所以它可以在 texteditor 中编辑。

感谢任何帮助。谢谢约翰·艾弗

AppleScript, read / write data from file (config file for script)

看看 Working with Property List Files,这是我要走的路。


以下示例 AppleScript code 已在 Script Editor 中测试macOS Catalina 下正常工作,并展示了如何创建 ~/Desktop/Example.plist file 然后读回来。两条 log ... 行只是为了在 回复 窗格 中显示 变量 具有在从 ~/Desktop/Example.plist file[=62 读回之前被设置为不同的 value =].

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 file 显示了创建的 ~/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 Text file,而不是其他东西structured 喜欢 XML Plist file,然后看看:Reading and Writing Files


备注:

  • RE: "Mount: user:password@192.168.0.10" -- 存储用户名密码[=62不是一个明智的主意=] 纯文本 file.