Accessing AppleScript

The conventional way to access an AppleScript in Xojo is to drag the script into the Navigator. That script will then be included in the built app and you can run it in your code just by knowing its name.

What I am trying to do is create an application where the user can point to /select AppleScripts so that they can be activated within Xojo. Can you activate an AppleScript from Xojo just knowing its path?

The idea for the application overall would be that the user could select AppleScripts that he/she would like to access at the moment or in the future. The app would take care of cloning the selected AppleScript compiled file and storing it in some folder whose path is known to the application. Then whenever the app is opened that folder could be examined and the names etc. of these previously selected AppleScripts could be stored in some array in the application and activated as needed by the user.

Is this possible? Does someone have advice about the best way to implement this?

I did a little test and saved an AppleScript as an application (myScript.app). I created a folderItem to it in Xojo (f) and then called f.open. The script ran. But this isn’t really what you want.

I found an old article (2003) in Xdex magazine that looks like it addresses this (I don’t have access to the article, though).

http://www.xdevmag.com/browse/1.6/1622/

At the very end of the excerpt, it seems that the author is going to create a script to embed in Xojo that will run external scripts.

I also found this snippet that is supposed to do exactly that:

set myScript to alias ((path to scripts folder as text) & “testScript.scpt”)
run script myScript

That might be worth pursuing.

Thanks Jonathan for chiming in. I worked with your suggestion and eventually got it to work in the simple case of a single script.

I have decided that all the scripts that the application is going to call will be stored in a folder Apple_Scripts

That folder has a known stable path so I can get to the script this way:
(path to home folder as text) & “Library:Application Support:com.bearboat.KM-Palettes:Apple_Scripts:” & “testScript.scpt”

I could get this work.

set myScript to alias ((path to home folder as text) & “Library:Application Support:com.bearboat.KM-Palettes:Apple_Scripts:” & “testScript.scpt”)
run script myScript

myScript will be a script that has been dragged to the Navigator so is readily available to be called by the Xojo app.

Now I need to be able to have the program itself (Xojo) pass a parameter to the myScript so I can specify the various scripts to substitute in place of “testScript.scpt”.

Maybe “OpenBBEdit.scpt” and “CloseWord.scpt” etc.

But how do I pass these strings to the script? I have difficulty understanding passing parameters to AppleScript when the whole thing is being initiated with Xojo.

The Documentation:

Passing Parameters

To pass parameters, add an “on run” handler to contain your script and specify the parameters using curly brackets:

on run {value1, value2} // your script code goes here end run

Xojo Integers passed to AppleScripts are sent as Integer values and are treated as Integers by AppleScript. All other Xojo types (including other numeric types such as Int8 and Double) are sent as Strings and are treated as Strings by AppleScript.


Well, I will work with this but for the moment am too tired to soldier on. It took me a long time to understand that paths in AppleScript use “:” as the separator rather than “/”. The latter leads to an error.

For me, working with AppleScript always swallows vast amount of time as I almost randomly tweak things trying to get some, often very short, script to actually work.

AppleScript has HFS and Posix paths. The former is with the : and the latter is similar to shell paths.

I have a wrapper for the MBS AppleScript at Some Xojo code . The AppleScripts are done in code. Doing AppleScripts from a file with MBS would be very simple.

1 Like

AppleScript is indeed finicky and anything more than a trivial case can take a lot of patience to get working. Luckily, there is a lot of information and myriad examples available, so you can often get what you need by searching. Also, as a general bit of advice, I suggest you look into Script Debugger. It’s much more powerful than Script Editor, and as the name indicates, you can actually debug your scripts. If you want to ever create your own scripts, it’s well worth the price (there is also a Lite version you can try).