Xojo GUI’s for AppleScript – chapter 2 : Shell scripts

Using Shell Script

These alternative methods of running AppleScripts from RealBasic cover the issue of constructing shell scripts out of REALBasic. It appears that you cannot add a REALBasic variable into the Shell command in real time, so to speak. So the ‘text’ of the Shell command needs to be constructed in advance before the command is sent.

Static starter

As a first pass simply add the fixed filepath to an Applescript file to the code section under the ‘Action’ of the ‘PushButton’ named Run. A simple hardwired solution:

Dim sh As New Shell
sh.execute ("osascript " + "/Users/pauljvallance/Documents/RBPython/OSAScriptRunner/doBeep.scpt")

Dynamic selection

However it’s neater to be able to allow the user, via the interface to ‘Choose the File’ from a Dialog box. You then have to modify the shell command in real time to take account of the user’s input. Problem is, as mentioned above, you can’t just drop a string variable containing the shell path (ffpath = ff.ShellPath) to the chosen file into the shell command.

You can put all the code into a ‘Run’ pushbutton’s action like so:


Dim ff As FolderItem
dim ffpath as String
dim sh As New Shell
dim string1 as string
dim string2 as string
dim commandString as string

string1 = "osascript "

ff=GetOpenFolderItem("")
ffpath = ff.ShellPath

string2 = ffpath
commandString = string1 + string2

sh.execute ( commandString)

commandString just concatenates the osascript command with the pathname of the chosen file

Dynamic selection with feedback to DialogBox

However, as the whole idea is to have a single interface from which to execute (multiple) AppleScripts and also be able to get feedback from their execution in order to modify the future behaviour of this ‘control app’, it makes sense to display the name (& path) of the file that the user chose as well as display the result of the AppleScript’s execution.

To display the chosen file name:

add an EditField to the Dialog box Window


Dim fffile as String

fffile = ff.Name
EditField.text = fffile

To display the chosen filepath & file name:


fffile = ff.ShellPath
EditField.text = fffile

To display and use the data of the returned result of the AppleScript’s execution (assuming your Applescript is set to return something (see below) save the returned shell result into a variable.

add an EditField (called resultField)


dim shellResult as String
shellResult = sh.result
resultField.text = shellResult

the applescript itself needs to specifically return a value:


-- test beep applescript - doBeep.scpt
tell application "Finder"
	beep
	set theResult to "I've beeped"
	return theResult
end tell

The text “I’ve beeped” will return back to the app”

If you place the result of the file selection process into the action of anything other that the button that runs the shell command then you’ll need to to pass the result of the getOpenFolderItem (fffile = ff.ShellPath) over to string2 on the ‘Run’ button in the above example. This is because all variables are by default local and are only in scope within the code of the object/ block that invokes this code. You do this by declaring the variable containing the shell path as a global.

Declare fffile as String as a global property by adding a new module under the project tab, and make sure not to dim the variable, otherwise it gets redimmed as a local and the program won’t work.

For example here is an interface that shows what’s happening:

cmdString is a concatenation of string 1 and string 2. Result is the result returned from AppleScript