【问题标题】:AppleScript, read / write data from file (config file for script)AppleScript,从文件读取/写入数据(脚本的配置文件)
【发布时间】:2021-03-11 10:53:18
【问题描述】:

我正在创建一个启动脚本,它将在多个不同位置的多台计算机上运行。 此脚本将同步字体、连接到 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


    【解决方案1】:

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

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


    以下示例 AppleScript 代码ma​​cOS 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 namepassword 存储在纯文本 文件中不是一个明智的主意

    【讨论】:

    • 这太完美了!这很有帮助!简单且完美无瑕。谢谢你。现在我也学习了日志功能,好用。
    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多