save-load AS-Studio prefs

These two handlers, loadPrefs & savePrefs, will load and save given preferences to the default app plist file.
loadPrefs will return a list of loaded prefs. You must pass it a list of {key,value} preferences. If it doesn’t exist such preference (as given in the key), it will be created with the default value you provide.
savePrefs will also accept a list of {key,value}. If it does exist an entry called “key”, it will update its contents. Otherwise, it will create a new entry with the key-value you provided.

A must-have for your AS-Studio project!

OS version: OS X

--> thesePrefs is a list of key-value-list
--> eg: {{"key1",value1},{"key2",value2}}

savePrefs({{"pref1",5},{"pref2","sample string"},{"pref3",{1,2,3}}})

set {pref1, pref2} to loadPrefs({{"pref1",5},{"pref2","sample string"}})

to loadPrefs(thesePrefs)
	set loadedPrefs to {}
	repeat with i in thesePrefs
		set iName to i's item 1 as text
		if default entry iName of user defaults exists then
			set end of loadedPrefs to contents of default entry iName of user defaults
		else
			set defaultValue to (i's item 2)
			set end of loadedPrefs to defaultValue
			make new default entry at end of every default entry of user defaults with properties {name:iName, contents:defaultValue}
		end if
	end repeat
	return loadedPrefs
end loadPrefs
to savePrefs(thesePrefs)
	repeat with i in thesePrefs
		set entryName to contents of (i's item 1)
		set entryValue to contents of (i's item 2)
		try
			set contents of default entry entryName of user defaults to entryValue
		on error
			make new default entry at end of every default entry of user defaults with properties {name:entryName, contents:entryValue}
		end try
	end repeat
end savePrefs