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:

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

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

	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.