Friday, July 21, 2017

#1 2006-02-28 07:53:43 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Binding Your Preferences in AppleScript Studio (Mac OS X 10.3+)

Do you want your AppleScript Studio app to easily remember it's preferences? If so, then you may find this information useful.

First, read this page from Apple: Binding Your Preferences in Cocoa.

This should give you a basic understanding of how to use Interface Builder to setup Bindings on an individual item. The most important part right now is the "Model Key Path" text field; the other options will come later.

While we're talking about Interface Builder... It helps to know to how to setup a preferences window. Did you can show a window without using code by using connections? You'll see that mentioned in the above article. (You can also watch this video: ib_window_connection.mov)

Also, you'll need to know how to set your application's identifier to something unique; This is also covered in the above article. (Identifier examples: "com.apple.Safari" or "org.mozilla.firefox")

Once you've setup the "Model Key Path" for your items, continue on to the next section...


Xcode: Required AppleScript

If you want your script(s) to read your preferences, then you'll need to add some code.

You should read some documentation about the User Defaults system, if you haven't already:
user-defaults
default entry

The documentation states:

Attempt to make new default entries for all preferences before attempting to retrieve the user's current settings from the defaults system. You make a default entry with a statement like the following:

Applescript:

make new default entry at end of default entries of user defaults with properties {name:"defaultName", contents:"Testing"}

... A good place to perform this step is in a will finish launching event handler connected to your application object. The application object is represented in Interface Builder by the File's Owner instance in the Instances pane of the MainMenu.nib window.


To clarify, you should add something like this to your "will finish launching" handler:

Applescript:

on will finish launching theObject
   try
       tell user defaults
           make new default entry at end of default entries with properties {name:"myTextField", content:"default answer"}
           make new default entry at end of default entries with properties {name:"myCheckbox", content:false}
       end tell
   end try
end will finish launching

You'll need to make a new entry for each different name you used in the "Model Key Path" text field earlier. (Multiple items can be bound to the same value.)

Hopefully, this will get started with Bindings. If you have a question that is not covered by this thread, please make a post in our AppleScript Studio forum.

Reading Default Entries

Applescript:

   tell user defaults
       set someText to content of default entry "myTextField"
       set someCheck to content of default entry "myCheckbox"
   end tell

Other Options/Miscellaneous Info

Color Wells: When binding a color well's value, you must set the Value Transformer to NSUnarchiveFromData.

Text Fields: When using bindings, you can put placeholder text in the Null Placeholder field.

Last edited by Bruce Phillips (2007-07-10 10:12:17 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)