• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Preferences and l10n
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Preferences and l10n


  • Subject: Re: Preferences and l10n
  • From: Shane Stanley <email@hidden>
  • Date: Wed, 20 May 2015 17:41:35 +1000

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.


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Preferences and l10n (From: Jean-Christophe Helary <email@hidden>)
 >Re: Preferences and l10n (From: Shane Stanley <email@hidden>)
 >Re: Preferences and l10n (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Re: Preferences and l10n
  • Next by Date: Text Frame Width in Illustrator
  • Previous by thread: Re: Preferences and l10n
  • Next by thread: Why isn't the Apply Images command in PhotoShop accessible to AppleScript?
  • Index(es):
    • Date
    • Thread