Reading plist into a table, writing plist

I am trying to insert the name and url from the plist below in a table:


<?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>favoriteservers</key>
	<dict>
		<key>Controller</key>
		<string>CustomListItems</string>
		<key>CustomListItems</key>
		<array>
			<dict>
				<key>Name</key>
				<string>afp://192.168.1.1</string>
				<key>URL</key>
				<string>afp://192.168.1.1</string>
			</dict>
			<dict>
				<key>Name</key>
				<string>afp://192.168.1.2</string>
				<key>URL</key>
				<string>afp://192.168.1.2</string>
			</dict>
	       </array>
	</dict>
</dict>

How would I loop thru the array to access each item in this array? Also, how would I add another dict to that particular array? I’ve been doing some reading but I’m missing something.

The code I’ve got that returns everything within ‘favoriteservers’ is as follows:

set theKey to "favoriteservers" as string
set theData to do shell script "defaults read ~/Library/Preferences/com.apple.sidebartest " & theKey
return theData

If you want to work with property list files in ASObjC, the best method is to convert them to dictionaries. So something like:

set theDict to current application's NSDictionary's dictionaryWithContentsOfFile_(path/to/file)

Then you can parse it using dictionary methods, or coerce it to a record and use AS.

set theDict to current application's NSDictionary's dictionaryWithContentsOfFile_(path/to/file)

This returned a missing value. Do you happen to have an example of how to get to the data using this method?

I did manage to get to one of the pairs of data using shell scripts:

set theServers to do shell script "defaults read ~/Library/Preferences/com.apple.sidebartest \"favoriteservers\" | grep \"URL\" | awk '{print $3}';"

But as you can see, it only returns the URL value instead of the Name AND url values.

set thePath to current application's NSString's stringWithString_("~/Library/Preferences/com.apple.sidebarlists.plist")
set fullPath to thePath's stringByExpandingTildeInPath()
set theDict to current application's NSDictionary's dictionaryWithContentsOfFile_(fullPath)

Thanks for the help, Shane. Always a big help.

I am now getting a returned value which is nice but I still can’t get it in pairs. In my example, I have:


<?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>[b]favoriteservers[/b]</key>
   <dict>
       <key>Controller</key>
       <string>CustomListItems</string>
       <key>CustomListItems</key>
       <array>
		<dict>
		   <key>Name</key>
		   <string>Name of computer 1</string>
		   <key>URL</key>
		   <string>Address of computer 1</string>
		</dict>

		<dict>
		   <key>Name</key>
		   <string>Name of computer 2</string>
		   <key>URL</key>
		   <string>Address of computer 2</string>
		</dict>
...etc, etc.

I’m basically wanting to loop thru each dict under the ‘favoriteservers’ array. My problem is I’m still unable to pair the key and string at the same time so I can put them both in my table at the same time ie

{tblName:dataName,tblAddress:dataAddress}

I’ve used Applescript for quite some time but I’ve never parsed plists before. Seems like a great idea but is really banging me up right now!

The key is the equivalent of the label in an AS record. Depending on what you eventually want to do, you might find it easiest to coerce the result to a record, and parse it as you would any other AS record.

Hi similar problem,

set valemaildata to NSDictionary's dictionaryWithObjects_forKeys_({DisplayDialogController's subgetprops_(me),"",""}, {"displaydialog", "choosefromlist", "choosefile"})
log valemaildata
log (POSIX path of (((path to desktop folder) as alias as string) & "testprops.plist")) as string
log (valemaildata's writeToFile_atomically_((POSIX path of (((path to desktop folder) as alias as string) & "testprops.plist")) as string, true))

the logs are fine but I never see a file appear on the desktop, the documentation says it will write the file the give t a path and then return true if successful, it clearly returns a 1 here.

try this:

log (valemaildata’s writeToFile_atomically_(POSIX path of (path to desktop folder as string) & “testprops.plist”, 1)

Ric

Thanks, why didn’t mine work?

If you look at your log, you’ll see that the “as alias” turned it back into an HFS (colon delimited) path instead of a POSIX path.

Ric

after edit: now I’m not sure-- I cut and pasted your code into an app and it logs just the same as what I posted (with a slash delimited path). I don’t know why your log shows a colon delimited path.

I see, please note I’m not stupid haha :)… and did try all my attempts alone in AS Editor, they all returned strings of posix paths. I feel like I post here a lot, but usually after at least half an hour of effort! :stuck_out_tongue:

Richard :smiley:

also note I’m using pre-release software here

Oh wow, I just found a file actually named “Macintosh HD/Users/Richard/Desktop/testprops.plist” in the debug build folder!

Edit, I tried the application support folder adding an additional path but it returned 0:

log POSIX path of (path to home folder as string) & “Library/Application Support/Richard Birkett/Dialog Maker/Temp/State Log.plist”
log (valemaildata’s writeToFile_atomically_(POSIX path of (path to home folder as string) & “Library/Application Support/Richard Birkett/Dialog Maker/Temp/State Log.plist”, true))

2011-07-15 00:49:32.090 Dialog Maker[2680:707] /Users/Richard/Library/Application Support/Richard Birkett/Dialog Maker/Temp/State Log.plist
2011-07-15 00:49:32.138 Dialog Maker[2680:707] 0

ASE agrees :frowning: