Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Vanilla Face

macrumors 6502
Original poster
Aug 11, 2013
471
150
Hi there. I'm trying to learn Applescript and have very basic knowledge of other languages. So far its pretty easy to use. I'm having trouble declaring a variable in another script. The scripts are a little long to post here, but this is essentially what I want to do:

Script 1
-Declare variable
-run script2
-change variable
-run script2

Script 2
-manipulate variable

Script 2 works perfectly if I just put the variable in myself, but I want to change the variable and run script 2 using script 1. Any help would be greatly appreciated.
 
Last edited by a moderator:
To start with, I'll explain how to do the same thing within a single script, as the concepts are very similar and it simplifies the explanation a bit. Also, if the reason you have two different scripts is just to avoid code duplication, then this approach is likely sufficient.

If you haven't encountered them already, it's worth reading about handlers (also known as functions, subroutines or methods).

You can provide a parameter to a handler as follows (this example will display a dialog saying "1", then another saying "2"):

Code:
set myVar to 1
testMethod(myVar)
set myVar to 2
testMethod(myVar)

on testMethod(parameter)
	display dialog (parameter)
end testMethod

If you want to modify the value of the parameter in the handler, you can return a new value and use that returned value in the original code (there are other ways, but they're a bit more complicated - see "Passing by Reference Versus Passing by Value" in the link above). For example:

Code:
set myVar to 1
set myVar to testMethod(myVar)
set myVar to testMethod(myVar)

on testMethod(parameter)
	display dialog (parameter)
	return parameter + 1
end testMethod

Doing this across multiple scripts is very similar. When a script is run, AppleScript checks for a special handler named "run". When no run handler is provided, any code left outside a handler is implicitly used as the run handler. By explicitly providing a run handler, however, you can specify a parameter list that lets you provide parameter values from other scripts. You can also return values in the same way as above.

Here's an example that's the same as the second example above, but across two scripts. Script 1:

Code:
set newVal to 1
set script2 to (path to desktop as text) & "Script2.scpt"
set newVal to (run script (file script2) with parameters (newVal))
set newVal to (run script (file script2) with parameters (newVal))

Script 2:

Code:
on run (param)
	display dialog param
	return param + 1
end run

As above, if you only need to read the parameter and not modify it in script 2, you can omit the "return" line and ignore the return value in script 1.
 
That did it! I didn't know you could create methods within applescript. Thank you so much!
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.