Applescript for modifying key value pairs in a plist file.

Hi All,

I am very new for apple scripts and unaware of the complexitiy of Applescript.Can any body help me to write a script to modify a plist content.I want to change values corresponding to their keys in the plist. Is there is a simple way to modify the plist if it exists in system. I am able to write the script up to here…

set someFile to ((path to "pref" as string) & "com.apple.myApp.plist")
tell application "Finder"
	if exists someFile then
		--Need your help to complete
		--Need to edit the myApp.plist and set values corresponding to keys "cal" and "addressbook"
	else
		display dialog "File not exists"
	end if
end tell

These key value pairs are already existing in the plist file.

Thanks for any reply!

Manish.

Hi Manish,

here an example using the defaults command of the shell

set myPlist to "com.apple.myApp"
set someFile to ((path to preferences as Unicode text) & myPlist & ".plist")
try
	someFile as alias
	set mycal to do shell script "defaults read " & myPlist & " cal"
	set myaddressbook to do shell script "defaults read " & myPlist & " addressbook"
	-- edit values to calValue &  addressbookValue
	do shell script "defaults write " & myPlist & " cal " & calValue
	do shell script "defaults write " & myPlist & " addressbook " & addressbookValue
	-- if a Value is an integer, use this instead
	do shell script "defaults write " & myPlist & " cal -int " & calValue
	do shell script "defaults write " & myPlist & " addressbook -int " & addressbookValue
on error
	display dialog "File not exists"
end try

For more information, read the man page of defaults
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/defaults.1.html

Hi StefanK,

Thanks for your help.This script is really help ful for me to solve the problem.:slight_smile:

Regards,
Manish.

Fairly new at the world of applescript and editing plist files so… I use a program to edit RAW image files and it saves the settings I use as .plist files. One thing I don’t like about the program is that when I save an image it adds an icon indicating it has been completed. There is no way to reset that icon in the program to it’s default (blank) state. I can however edit the plist file to remove the entry. I want to be able to do this in a batch format. Any files dragged onto the icon get the field value deleted and then resaved to it’s original location. The field is called ProcessedList and it is an Array class. If a file is “processed” it creates an entry with a ProcessedDate (string) and a ProcessedURL (string).

Can someone point me in the right direction. Even if someone can start it I may be able to fill in the details to make it work.

Any chance you can send me to cop of the plist? One that has the entry in there… the other with the entry stripped out?

How do I attach/send the files?

You got mail.

Here’s my way around the AppleScript/PLIST communication problem. I am writing an AppleScript Studio application, and I needed a good, efficient way to set all sorts of properties. I created two subroutines, placed at the bottom of my script (outside the “on run” block); one to set properties, and one to read them.

to setPreferences about theKey at theType for theValue
	do shell script ("defaults write " & myDomain & " " & (the quoted form of theKey) & " -" & theType & " " & (the quoted form of theValue) as text)
end setPreferences

and

to readPreferences about theKey
	try
		set theValue to (do shell script ("defaults read " & myDomain & " " & (the quoted form of theKey) as text)) as text
	on error
		set theValue to "" as text
	end try
	return theValue
end readPreferences

Of course, since I was only writing properties for myself, I created a global property at the very beginning of my script containing the Domain of my app:

property myDomain : "com.c8.VERB"

Note that this must be done outside of “on run,” “to setPreferences,” or any other blocks. With a fairly small reworking, you could change that to any application you need, and properties are changeable during runtime just like any other variable.

There is a built-in error catcher when reading preferences: if you attempt to read a property that does not exist, you will simply get an empty string “” back.

With this set up, it is really simple to set preferences:

setPreferences of me about "defaultRemoteHost" at "string" for "hal.local"
-- equivalent to: do shell script "defaults write com.c8.VERB defaultRemoteHost -string 'hal.local'"

.where at “string” identifies the type of the value: string, data, int, float, bool, date, array, array-add, dict, or dict-add.

It is equally easy to read values:

set theLocation to readPreferences of me about "defaultRemoteHost"
-- returns "hal.local"

Hello,
I am super new when it comes to creating an applescript.

I’m trying to create a few applescripts that writes to the Info plist.
My main purpose for this is to simply automate the writing to the Server Address string assignment with out having to open up the terminal and type out the following:

(user)$ defaults write /Applications/(NameofApp)\Universal.app/Contents/Info ServerAddress “aaaa.aaaa.com

Since we are constantly having to quickly switch from one ServerAddress to another while doing some group coordinated testing; I figured the quickest way to make sure that we are all on the same page before we execute our testing is to simply click on the apple scripts and tada! We are all pointing to the server address of choice with out the hassle of typing stuff out.
This is the Key and String i’m writing to in the app’s Info.plist

ServerAddress
aaaa.aaaa.com

It would be great to also to get an output for “defaults read” as well so we can confirm that the script succeeded in changing the plist.

Anyones help with this would be greatly appreciated!

It should help - http://www.j-schell.de/node/316 .

If you’re happy to accept a little third-party help:

set theFile to (path to preferences as text) & "com.apple.LaunchServices.plist"
tell application "ASObjC Runner" to set theData to read plist at theFile
   --> list or record