Mac Applescript Edit plist File

bastien_icpt

macrumors newbie
Original poster
Mar 29, 2017
2
0
i need an applescript to create that plist on desktop that a part of my project to automate image creation thanks for the futur answer :)

Plist file to create with Applescript
<?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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.apple.iCalToDesktop</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Volumes/Fake mac/ABCD.app</string>
</array>
</dict>
</plist>

i found a exemple but i don't managed to use it
Applescript to create plist file
  1. tell application "System Events"
  2. -- Create an empty property list dictionary item
  3. set theParentDictionary to make new property list item with properties {kind:record}
  4. -- Create a new property list file using the empty dictionary list item as contents
  5. set thePropertyListFilePath to "~/Desktop/Example.plist"
  6. set thePropertyListFile to make new property list file with properties {contents:theParentDictionary, name:thePropertyListFilePath}
  7. -- Add a Boolean key
  8. tell property list items of thePropertyListFile
  9. make new property list item at end with properties {kind:boolean, name:"booleanKey", value:true}
  10. -- Add a date key
  11. make new property list item at end with properties {kind:date, name:"dateKey", value:current date}
  12. -- Add a list key
  13. make new property list item at end with properties {kind:list, name:"listKey"}
  14. -- Add a number key
  15. make new property list item at end with properties {kind:number, name:"numberKey", value:5}
  16. -- Add a record/dictionary key
  17. make new property list item at end with properties {kind:record, name:"recordKey"}
  18. -- Add a string key
  19. make new property list item at end with properties {kind:string, name:"stringKey", value:"string value"}
  20. end tell
  21. end tell

File create
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>booleanKey</key>
  6. <true/>
  7. <key>dateKey</key>
  8. <date>2016-01-28T19:34:13Z</date>
  9. <key>listKey</key>
  10. <array/>
  11. <key>numberKey</key>
  12. <integer>5</integer>
  13. <key>recordKey</key>
  14. <dict/>
  15. <key>stringKey</key>
  16. <string>string value</string>
  17. </dict>
  18. </plist>
 

Mark FX

macrumors regular
Nov 18, 2011
124
11
West Sussex, UK
I'm Not sure wether your trying to create the first posted plist file example, or the last posted example.
But here's an example of what you should be doing with the System Events property list file commands.

Code:
use scripting additions

set plistFilePosixPath to (POSIX path of (path to desktop folder) as text) & "Example.plist" as text

tell application id "com.apple.systemevents"

    set plistParentRecord to make new property list item with properties {kind:record} as record

    set propertyListFile to make new property list file with properties {contents:plistParentRecord, name:plistFilePosixPath}

    tell property list items of propertyListFile

        make new property list item at end with properties {kind:boolean, name:"boolKey", value:true}

        make new property list item at end with properties {kind:string, name:"stringKey", value:"com.apple.itunes"}

        make new property list item at end with properties {kind:number, name:"numberKey", value:10.0}

        make new property list item at end with properties {kind:date, name:"dateKey", value:(current date)}

        make new property list item at end with properties {kind:list, name:"listKey", value:{1, 2, 3, 4}}

        make new property list item at end with properties {kind:record, name:"recordKey", value:{firstname:"John", surname:"Doe", age:40}}

    end tell

end tell
Copy and paste the above code into a new Script Editor file, and click the run button.
And this should create a new plist file on your Desktop with the example items in it.

You can change the example code to set the kind of data type you want to store, and change the name of the keys to whatever label you want to use like this.

Code:
make new property list item at end with properties {kind:boolean, name:"RunAtLoad", value:false}
You can also open and read the plist file back into an AppleScript, or change or edit any of the key value pairs contained within it from AppleScript code.

Here's a link to Apple's own examples.

https://developer.apple.com/library...ScriptingGuide/WorkwithPropertyListFiles.html

A better way of creating plist files is with AppleScriptObjC code, but this can only be used in an Cocoa AppleScript bundle or Script Library, or an Xcode AppleScript Project, and not with a plain vanilla AppleScript script file.

Regards Mark
 
Last edited:

Red Menace

macrumors 6502
May 29, 2011
440
78
Fruita, Colorado, USA
A better way of creating plist files is with AppleScriptObjC code, but this can only be used in an Cocoa AppleScript bundle or Script Library, or an Xcode AppleScript Project, and not with a plain vanilla AppleScript script file.
Actually, OS X Yosemite v10.10 and later allow using Objective-C frameworks from any script - you declare the desired framework(s) with the use statement.
 

