On 20 May 2015, at 12:32 pm, Jean-Christophe Helary <email@hidden> wrote:
I just want to have the user enter values in the various dialogs and those values would be remembered as defaults for the next launch.
FWIW, here's a script library you can use to store values in a property list in ~/Library/Preferences. Just remember the limitations of what can be stored in a .plist file. Save this as a .scptd file in ~/Library/Script Libraries:
use scripting additions use framework "Foundation"
script forInheritance -- required for inheritance end script
on makePrefsWithIdentifier:scriptId factoryValues:initialValues script property parent : forInheritance
property factoryDefaults : missing value property scriptDefaults : missing value property scriptIdentifier : missing value
on retrievePrefs() set storedDefaults to (current application's NSUserDefaults's standardUserDefaults()'s persistentDomainForName:scriptIdentifier) if storedDefaults is missing value then copy factoryDefaults to scriptDefaults else set my scriptDefaults to (storedDefaults as record) & factoryDefaults end if end retrievePrefs
on storePrefs() current application's NSUserDefaults's standardUserDefaults()'s setPersistentDomain:scriptDefaults forName:scriptIdentifier end storePrefs
on resetPrefs() current application's NSUserDefaults's standardUserDefaults()'s removePersistentDomainForName:scriptIdentifier end resetPrefs
end script set ASPrefs to result copy initialValues to ASPrefs's factoryDefaults set ASPrefs's scriptIdentifier to scriptId return ASPrefs end makePrefsWithIdentifier:factoryValues:
Then in your applets, you can use it like this. First, get a prefs object using the default values you want used the first time the all is run:
-- put default values in a record set myFactoryValues to {counter:0} -- whatever you want to store tell script "<name of your lib>" to set thePrefs to its makePrefsWithIdentifier:"com.yourbusiness.lib.applet.User-defaults" factoryValues:myFactoryValues -- use an ID related to the applet, but not exactly the same
Then you can read the prefs using:
tell thePrefs to retrievePrefs()
And you can access and modify the values like this:
tell thePrefs to set counter of its scriptDefaults to (counter of its scriptDefaults) + 1 set theResult to (display dialog "Counter is " & (counter of scriptDefaults of thePrefs) buttons {"Reset", "OK"})
And you can store them using storePrefs() or reset them using resetPrefs(). For example:
if button returned of theResult = "Reset" then tell thePrefs to resetPrefs() else tell thePrefs to storePrefs() end if
If you want the apps to be portable to other users, just add the script library to your applet's bundle inside /Contents/Resources/Script Libraries. |