从列表中选择并将plist写为字符串,而不是数组

时间:2014-08-16 21:28:14

标签: applescript

我有以下内容显示两个文本输入提示,然后查询PPTP VPN配置并要求用户“从列表中选择”。然后将结果保存到plist文件中,但vpn_name始终在两个空白数组内编码。我已经尝试将vpn_name的数据类型设置为string,但同样适用。

我怎样才能将其编码为:

<dict>
    <key>password</key>
    <string>fnkdslfsd</string>
    <key>username</key>
    <string>fnsdkfnds</string>
    <key>vpn_name</key>
    <string>SAMSUNG_Android</string>
</dict>

与其目前的编码方式相反:

<dict>
    <key>password</key>
    <string>fnkdslfsd</string>
    <key>username</key>
    <string>fnsdkfnds</string>
    <key>vpn_name</key>
    <array>
        <array>
            <string>SAMSUNG_Android</string>
        </array>
    </array>
</dict>

AppleScript的:

property key_path : "~/Library/Preferences/vpn-keys.plist"
property user_name : ""
property pass_word : ""
property vpn_name : ""

tell application "Finder"
    if user_name is "" then
        set dialog_1 to display dialog "Please enter your server username: " default answer ""
        set the user_name to the text returned of dialog_1
    end if

    if pass_word is "" then
        set dialog_2 to display dialog "Please enter your server password: " default answer ""
        set the pass_word to the text returned of dialog_2
    end if

    if vpn_name is "" then
        tell application "System Events"
            tell current location of network preferences
                set VPN_list to get name of every service whose (kind is greater than 11 and kind is less than 17)
            end tell
        end tell

        vpn_name as string
        set vpn_name to {choose from list VPN_list with prompt "Select Office VPN"}
    end if


    tell application "System Events"
        set the parent_dictionary to make new property list item with properties {kind:record}
        set the plistfile_path to key_path
        set this_plistfile to ¬
            make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
        make new property list item at end of property list items of contents of this_plistfile ¬
            with properties {kind:string, name:"username", value:user_name}
        make new property list item at end of property list items of contents of this_plistfile ¬
            with properties {kind:string, name:"password", value:pass_word}
        make new property list item at end of property list items of contents of this_plistfile ¬
            with properties {kind:string, name:"vpn_name", value:vpn_name}
    end tell
end tell

1 个答案:

答案 0 :(得分:3)

从列表中选择会返回所选项目list,并在取消时返回false。您在调用list命令之前定义了一个空字符串,但在AppleScript中,行之间没有任何影响。因此,您可以做的是在继续之前测试结果是否为false,因为与显示对话框不同,当用户按下取消按钮时脚本会继续。然后将新属性列表项的值设置为item 1 of vpn_name

property key_path : "~/Library/Preferences/vpn-keys.plist"
property user_name : ""
property pass_word : ""
property vpn_name : ""

tell application "Finder"
    if user_name is "" then
        set dialog_1 to display dialog "Please enter your server username: " default answer ""
        set the user_name to the text returned of dialog_1
    end if

    if pass_word is "" then
        set dialog_2 to display dialog "Please enter your server password: " default answer ""
        set the pass_word to the text returned of dialog_2
    end if

    if vpn_name is "" then
        set vpn_name to false
        tell application "System Events"
            tell current location of network preferences
                set VPN_list to get name of every service whose (kind is greater than 11 and kind is less than 17)
            end tell
        end tell

        set vpn_name to choose from list VPN_list with prompt "Select Office VPN"
        if vpn_name is false then return --stop, user pressed cancel
    end if


    tell application "System Events"
        set the parent_dictionary to make new property list item with properties {kind:record}
        set the plistfile_path to key_path
        set this_plistfile to ¬
            make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
        make new property list item at end of property list items of contents of this_plistfile ¬
            with properties {kind:string, name:"username", value:user_name}
        make new property list item at end of property list items of contents of this_plistfile ¬
            with properties {kind:string, name:"password", value:pass_word}
        make new property list item at end of property list items of contents of this_plistfile ¬
            with properties {kind:string, name:"vpn_name", value:item 1 of vpn_name}
    end tell
end tell