bastien_icpt

macrumors newbie
Original poster
Mar 29, 2017
2
0
Thanks for the both quickly and usefull answer with your help i have almost the result i need to write plistTrue with an Applescript , thanks for the futur answer .

I need that :
plistTrue
<?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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.apple.iCalToDesktop</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Volumes/Fake mac/ABCD.app</string>
</array>
</dict>
</plist>


Now i have that i need to up RunAtLoad
plistFalse

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.apple.iCalToDesktop</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Volumes/Fake mac/ABCD.app</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

AppleScript False
use
scripting additions


set plistFilePosixPath to (POSIX path of (path to desktop folder) as text) & "Examples.plist" as text


tell application id "com.apple.systemevents"


set plistParentRecord to make new property list item with properties {kind:record} as record


set propertyListFile to make new property list file with properties {contents:plistParentRecord, name:plistFilePosixPath}


tell property list items of propertyListFile




make new property list item at end with properties {kind:boolean, name:"RunAtLoad", value:true}





make new property list item at end with properties {kind:list, name:"Label", value:"com.apple.iCalToDesktop"}





make new property list item at end with properties {kind:list, name:"ProgramArguments", value:{"osascript", "/Volumes/Fake mac/ABCD.app"}}



end tell


end tell
 

Mark FX

macrumors regular
Nov 18, 2011
124
11
West Sussex, UK
@ Red Menace
Your correct that any AppleScriptObjC code can be used in any script as of OSX 10.10, but you cannot save ObjectiveC class references in a plain AppleScript script file.
Copy and Paste the example code below into a new plain Script Editor file, then try to save it as a plain script file, in my OSX 10.12 system you cannot.

Code:
use scripting additions
use framework "Foundation"
use framework "AppleScriptObjC"

set theASList to {"A", "B", "C", "D"} as list

set theNSArray to current application's NSArray's alloc()'s initWithArray:theASList
As far as I'm aware, you can only save AppleScriptObjC code in a Cocoa AppleScript bundle, or a Script Library, or AppleScript Xcode project.
Which is not a problem, more of an observation that I noticed when working with plain vanilla AppleScript's.

Most of my own projects are done with Xcode these days, but the original poster appears to be using a plain script file.

@ bastien_icpt
Sorry, but I don't understand what further question your asking.
Please be more clear about how we can help you further.

But another approach worth considering when creating your plist file, is to build your record ahead of saving it like the example below, this technique makes the code cleaner and more readable.

Code:
use scripting additions

set appSettingsPlistFilePath to (POSIX path of (path to desktop folder) as text) & "appSettings.plist" as text

set appSettingsRecord to {RunAtLoad:true, LastRunDate:(current date), LastUserName:"Mark", VolumeLevel:7, HighScores:{23500, 34780, 35675, 36550}} as record

tell application id "com.apple.systemevents"
   set appSettingsPlistRecord to make new property list item with properties {kind:record, name:"Root", value:appSettingsRecord} as record
   set appSettingsPlistFile to make new property list file with properties {contents:appSettingsPlistRecord, name:appSettingsPlistFilePath}
end tell

Regards Mark
 
Last edited:

Red Menace

macrumors 6502
May 29, 2011
440
78
Fruita, Colorado, USA
As far as I'm aware, you can only save AppleScriptObjC code in a Cocoa AppleScript bundle, or a Script Library, or AppleScript Xcode project.
I've never had an issue saving regular, single file scripts using AppleScriptObjC with the Script Editor. I don't know if something changed in Sierra, but I can save your example script just fine in El Capitan 10.11.6.
 

Mark FX

macrumors regular
Nov 18, 2011
124
11
West Sussex, UK
@ Red Menace

Yeah it must be a Sierra thing.
I cant say when this restriction was introduced, but AppleScriptObjC code can only be saved in a bundle on my Sierra Mac.

For sure writing plist files is much easier with NSDictionary and NSArray, but as bastien_icpt post seems to be a plain script file, I did'nt see any point in posting a Cocoa solution with this file saving restriction.

Mark

[doublepost=1490981628][/doublepost]Just an update for Red Menace and anyone else who might be interested.

after a bit of experimenting, you can save ObjectiveC references in plain AppleScripts in Script Editor on Sierra.
But it seems you have to compile them first, by going to the main menu Script -> Compile first.
Then you can go to the main menu File -> Save item.

Not sure when this behaviour was introduced, just goes to show how much I use Script Editor these days.

Hope this helps Sierra users.

Mark
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.