Using Xojo to run an AppleScript that runs a KM Macro

Xojo – general purpose app development.

Xojo has the ability to run AppleScript. I can test it by using an AppleScript to open iTunes. That works just fine.

But, in my hands, for some mysterious reason, when I run an AppleScript from Xojo that directs KM to run a macro nothing happens. When I run that same script from the ScriptEditor, it works just fine.

I have even created a composite script that does two things. It opens iTunes and it attempts to fire off a KM macro. When I run it from the ScriptEditor, it works just fine. Both things happen. When I run it from Xojo, the only effect that I see is that iTunes opens.

tell application "iTunes"

launch

end tell

tell application "Keyboard Maestro Engine"

do script "GarbageSample"

end tell

Has anybody tried to use Xojo to initiate a Keyboard Maestro script using AppleScript?

Thanks for sharing. Unfortunately I can offer no help.
I investigated using Xojo for about a month, because it is so easy to create some nice, powerful UIs. My ultimate goal was to do exactly what you are trying. :wink:

One thought, since you can trigger a KM Macro using URL Scheme, maybe you could just use Xojo to open that URL.

Please let us know what you learn about using Xojo with KM.

Well, I am glad that someone else was curious about the same possibility. I find it odd that Xojo is "happy" to run AppleScripts and I can, as I said, successfully run an AppleScript to open an application but I cannot seem to run an AppleScript from Xojo that initiates a Keyboard Maestro macro.

Was it the same issue that stopped your own exploration of Xojo?

Xojo does not support URL schemes from what I can understand so that does not seem to be an alternative.

There is one trick that I have employed that actually has helped me in some of my personal work. Keyboard Maestro has an oddball trigger, Clipboard Changed Trigger. This monitors the System Clipboard and when that changes, the script runs. Xojo has a command to place text on the System Clipboard. So you can set up an agreed-upon convention that allows Xojo to fire off a Keyboard Maestro script.

I put something like @@012 on the Clipboard using Xojo. Keyboard Maestro detects this because the Clipboard has changed. Of course, the Clipboard is changing all the time. But KM looks to see if the text on the Clipboard starts with @@ and only proceeds if it does. KM can then looks at what follows the @@ (012 in my example) and put that into a Switch/Case and then run the agreed-upon macro or action that 012 specifies.

It feels very kludgy. And under some circumstances, I can imagine that there might be some important stuff on the Clipboard that you do not want to be displaced. I have even gone to the effort of restoring the previous Clipboard contents using Set Clipboard to Past Clipboard, but I was not sure that worked all the time. The Switch statement can seem a little unwieldy and difficult to maintain if it has 30 or more choices, As I say, I could make it work for myself knowing all the innards, but it was difficult tor recommend to others. I wanted a more "general" solution.

When I realized that Xojo could call AppleScript and that KM scripts could be initiated with AppleScript I was briefly delighted. I thought I was home-free, but then I ran into this bizarre and to me inexplicable "problem".

Not exactly. I never got that for in my testing, but I finally concluded that I would not be able to easily use Xojo as an UI window called from either KM or an AppleScript. As you probably know, Xojo has to be an app to be used like this, and I didn't want to go that far, or pay the expense of creating Xojo apps (at least not yet).

I believe it has an open url method, which should work.

Yep. Have you posted this question on the Xojo forum?

I have. I like the forum there just fine. People can be very helpful.. But this is kind of a tweener problem. Nobody has any answers. Some suggest getting various plug-in packages, but I am not a professional programmer and wanted only to offer a "free" app to friends and make my life easier.

There is a current of "concern" about AppleScript being neglected and hamstrung by Apple and the latest OS'es and problems with the AppStore and tools that use AppleScript. So people respond about these "big" topics. That fills up the posts. None of this is helpful to me trying to get over what seems like such a small hump.

The Xojo interface to Apple Scripts is very easy to use. As I say, I could get the AppleScript to open iTunes to run without any difficulty. The peculiar problem is why doesn't the AppleScript to fire off a KM macro work? When that script, called from ScriptEditor, works just fine.

I have gotten no speculation on this particular issue. It is such a niche thing that it may be simply out of the realm of the experiences of the people on that forum.

Meanwhile, I will look again at Open URL & Xojo and see if I can find anything.

