Applescript changing value on plist

applescriptplist

I have a script which is passing values to a plist, it's work fine, but I would like to change some of the values without removing everything else.
I used to have a script for that but can't found it at all.

Here is my script which creating the value

tell application "System Events"
    set the parent_dictionary to make new property list item with properties {kind:record}
    set the plistfile_path to "~/Desktop/MY_DATA.plist"
    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:"thename", value:theName}
    make new property list item at end of property list items of contents of this_plistfile ¬
        with properties {kind:string, name:"creationDate", value:creationDate}
    make new property list item at end of property list items of contents of this_plistfile ¬
        with properties {kind:string, name:"serialNumber", value:serialNumber}
    make new property list item at end of property list items of contents of this_plistfile ¬
        with properties {kind:string, name:"Email", value:fullEmail}
    make new property list item at end of property list items of contents of this_plistfile ¬
        with properties {kind:string, name:"IPAddress", value:IPAddress}
end tell

here is what I wrote for changing the value, but the issue is the plist will only have the new value and all the other properties would be removed

set theName to "demo"

tell application "System Events"
    set the parent_dictionary to make new property list item with properties {kind:record}
    set the plistfile_path to "~/Desktop/MY_DATA.plist"
    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:"thename", value:theName}

end tell

how can I change some of the value without removing the others ?

Best Answer

Firstly, you can simplify your initial script—the one used to create the property list file—to this:

    set plistR to {theName:"name", creationDate:"date", serialNumber:"serial #", fullEmail:"@", IPAddress:"1.2.3.4"}

    tell application "System Events"
        set plistf to make new property list file ¬
            with properties {name:"~/Desktop/MY_DATA.plist"}

        set plistf's value to plistR
    end tell

Then changing a value is as simple as changing an item in the record plistR, and setting plistf's value to the new plistR:

    set plistR2 to {theName:"name2", creationDate:"date", serialNumber:"serial #", fullEmail:"@", IPAddress:"1.2.3.4"}

    tell application "System Events"
        set plistf to the property list file "~/Desktop/MY_DATA.plist"
        set plistf's value to plistR2
    end tell

If you don't want the hassle of writing out a whole new record declaration (imagine you had, say, 1000 items and only needed to change one), you can adjust a single plist item also like this:

    tell application "System Events"
        set plistf to property list file "~/Desktop/MY_DATA.plist"
        set the value of the property list item "theName" of plistf to "name3"
    end tell