Writing Arrays in User Defaults

I’ve found it easy to write and read preferences using scripts like the one below. I’ve had no issues, EXCEPT that my app now has a very messy preference file. I’d like some advice on how to group my preferences into Arrays, so that I can separate the preferences into sections within the .plist for my Application. Can someone give me a short example?

set cucache to state of button "cuc" of tab view item "auto" of tab view "tabs" of window "Main"
	if exists default entry "cleanusercache" of user defaults then
		set contents of default entry "cleanusercache" of user defaults to cucache
	else
		make new default entry at end of default entries of user defaults with properties {name:"cleanusercache", contents:cucache}
	end if

Working in something similar, I had the bright idea of using records, as in:


make new default entry at end of default entries of user defaults with properties {name:"WindowPreferences", contents:{MainWindowWidth:200, MainWindowHeight:400, SubwindowWidth:100, SubwindowHeight:300}}

--to read out the values use something like
	set prefRec to contents of default entry "WindowPreferences" of user defaults
	log SubwindowHeight of prefRec

This works fine, but the problem is that so far I’ve only been able to read values, not write them. Examining the preferences file in Property List Editor shows that “WindowPreferences” is inserted as type dictionary and that “MainWindowWidth” etc show up as children underneath. But how to access them for writing?

I’ve been experimenting since I wrote my last reply, and writing arrays and lists to the value of a node does fine in regular .plist files, it just doesn’t work in the default entry variety. Apparantly default entry is a special form of .plist and places restrictions on the input.

Hi Elise,

I think you can write lists to the default entry. I ran this and could get an item from the list:

on awake from nib theObject
if not (exists default entry “test” of user defaults) then
make new default entry at end of default entries of user defaults with properties {name:“test”, contents:“hi”}
end if
tell default entry “test” of user defaults
set contents to {“true”, 1}
set c to contents
set item1 to item 1 of c
set ptext to my getPlainText(item1)
set b to ptext as boolean
log b
end tell
end awake from nib

on getPlainText(unicodeText)
set styledText to unicodeText as string
set styledRecord to styledText as record
return «class ktxt» of styledRecord
end getPlainText

Don’t mind the rest of the script. I just wanted to test the unicode to boolean coercion. Does this work for you?

gl,

I should have wrote the unnecessary part out of the tell block.

on awake from nib theObject
if not (exists default entry “test” of user defaults) then
make new default entry at end of default entries of user defaults with properties {name:“test”, contents:“hi”}
end if
tell default entry “test” of user defaults
set contents to {“false”, 1}
set c to contents
end tell
set item1 to item 1 of c
set ptext to my getPlainText(item1)
set b to ptext as boolean
log b
end awake from nib

on getPlainText(unicodeText)
set styledText to unicodeText as string
set styledRecord to styledText as record
return «class ktxt» of styledRecord
end getPlainText

Rushing too much.

gl,