The kludgy Clipboard Change trigger lets me personally craft custom solutions that come up in my own work, but I wish I had a cleaner strategy.

My own approach was going to be to use Xojo as the UI to call KM (and AppleScript). The reverse direction that you speak of.

In many ways, primarily the ease of creating a nice simple GUI, Xojo is very suited to this. And if there is some "problem" you have a full blown language to code around it. In this scenario, KM is sort of in the background, offering its unique skills in terms of interacting with one or more other programs and "operating them", but is basically out of sight. And as the user, once you have set this up, you do not have to remember a bunch of hot keys or deal with the somewhat primitive graphics of a palette.

I have heard of URL schemes but never used them.

There is a command in Xojo

ShowURL(). If you put in: ShowURL(https://www.xojo.com) the works and the browser shows the home page for Xojo.

If I manually put in the URL bar of Safari
kmtrigger://macro=Sample2
then it works although annoying that it asks for permission every time.

However, at first I thought that

Show URL("kmtrigger://macro=Sample2") did nothing. Or at first it did not seem to do anything.

But perhaps I mistyped something. Being totally unfamiliar with URL schemes, I tried many variations.
ShowURL("https://kmtrigger://macro=Sample2")
ShowURL("https://kmtrigger//macro=Sample2")
ShowURL("kmtrigger//macro=Sample2")

None of those worked.

Eventually, I went back to
ShowURL("kmtrigger://macro=Sample2")

Then it seems to just work (and did not ask for permission).

Obviously, I have more testing to do but possibly this is a crack in the door. I will report back after I have had a chance to do more work etc.

While I am on the topic, KM offers other choices (like kmtrigger://macro=6E986FA2-FEF1-488E-B778-70BE06570BAD). Is there any reason to prefer the UUID over just using the name?

I've owned Xojo for several years so this discussion caught my eye. I hasten to add I am NOT a Xojo expert. It just so happened that a few years ago I had to develop a database app and Xojo was the easiest approach to use.

I've tried to do what you did and discovered that the Xojo log itself reveals the cause of the problem.

Here's the screenshot:

You can access the log by clicking the icon at the bottom (circled in red).

As to why the script is not authorised to send Apple events to KM is another matter. The Xojo Applescripts documentation talks about Apple Events on Mohave and later. Reading through I tried this: in the app produced by building the Xojo project, I edited the info.plist file and added a line for NSAppleEventsUsageDescription. You can see this in the screenshot below:

Now, when I run the app (the one whose info.plist I just edited) it executes the KM macro as intended.

I wanted to post this asap so you can see all is not lost with Xojo but I've yet to figure out how to get Xojo to put the need extra permission into the info.plist file at build time, rather than having to do it manually. If I get time I'll have a look later.

In the meantime, stay safe!

3 Likes

So, replying to my own post, this is how to get Xojo to automatically give permission to apps to use Applescripting...

As I said, Xojo needs to add some extra information to the info.plist file it creates in the app bundle when you build your application. This is documented but simply, you just have to create your own info.plist file with the extra information in it and tell Xojo to add that to the one it builds by default.

To save you the trouble, here is a zipped version of that file: Info.plist.zip (992 Bytes) and it contains this:

CleanShot 2020-09-29 at 11.37.45
All you need do is then unzip this file and drag the result into you Xojo project, like this:

It appears as info in the Xojo navigator.

When you run your compiled and built app, it will popup a dialog telling you that the app wants to control KM to which you click OK. When you've done that, your app will appear in the Automation section of the Privacy system preferences, like this:

Obviously, the apps you build with Xojo will not necessarily be called My Application.app

I hope this helps. If I've left some gaps in the explanation, or you find out other things, keep the discussion going!

3 Likes

Outstanding detective work, @tiffle! :+1:

Many thanks for sharing this and the complete solution.

1 Like

Wow!!!
It works!. This has made me so happy. Your extensive explanation, dumbed down to a level that I could understand and act on, is a marvel. I really appreciate your going out of your way to construct your answer.

Did you use DeterminePermissionToAutomateTarget method in NSAppleScriptMBS class to query status?

Asking the same question in the Xojo forum elicited the above response from someone kind enough to try and be helpful, but I could understand perhaps only every other word.

Exploring this topic had led me to a lot of mentions of NSAppleScriptMBS and plists. iBut I did not have the background to put these words together.

Over the next couple weeks, I will be trying to build out my project. I will see if I run into any additional problems or roadblocks. I am very nervous about upgrading to Catalina or Big Sur because I have heard so many complaints about how Apple has been "neglecting" AppleScript and introducing more and more security features that interfere with its use. So I will stay with Mojave for the moment.

If things do smoothly for a day or two, do I have your permissions to encapsulate what you have told me here and close out the thread I started in the Xojo forum? I would credit the fact that I had gotten the information from you here.

And if I get the project completed and working satisfactorily, I will put a post script on this thread here. I am trying to create a sort of a generic GUI interface to KM that can be used to smooth out certain use case scenarios, and, if I am successful, it might be of interest to denizens of this forum.

Thanks for sharing. I would definitely be interested in your generic GUI interface to KM.
I need to revisit my work with Xojo.

I'm very glad I could help. I'm all for clarity :grinning:

Of course, use the information as you wish.

Well, if you get that far then I'd be really interested.

Praise indeed from the master - thanks!

Using tiffle's suggestions, I was able to accomplish what I wanted which was to use an AppleScript imbedded in Xojo to fire off a Keyboard Maestro script.

The screenshot below shows the navigation bar with the file that tiffle supplied (Info2) and the AppleScript (AardvarkPrepare). Both have been dragged to this location.

AardvarkPrepare is very simple.

At this point, all works pretty well. It is cool that in Xojo you can just write a line of code

AardvarkPrepare

in, for example, the action event of a button and the AaarvarkPrepare.scpt in the navigation bar of the same name will run. It will even auto-complete when you are typing that line of code. And this particular AppleScript will launch the Keyboard Maestro macro.

201011AaardvarkPrepareScript

__

For the record, when I attempted a more complex Keyboard Maestro, I ran into a problem that stumped me for a considerable time. The Keyboard Maestro macro that I was running bounced around various apps (one of the magic powers of Keyboard Maestro) and then returned to the Xojo app to get some additional information. The whole process was initiated by a menu event handler in Xojo. When the logic of the macro brought it back to Xojo a second menu event handler in Xojo was used to keep the chain of Keyboard Maestro commands going.

This did not work. I am not sure exactly why, but when I tried debugging the situation it was apparent that, while nothing crashed, I did not end up with the desired outcome. If I launched the same Keyboard Maestro macro using a hotkey trigger in Keyboard Maestro then things worked just fine.

For whatever reason, the menu event handlers in Xojo never really completed until after all the Keyboard Maestro steps had been run. In some sense, it was suspended. So if I wrote some code to time-stamp and log the ending (last line of code) of the menu event handlers those lines of code did not happen until after everything in Keyboard Maestro had completed.

To deal with this situation, I changed tactics slightly. I did not imbed the AppleScript script in the navigation bar of Xojo. Rather I saved the AppleScript as an app rather than the default complied script. Xojo was informed of the path of this app on the file system of the computer.

Var NAME_KM_MACRO As String // The simple AppleScript app that initiates KM script
NAME_KM_MACRO = "AardvarkPrepare.app"

Var f As folderitem = SpecialFolder.Desktop.Child(NAME_KM_MACRO)
If f <> Nil And f.exists Then
f.open
End

So the Keyboard Maestro macro was fired off by opening the app (AardvarkPrepare.app). That overcame the issue. It involves having to know where in the file system the app lives and making sure that the location corresponded to the Xojo code but it works. (In the example I put the code on the Desktop)

So in this special circumstance, I use the

f.open

technique but usually I can just put the AppleScript title in the navigation bar and access it from there.

1 Like

I have created an application that allows the user to create 2D palettes to launch Keyboard Maestro macros.

This should be considered beta software. I have just released it on 02/15/21 and, to date, I am the only user and tester.

If anyone gives this software a try, I would appreciate some sort of feedback even if only a few words.
You have to paste this URL into your browser.

http://www.bearboat.net/KM_Palettes/KM_Palette.html

1 Like

Hi @rlivingston, you have been busy! I’ll take a look a bit later when I have some spare time.

(BTW - the link to your website doesn’t respond to clicks - you need to edit and correct that...)

I noticed that it did not respond to clicks so I added the sentence about pasting the URL into the browser. I basically could not figure out WHY it did not respond to clicks. I tried again and I think I got it to work.

Yep - it works fine now!