A Variable Question…

Hi All…

I have a lot of scripts for our workflows which use the same variables and paths to servers, the scripts are starting to get really big and slow, and i’ve being trying to trim them down and make them more efficient…

I was wondering if it’s possible to have a separate script that defines all the variables and then all the other scripts can link to this main script and use which ever variable(s) they need…??

Is this just wishful thinking? :smiley:


Why would that be faster?

Not faster, just more efficient and easier to manage…
We use filemaker, and our workflows are heavily reliant on filemaker and applescript scripts, i would ideally like to have this central ‘variable’ script, so it will be easier to manage, if we change a server address, i will have to amend a large amount of our scripts - if all the scripts would link to one, i could just amend the one server address or file path etc.

One way to do it is to create a library script consisting entirely of handlers:

on ServerGGL()
	return "http://www.google.com"
end ServerGGL
-- etc.

And then load this script into each of the workflows that requires these addresses:

--load the library
set scriptPath to (path to scripts folder from user domain) & "URLFile:" -- a folder to hold your libraries
set tURLs to load script ((scriptPath as text) & "myURLs.scpt") as alias
set Google to ServerGGL()


Another way is to create a script server. You save it as a stay open application and other scripts query its properties and subroutines. Here’s an example I just whipped up so it might have bugs and ness error checking added for like the class of parameters.

property the_db : {}
property the_keys : {}
on run
	display dialog "Select an option:" buttons ¬
		{"Add/Change Value", "Delete Value", "Continue"} default button "Continue" giving up after 10
	set b to button returned of result
	if b is "Add/Change Value" then
		display dialog "Enter new or existing key:" default answer "key"
		set the_key to text returned of result
		display dialog "Enter value:" default answer "\"value\""
		set text_value to text returned of result
		set the_value to run script text_value
		set r to AddChangeValue(the_key, the_value)
		beep 3
	else if b is "Delete Value" then
		display dialog "Enter key to delete:" default answer "key"
		set the_key to text returned of result
		set r to DeleteKey(the_key)
		set r to "continuing"
	end if
	return r
end run
on reopen
end reopen
-- changes value of existing key or adds new key/value
-- returns  index of the key
on AddChangeValue(the_key, the_value)
	if the_key is in the_keys then
		set key_index to SearchKey(the_key)
		set item key_index of the_db to the_value
		set end of the_keys to the_key
		set end of the_db to the_value
		set key_index to count the_keys
	end if
	return key_index
end AddChangeValue
-- returns index of a key that exists
on SearchKey(the_key)
	set i to 0
	repeat with this_key in the_keys
		set i to i + 1
		if the_key is (contents of this_key) then
			exit repeat
		end if
	end repeat
	return i
end SearchKey
on DeleteKey(the_key)
	return "handler not implemented yet"
end DeleteKey
on DoMoreStuff(the_key)
	return "handler not implemented yet"
end DoMoreStuff

When you run it, it allows you to add or change data. You can also add/change values or get values with another script. Something like this would run it:

tell application "ScriptServer1"
	return {the_keys of it, the_db of it}
end tell

or you can make your own script and use its handlers by placing in tell block.

tell application “ScriptServer1”
end tell

For more info on script servers, see:


Edited: almost forgot this. You can easily get more speed if needed by changing the type of searching method instead of the simple repeat loop.


Hi Guys,

Thank you both for the suggestions, very much appreciated!!!..

I’ve been trying with Adam’s suggestion all morning, but all i get is this message " <> doesn’t understand the ServerGGL message."

Have i done something wrong?? No matter what i do it doesn’t run… :frowning:

I’ve tried to mount a server with it but i get the same message…

Here’s the Library Script…

on mount_Jobs()
	return "server address"
end mount_Jobs

and the other one…

--load the library
set scriptPath to ("Macintosh HD:Users:kyle.jones:Desktop:") & "URLFile:" -- a folder to hold your libraries
set tURLs to load script ((scriptPath as text) & "myURLs.scpt") as alias

set x to mount_Jobs()
	mount volume x
end try

My bad — :rolleyes:

set x to tUrls's mount_jobs()

Fantastic!!! It works!! :smiley:

Thanks alot, i’l go and test kel’s way and see how they both can be merged into our workflow…

Thanks guys…

There’s another rather simple approach. Prepare a text file of your addresses using the first script, and distribute the text file.
Users would access it’s contents using the second script:

-- Prepare a text file thus:
set tPath to ((path to desktop as text) & "url")
set R to open for access tPath with write permission
	set eof of R to 0
	write {Ggl:"http://www.google.com", M:"http://macscripter.net"} to R as record
	close access R
on error
	close access R
end try
--- end of first script

To get the data back:

--- Then in any other script:
set tPath to ((path to desktop as text) & "url")
set U to read alias tPath as record
set G to U's Ggl
set M to U's M

Still another approach is to write your data as a “Addresses.plist” file to be put into each users ~/Library/Preferences folder. In the example above, I’d have had them put the text file in their ~/Library/Scripts/ folder so I could use:

((path to Scripts folder from user domain) & “whatever” as alias to read it

Just another quick question regarding handlers…

The route you guys suggested for my library script is fantastic and works well… and i wish i had investigated handlers earlier…


if i try to mount a server which is already mounted, with a handler from another script i get this confusing message… The Variable x is not defined

here’s the handler script

on mount_Server()
		mount volume "ServerX"
end try
end mount_Server

here’s the script that links to it

set scriptPath to folder "Documents" of home
	set tURLs to load script ((scriptPath as text) & ".Library.scpt") as alias
	set x to tURLs's mount_Server()
end tell

What i would like to know is, even if i have a try in my handler i still get the error above and why does my script believe that the variable x has not been defined although i have?

Thank you for your help

mount_Server() errors if ServerX is already online, so it returns nothing since there is no “on error” in your try block. Back in the calling script, therefore, set x to tURL’s mount_Server() has no value.

To give it one,

on mount_Server()
		mount volume "ServerX"
	on error
		-- return "whatever x should be"
	end try
end mount_Server