preferences window in applescript

I have an applescript xcode app, and I would like to incorporate a preferences window where I can have certain global variables affected-- Does anyone have any advice on how I can accomplish this so that the preference variables are written to the .plist file? Or is this beyond the scope of Applescript?

-patrick

It is quite possible to do this. In fact, I’ve been meaning to write an article on this. Also, if you have OS X 10.3 or later, you can use Binding to make this easier. For the time being, I would encourage you to read about the user-defaults class.

Thanks for the reply…

Ok, so I’m a little stuck–

I have created a preference window titled “preferences” with a checkbox button called “distributionbutton”

my code is this:

on will finish launching theObject
	set visible of window "preferences" to false
	global distribution
	make new default entry at end of default entries of user defaults with properties {name:"distributionlist"}
	set distribution to contents of default entry "distributionlist" of user defaults
	if distribution = "" then
		set distribution to "off"
	        call method "synchronize" of object user defaults
	end if
end will finish launching

on clicked theObject
		if (state of button "distributionbutton" of window "preferences" as boolean) then
                    set distribution to "on"
                else
                    set distribution to "off"
                end if
	        call method "synchronize" of object user defaults
end clicked

However, I get an error everytime saying that “variable distribution is undefined”… Which I don’t get…

what am I doing wrong here?

Try putting “global distribution” at the top of the script.

global distribution

on will finish launching theObject

You might want to look into:

/Developer/Examples/AppleScript Studio/Archive Maker/

And see how it does it’s preferences as well. Excellent example imo.

I figured out its because that property had no value-- Once I set it to something, then I no longer got that error.

Arg… No… That’s not true… I still get the error outside of my handlers… When I run the script the dialog window appears and shows that dist = “on” … which is correct… But, as soon as I click preferences it says “Variable Dist is not defined”!!! I tried putting the global outside of the handler, but it doesn’t work either… I even tried copying the whole ‘set dist to contents of default entry “distributionlist” of user defaults"’ into my choose menu handler, but then if I close my preferences window and reopen it I get “NSReceiverEvaluationScriptError: 4 (1)”

WHAT IS THE DEAL???

This is my actual code now:

on will finish launching theObject
	global dist
	set visible of window "preferences" to false
	make new default entry at end of default entries of user defaults with properties {name:"distributionlist"}
	--set contents of default entry "distributionlist" of user defaults to "on"
		set dist to contents of default entry "distributionlist" of user defaults
	display dialog dist
end will finish launching

on choose menu item theObject
	if title of theObject is "Preferences" then
		set visible of window "preferences" to true
		if dist = "off" then
			set (state of button "distributionlist" of window "preferences") to false
		else if dist = "on" then
			set (state of button "distributionlist" of window "preferences") to true
		end if
	end if
end choose menu item

on clicked theObject
	if title of theObject is "distributionlist" then
		set dist to contents of default entry "distributionlist" of user defaults
		if (state of button "distributionlist" of window "preferences" as boolean) then
			set dist to "on"
		else
			set dist to "off"
		end if
		call method "synchronize" of object user defaults
	end if
end clicked

I have no idea how to use bindings but here’s how I do it:

property distribution : true

on will finish launching theObject
	make new default entry at end of default entries of user defaults with properties {name:"distribution button", contents:distribution}
	set distribution to contents of default entry "distribution button" of user defaults
	set content of button "distributionbutton"of window "preferences" to distribution
end will finish launching

on clicked theObject
	set distribution to content of theObject
end clicked

on will quit theObject
	set contents of default entry "distribution button" of user defaults to distribution
end will quit

Reading the guidelines, this certainly does not look like the recommended way.

Instead of using the line set visible of window “preferences” to false just untick the box “Visible at launch time” in Interface Builder for that window.

Yeah global doesn’t work for me in Xcode. I have to use property.