Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Code samples for an AppleScript preferences system' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Code samples for an AppleScript preferences system
Authored by: roxeteer on Apr 06, '05 10:43:53AM
I think you're making it overly difficult... You can write a preference to a file in Terminal with
defaults write com.ShullianProductions.preftester 'prefName' 'prefValue'
You can read a named preference with
defaults read com.ShullianProductions.preftester 'prefName'
Then you can execute these commands in AppleScript with
do shell script "defaults write com.ShullianProductions.preftester 'prefName' 'prefValue'"


[ Reply to This | # ]
Code samples for an AppleScript preferences system
Authored by: aamann on Apr 06, '05 12:14:31PM
The defaults command requires the BSD subsystem to be installed or it will fail - for AppleScript Studio applciations you should use the AppleScript Version of theis command (which is part of the AppleScript.framework and thus always present):
default entry [of ...]
If you use of user defaults, there is no need to add a prefs name, it will automatically use the application's bundle identifier.
Here are some handlers I use for reading/writing prefs (the reading one allows you to set a default value in case the key is not present - if no value is present in the prefs file, a new key with the default value will be added. That way you are sure to always get something back...):

on readDefaultEntry(theKey, defaultValue)
  try
    set theResult to contents of default entry theKey of user defaults
  on error
    make new default entry at end of default entries of user defaults with properties {name:theKey, contents:defaultValue}
    set theResult to defaultValue
  end try
  return theResult
end readDefaultEntry

on writeDefaultEntry(theKey, theValue)
  set contents of default entry theKey of user defaults to theValue
end writeDefaultEntry


[ Reply to This | # ]