Plist & Lists

This script stores all preferences as “first/second”. Both are in same Sibling. I dont think this is proper way to store prefs. How can i use Array and create New Sibling for every item in list?

set testthis to {"first", "second"}
do shell script ("defaults write Scripts.Test testthis " & testthis)

set testpref to (do shell script "defaults read Scripts.Test testthis")

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set newTxt to text items of testpref
set AppleScript's text item delimiters to tid
set newTxt to newTxt as list

set prefready to item 2 of newTxt

not really sure if this is what you mean?

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "

set testthis to {"first", "second"}
do shell script ("defaults write Scripts.Test testthis -array " & testthis)

set testpref to (do shell script "defaults read Scripts.Test testthis | sed -e 's/^(//' -e 's/)$//'")

set AppleScript's text item delimiters to ", "
set newTxt to text items of testpref
set AppleScript's text item delimiters to tid
set newTxt to newTxt as list

set prefready to item 2 of newTxt

D.

I wonder what this gibberish does: | sed -e ‘s/^(//’ -e 's/)$//

That helped, but now that i peeked Mail.app plist, i want to save preferences easier way.

My current script have 5 lists and every list has 300 items:

list1 = {“name1”, “name2”…}
list2 = {“email1”, “email2”…}
list3 = {“address1”, “address2”…}
list4 = {“phone1”, “phone2”…}
list5 = {“text1”, “text2”…}

and i want to save and read and compare these easily. I think there is “records?”, which could be used something like this:

set mylist to {name:"Joe", email:"user@host.com", address:"165 XXX", phone:"22929", text:"lkjlkjk"}
email of mylist

But how can i ask “email of Joe”? Especially if i have 300 people in list?

How i read 300 peoples data when script runs and save later on?

wonder no longer!

cheers.

Side note: Nice link, waltr. :cool:

Thanks for the link.

I’m still trying to do that array thing. Never tried arrays before.

It simply removes ‘(’ and ‘)’ from start and end of the resulting output of the defaults read :wink:

I am afraid you’ll have to do a repeat loop search through all list elements - for example:

set mylist to {{nam:"Joe", email:"Joe@work.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Jim", email:"Jim@host.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Joe", email:"Joe@home.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Jane", email:"Jane@aol.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Jeff", email:"Jeff@yahoo.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}}
get my searchListforNam("Joe", mylist)

on searchListforNam(theNam, theList)
	set retval to {}
	repeat with listItem in theList
		if nam of listItem is theNam then copy (email of listItem) to the end of retval
	end repeat
	return retval
end searchListforNam

Btw: I think you should avoid reserved words (‘text’, ‘name’) as your record’s keys.

D.

Now i need to save/read that array also i have normal list which need to be saved/read too.

hi bruce,

if you are in any way responsible for that website, a hearty thank you! i use that one all the time.

hi cirno,

don’t know if this helps you in any way, but i wrote up a quick, ‘how to’ for ‘PlistBuddy’ a while back. you can find it here:

http://bbs.applescript.net/viewtopic.php?id=18380

i personally find PlistBuddy to be an easy way to work with plist files.

cheers.

What ways there is to save/read this to plist?

set mylist to {{nam:"Joe", email:"Joe@work.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Jim", email:"Jim@host.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Joe", email:"Joe@home.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Jane", email:"Jane@aol.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}, {nam:"Jeff", email:"Jeff@yahoo.com", address:"165 XXX", phone:"22929", tex:"lkjlkjk"}}