Include global declarations and initializations.

Hi there.

I have a bunch of scripts that use the same set of global variables. Instead of copying and pasting them into the individual scripts, is there a way to make a seperate file with all the global declaraions and initializations and then include them into all my scripts?

This would be convenient if I had to change anything in the declarations as well.

here’s an example of the stuff I would be declaring:

global RightPanel
set RightPanel to {-142, 224}
set DevTemp to {-61, 314}
set DevTint to {-62, 332}
global DevYCoords
set DevYCoords to {313, 331, 381, 402, 420, 437, 467, 485, 533, 551, 569}

Thanks in advance!

sam

Hi.

Your separate file can be a script with the “globals” declared as properties:

property RightPanel : {-142, 224}
property DevYCoords : {313, 331, 381, 402, 420, 437, 467, 485, 533, 551, 569}

-- etc.

You can then load the script into a global variable in the client scripts:

global g

set g to (load script file "path:to:above:script")

get g's DevYCoords
--> {313, 331, 381, 402, 420, 437, 467, 485, 533, 551, 569}

Thanks so much Nigel.

One more question… Is there an elegant way to set the path to my declarations script to the same folder that my main script is in?

I’m currently using:

	tell application "Finder"
		set mypath to container of (path to me) as text
	end tell
	set MyDeclarationsPath to (mypath & "Declarations.app")
	set MyProperties to (load script file MyDeclarationsPath)

Sam.

Sorry, Sam. I didn’t notice there was another post in this thread.

I don’t know about elegant. A couple of slightly faster methods are to use text item delimiters to remove “my” name from the path and substitute the other file name .

-- Text item delimiters.
-- With a delimiter of ":", get (text 1 thru text item -2 of "path:to:me") & ":file name"
-- or (text 1 thru text item -3 of "path:to:me:") & ":file name"
--> "path:to:filename"

set myPath to (path to me as text)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set myDeclarationsPath to text 1 thru text item (((myPath does not end with ":") as integer) - 3) of myPath & ":Declarations.app"
set AppleScript's text item delimiters to astid
load script file myDeclarationsPath

. or to use a relative path:

-- HFS relative path.
-- Get "path:to:me::file name"
--> Interpreted by the filing system as "path:to:file name"

set myPath to (path to me as text)
set myDeclarationsPath to myPath & text (1 + ((myPath ends with ":") as integer)) of "::Declarations.app"
load script file myDeclarationsPath

The loaded script doesn’t have to be an application, by the way. An ordinary compiled script’s fine.