dg85
dg85

Reputation: 490

Choose from list and write to plist as string, not as array

I've got the following which displays two text input prompts and then queries for PPTP VPN configs and asks the user to 'choose from list'. The results are then saved to a plist file however vpn_name is always encoded inside of two blank arrays. I've tried setting the datatype of vpn_name to string but the same applies.

How can I get this to encode simply as:

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

as opposed to how it's currently encoding:

<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

Upvotes: 1

Views: 574

Answers (1)

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

choose from list returns a list of items selected and returns false when canceled. You defined an empty string before choose from list command is invoked but in AppleScript that doesn't have any effect between lines. So what you can do is test if the results are false before continuing because unlike a display dialog the script continues when the user pressed the cancel button. Then set value of new property list item to 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

Upvotes: 3

Related Questions