Minimal Appleevents Project

Hello

I don’t see an apple events example in the examples that come with Xojo
I want my program to respond to apple events (ie be scriptable)
Surely one could just paste in some code to implement the basic set
I just want get to where my application shows up in the Script Editor dictionary.

TIA

Found something useful:
http://www.rau-deaver.org/Mac-DevForAppleScript.html

Using this information, my app is now showing up in Script Editor Dictionary
However the execution isn’t working, the syntax is unclear.

https://forum.xojo.com/31274-anyone-interested-in-adding-scripting-support-to-their-mac-app/p1#p255833

Thanks Norman. Thomas said he was covered for testers. I am making some progress so will carry on.
BTW…the code at the moment. This is a simpler than on that other page, kind of a “hello world” effort. Next I need to figure out how eventClass and eventID relate to the Dictionary entry. I have assumed the return Boolean indicates whether or not the command was executed.

HandleAppleEvent(theEvent As AppleEvent,eventClass As String,eventID as String) As Boolean

#if TargetMacOS
Dim reply As String = “”
Dim arg As String

Select Case eventClass
Case "execute"             // find out what it is     
  Select Case eventID
  Case "exec"               // handle it
    arg   = theEvent.StringParam("----")
    reply = "hello Applescript! We got " + arg
    Return True
  End Select
End Select

If reply <> "" Then
  theEvent.ReplyString = reply
  Return True
End If

Return False

#endif

Anyone know what the relation is between SDEF editor parameters, and the eventClass and eventID arguments?

OK got it. I had a long period of having Script Editor not able to load the dictionary. I ended up renaming the app. It seems Script Editor may have some hidden info stored about dictionaries that have been loaded. I got a “corrupt dictionary” message once, then it didn’t work. Changing the name of the app fixed it.

In Sdef Editor (see link above)

eventClass = Code field for the Suite … it seems the convention is to use 4 characters eg cmnd
eventID = Code field for the Command, without the eventClass prefix eg exec (ie Code field set to “cmndexec” results in “exec” being passed as eventID)

In the code above, change the name of eventClass to “cmnd” (not “execute”)

The eventClass and eventID are an internal naming system. The command that shows up in the dictionary is the Command Name field in Sdef Editor. I defined the name to be “execute”, with a text argument and a text return value. It shows up in Dictionary like this:

execute?v : Execute a command
execute text : the command to execute
? text : Return value…for testing

I changed one line of code in the above event handler:

MsgBox arg
instead of
reply = "hello Applescript! We got " + arg

Then ran this script in Script Editor:

 tell application "myapp" to execute "hello world!"

Running that script puts up a “hello world!” message box

All seems kind of obvious now, but without the Sdef Editor, I don’t see how this could be done.

It has been said that every app must respond to 6 system events…but I haven’t had any problem with not supporting those.