new to applescript. need to create a plist file using applescript

Needed some help I need on creatinga plist file below using applescript and I can't make it happen needed some hand on this.


<?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>Username</key>

<string>${localAdminUser}</string>

<key>Password</key>

<string>${localAdminPassword}</string>

<key>AdditionalUsers</key>

<array>

<dict>

<key>Username</key>

<string>${userName}</string>

<key>Password</key>

<string>${userPassword}</string>

</dict>

</array>

</dict>

</plist>



I have tis code but it doesn't seems to work.

tell application "System Events"


-- create an empty property list dictionary item

set the parent_dictionary to makenewproperty list itemwith properties {kind:record}


-- create new property list file using the empty dictionary list item as contents

set the plistfile_path to "~/Desktop/example.plist"

set this_plistfile to ¬


makenewproperty list filewith properties {contents:parent_dictionary, name:plistfile_path}


-- add new property list items of each of the supported types


makenewproperty list itemat end of property list items of contents of this_plistfile ¬

with properties {kind:string, name:"Username", value:"${localAdminUser}"}


makenewproperty list itemat end of property list items of contents of this_plistfile ¬

with properties {kind:string, name:"Password", value:"${localAdminPassword}"}


makenewproperty list itemat end of property list items of contents of this_plistfile ¬

with properties {kind:list, name:"AdditionalUsers"}


makenewproperty list itemat end of property list items of contents of this_plistfile ¬

with properties {kind:string, name:"Username", value:"${localAdminUser}"}


makenewproperty list itemat end of property list items of contents of this_plistfile ¬

with properties {kind:string, name:"Password", value:"${localAdminPassword}"}

end tell



The result of the above code will generate a plist file below


<?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>AdditionalUsers</key>

<array/>

<key>Password</key>

<string>${localAdminPassword}</string>

<key>Username</key>

<string>${localAdminUser}</string>

</dict>

</plist>

Posted on Jun 5, 2014 12:05 AM

Reply
2 replies

Jun 5, 2014 3:12 AM in response to JohnAris@

Hello


You need to create elements at correct container. Like this.


set plist_file to (path to desktop)'s POSIX path & "example.plist"
--set plist_file to "~/desktop/example.plist"

tell application "System Events"
    tell (make new property list file with properties {name:plist_file})
        make new property list item at end with properties {kind:string, name:"Username", value:"${localAdminUser}"}
        make new property list item at end with properties {kind:string, name:"Password", value:"${localAdminPassword}"}
        tell (make new property list item at end with properties {kind:list, name:"AdditionalUsers"})
            tell (make new property list item at end with properties {kind:record})
                make new property list item at end with properties {kind:string, name:"Username", value:"${localAdminUser}"}
                make new property list item at end with properties {kind:string, name:"Password", value:"${localAdminPassword}"}
            end tell
        end tell
    end tell
end tell



Or you may create a record in AppleScript and set the value of plist file at once. Like this.


set plist_file to (path to desktop)'s POSIX path & "example.plist"
--set plist_file to "~/desktop/example.plist"

set dict to ¬
    {|Username|:"${localAdminUser}", |Password|:"${localAdminPassword}"} & ¬
    {|AdditionalUsers|:{¬
        {|Username|:"${localAdminUser}", |Password|:"${localAdminPassword}"} ¬
            }}
--set dict to {|Username|:"${localAdminUser}", |Password|:"${localAdminPassword}", |AdditionalUsers|:{{|Username|:"${localAdminUser}", |Password|:"${localAdminPassword}"}}}

tell application "System Events"
    tell (make new property list file with properties {name:plist_file})
        set value to dict
    end tell
end tell



Regards,

H


Message was edited by: Hiroto (PS. Fixed second script so that it uses the original case (uppercase) in key string)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

new to applescript. need to create a plist file using applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.