Developing AppleScriptable XOJO Applications
Developing AppleScriptable XOJO Applications
After building you XOJO application, right click on it and select Show Package Contents. After clicking the disclosure triangle on the Resources folder you should see something similar to the picture with the two red arrows. Put you saved sdef file in the Resources folder. Double click on the file info.plist. This will launch Xcode. If it doesn’t, right click on info.plist and select Open with... and select Xcode. you will get a XML editor window with your info.plist file. if you hover your cursor over the last item in the Information Property List (Minimum system version below) you will see a grey + and - appear where the little up and down triangles are. Click on the +. You should now see a list of property names, scroll down until you see Scripting definition file name and select it. double click in its value area and enter the name of your sdef file. Save this file.
You can also use plistbuddy to modify your info,plist file if you want, check out the man page.
plistbuddy is more suited to do build automation. You might want to add this to your .profile file for easier access if you use plistbuddy:
echo $PATH | grep -q -s "/usr/libexec"
if [ $? -eq 1 ] ; then
PATH=$PATH:/usr/libexec
export PATH
fi
This page is a quick story of how I made my programmable power supply controller AppleScript scriptable. I am not knowledgable enough to call this a tutorial, but you are welcome to use what I have learned. The obvious tools are used for AppleScript and XOJO, AppleScript Editor and the XOJO IDE. I use Sdef Editor from http://www.shadowlab.org to create the scripting definitions file. You will also need Xcode to edit the info.plist file so you can add your sdef file into info.plist.
Below is a diagram that shows the flow of information from a line of AppleScript, through the sdef file, to XOJO and back. The AppleScript command text, OpenSequenceFile, is chosen (red arrow). It must be entered into the scripting definition, sdef, file using Sdef Editor. The actual AppleEvents use four character codes between applications. Both the four character application code and the command code is entered in the sdef file, this allows the sdef file to act as the dictionary of your XOJO application (orange arrow). These four character codes are sent to your AppleEventHandler (yellow and green arrows) within your XOJO application. Your application can pick up a parameter (blue arrow). Lastly, after your function is complete it can return a parameter back to your AppleScript (violet arrow).
Now for some details.
Starting with the XOJO side of things. You need to add a HandleAppleEvents event handler to the APP class.
All code in this event should be enclosed in a
#if TargetMacOS then
...
#endif
since Windows or Linux won’t be too happy with any of this.
This event is passed an AppleEvent (with its properties, and two strings, a class and a ID so you can figure out what AppleScript command got you here.
The eventClass and eventID must matchup with your command definition code and ID in your sdef file (see Sdef Editor below). These codes are unrelated to your creator code. The parameter of the command is extracted from the event with
theEvent.StringParam(“----”)
for a text parameter. Parameters can be of many types including Boolean, Description Lists, Enumerated...
The command returns a parameter in the event, in this example a string using theEvent.ReplyString = ...
I return from the event with
return True
I honestly don’t know what happens if you return False.
The Sdef Editor is how I make a ...sdef file for my scripting definition. You click on the + at the bottom of the Suites list. Name your Suite anything you want, this will appear in the AppleScript Dictionary. Add the code you used in your XOJO event above. Double click on the new suite you just created.
Now click on the disclosure triangle (violet arrow) and highlight the Commands folder. Add all your commands here careful to match the Code and ID with your XOJO code. I do not fill in the Types, Classes, and Events. Save you sdef file.
After building your XOJO application, right click on it and select Show Package Contents. After clicking the disclosure triangle on the Resources folder you should see something similar to the picture with the two red arrows. Put you saved sdef file in the Resources folder. Double click on the file info.plist. This will launch Xcode. If it doesn’t, right click on info.plist and select Open with... and select Xcode. you will get a XML editor window with your info.plist file. if you hover your cursor over the last item in the Information Property List (Minimum system version below) you will see a grey + and - appear where the little up and down triangles were. Click on the +. You should now see a list of property names, scroll down until you see Scripting definition file name and select it. double click in its value area and enter the name of your sdef file. Your file should look similar to above. Save this file.
You should now be able to launch AppleScript Editor, go to File -> Open Dictionary... and click on the Browse button at the bottom, find your application and open its dictionary.
Have fun writing your script for your application.
Visit my other pages: