Global Variables and Properties

I have 2 scripts. One script retrieves some data from Excel and stores it in a variable. Simple enough.

I want to pass that variable on to another script file I have, and perform actions using that variable. Is this possible with Applescript?

I know we can do this with properties:

set property varOne : "some text" 


global x
set x to (load script file <path to the script 1 file.scpt>
return x's varOne

But the data I need to pass between the scripts isn’t going to be constant, and I know you can’t set a property’s value using a variable.

Any suggestions? I know I could save the varOne on a text document and have script2 get the value from that text, but I’m hoping there is a way to do this just using Applescript

I don’t understand what you’re saying. You can set a property’s value from within a script. If the script’s running in its own right (not as part of another script, although there are ways round that), the new value will be saved back into the script file as the value of that property. You’ll still see the original value in the source code when you look at it in AppleScript Editor, but its value internally will be whatever it was the last time the script finished running. Recompiling the script will restore the original value

Your “script1” is incorrect. Properties are declared with an initial value that’s compiled into the script. Thereafter, you change the value at run time the same way as you would with any other variable. Try running this a few times without recompiling:

property varOne : 0

set varOne to varOne + 1
display dialog varOne

Not only are property values saved when a script finishes, but also those of globals and “top level” variables (ie. variables used in the run handler and not explicitly declared local). The effect of this in “script2” will be that when it finishes, the whole of script1 will be saved back into its file as the value of the global variable x. This isn’t a crime, but it may have undesirable bloat effects on script2’s file. One way round this is to set x to “” or something equally brief before script2 finishes.