Really newb question on variables

Can you assign a variable so that it can be read and changed throughout the AS app in different scripts of the app?

Short answer: No.

However, you can utilitize the user defaults to pass data from one script another.

Brad Bumgarner, CTA

Actually, I use this quite a bit. Here is an example from a A.S.Studio application that accesses MySQL data, one script file controls the interface, another supplies the MySQL commands.

I have two script files, “MySQLcode.applescript” and “main.applescript” in my “Scripts” folder in XCode. “main” controls interface functions, “MySQLcode” supplies MySQL functions.

When my application starts up I use the “on awake from nib” handler to load the MySQLcode.applescript file into the main file as a variable like this

on awake from nib theObject
	set myPath to path to me

	set myScriptPath to (myPath as string) & "Contents:Resources:Scripts:MySQLcode.scpt"
	set MySQL to load script file myScriptPath
end awake from nib
  • NOTE: The script file is called “MySQLcode.applescript” in the “Scripts” folder of XCode, but must be loaded as “MySQLcode.scpt”

This sets the variable MySQL to the script file. The MySQL script has the following properties defined at the top, and I set these using the main.applescript file.

property mysql_user : ""
property mysql_host : ""
property mysql_pw : ""
property mysql_db : ""

My handler in “main.applescript” sets each property of the MySQLcode.applescript to the values of text fields that are retrieved from the interface

on setPreferences()
	set thePrefBox to box "MySQLprefs" of window "preferencePane"
	
	tell MySQL
		set its mysql_host to contents of text field "MySQLhost" of thePrefBox
		set its mysql_pw to contents of text field "MySQLpassword" of thePrefBox
		set its mysql_user to contents of text field "MySQLuser" of thePrefBox
		set its mysql_db to contents of text field "MySQLdatabase" of thePrefBox
	end tell
end setPreferences

I then have another method in “main.applesript” that will load these values back into the text fields of the interface if I need to revert them to the values saved in the “MySQLcode.applescript” file

on refreshPreferencePane()
	set thePrefBox to box "MySQLprefs" of window "preferencePane"
	
	tell MySQL
		set contents of text field "MySQLhost" of thePrefBox to its mysql_host
		set contents of text field "MySQLpassword" of thePrefBox to its mysql_pw
		set contents of text field "MySQLuser" of thePrefBox to its mysql_user
		set contents of text field "MySQLdatabase" of thePrefBox to its mysql_db
	end tell
end refreshPreferencePane

Hope this helps, if anything needs clarification just let me know.

jON bEEBE