Subroutines and AppleScript Studio


I’m making the leap from programming in REALBasic to AppleScript Studio. It’s weird that you can’t have global variables but I’ve found a way to work around that. But what about subroutines?

In REALBasic these are called methods and can be either subroutines or functions depending on how you want the results returned. Does AppleScript Studio support subroutines? This is kind of related to the lack of global vars in that I am now going to have to do a lot of redundant coding and it’d be nice to just stick some of these into a subroutine and call when needed.



It has the same support that “standard” AppleScript has.

Yes, applescript (both plain and ASStudio) supports the use of subroutines.

For example, to simply execute a subroutine…


to displayMyDialog(theText)
	display dialog ("You sent me the text: " & theText)
end displayMyDialog

Or, to pass some data and get some back…

set theTotal to addSomeNumbers({1, 2, 3, 4})

to addSomeNumbers(theInput)
	set theOutput to 0
	repeat with tmpInput in theInput
		set theOutput to (theOutput + tmpInput)
	end repeat
	return theOutput
end addSomeNumbers


They still have to be in the script (somehow) that is calling them though. :slight_smile:

Thanks for the replies.

Correct me if I’m wrong but those subroutines are regulated to the same script, right? What if I want to call the same subroutine up from different scripts, say scripts on two different control items or windows?

For example in RB I can have Button1 and Button2 call the same subroutine - is this not possible in AS-S

Sure, pretty much anything can call the subroutine… as long as it’s really in the same script. You need to be careful how you word your questions though, because using “scripts” doesn’t seem to appropriately describe the real question you’re asking. It appears as if “handlers” is the word you’re looking for. For example, you could write a subroutine to update a table view’s contents that would be triggered by any number of events in various handlers…

on clicked theObject
	if name of theObject is "UpdateButton" then
	else if name of theObject is "UpdateButton2" then
	end if
end clicked

on choose menu item theObject
	set theMenuItem to name of current menu item of theObject

	if name of theObject is "SomePopupButton" then
		if theMenuItem is "UpdateMenuItem" then
		end if
	end if
end choose menu item

on will open theObject
	if name of theObject is "MainWindow" then
	end if
end on will open

on UpdateTableView()
	tell table view "tableView" of scroll view "scrollView" of window "MainWindow"
		set contents to someList
	end tell
end UpdateTableView

This is a pretty simplistic example, but it does show that all you need to do is make the call from an appropriate place in your code, and the subroutine will be executed. It doesn’t matter how or who calls it, as long as you’re sure that any data you pass to or from the subroutine is compatible with any code that is in the routine or that deals with the value returned.

Now, if you ARE talking about calling code from different scripts… such as in creating libraries or communicating between different applescripts in your project… then the answer may be different. You can load a script into another script and access it’s functions and subroutines, but this can get complicated with large or complex applications. In many cases, you’ll often have one script that handles all of your controls, so sharing a subroutine between handlers of different object classes is not that difficult to do.

Hope that clarifies things a bit,:wink:

Thanks for the reply. Someone was showing me the same thing this morning.

It’s reversed than how you do it in RB but that’s ok.