Executing AppleScript from Keyboard Commander

By PaulMartz, 30 March, 2024

Forum
macOS and Mac Apps

I'm looking for tips or a guide on how to execute an AppleScript from Keyboard Commander. I've got the script working. I can execute it from the Menus Extra script menu. It works fine. But I want to associate it with a keyboard shortcut.

I could create an Automator Workflow to execute the script, then create a shortcut to execute the workflow in Settings, Keyboard, Shortcuts. I've done that in the past, back when I had enough low vision to decipher the cryptic Automator user interface. Now that I'm totally blind, I'm not sure I'm up to this approach.

That leaves Keyboard Commander, which sort of works, but not consistently. The first time I press the key combo, it executes and works as expected. But after one successful execution, it will no longer work. It does something, but I haven't been able to figure out what. It's certainly not the same correct behavior I get from the first keypress. Note there's nothing wrong with the script. I can execute it multiple times from the Menus Extra Scripts menu. So the problem must be something in Keyboard Commander.

The script is executing key commands in Google Docs, and I get the same type of failure whether I'm using Docs in Chrome or Safari. It's tempting to say this is some kind of incompatibility between Docs and Keyboard Commander, but it works the first time. And if I close the Doc and reopen it, it works the first time again.

So, what am I looking for with this post. Good question.

I guess I'm looking for other people who might have experienced something similar while trying to execute an Applescript from Keyboard Commander, and what you eventually did as a workaround.

Barring that, I'm also looking for guidance on using Automator Workflow totally blind. I found very little with an AppleVis site search. Any pointers or tips would be appreciated, because I suspect this will end up being my solution.

Options

Comments

By Kevin Shaw on Tuesday, April 2, 2024 - 19:26

VoiceOver Utility;
Commanders;
Keyboard Commander;
Activate Add;
Type the shortcut key;
From the associated menu: Custom Commands>Run AppleScript script;
Select the script.

By Brian on Tuesday, April 2, 2024 - 19:26

If your script simulates pressing of keys, or a combination of keys, this could also be why the script only works occasionally. Just something to consider.

By PaulMartz on Tuesday, April 2, 2024 - 19:26

Thanks, Brian. Yes, it does emulate keypresses. It uses the key code syntax and includes some modifier keys. If you want to see the script, it's in this blog. See the section where I describe AppleScripts to move to and read the next and previous comment.

But, remember: It executes correctly and repeatedly when I launch the script from the Script menu. It's sending key codes in both cases, whether I launch it from the Script menu or from Keyboard Commander. So why do repeated executions via Keyboard Commander fail? It's a bit of a head-scratcher.

By Brian on Tuesday, April 2, 2024 - 19:26

Currently, I am running macOS 12.7.4, on my old MacBook Pro. Did a bit of downgrading, since getting my new Ryzen-based PC for school. Anyways, if you don't mind, I would like to look at the script on my MacBook, and see how it does on an older version of macOS. Because, honestly, I'm thinking it's Sonoma that's giving you the headache.

I'm sure I will get a lot of grief for this, but Sonoma is horrible.

By PaulMartz on Tuesday, April 2, 2024 - 19:26

Cool, Brian. Thanks. Follow the link I provided, find and copy the script, and paste it into Script Editor. The version you'll find is hardcoded for Safari, but you can change that to Google Chrome on the first line. I get the same behavior from either browser.

Before trying to execute the script, you'll need to have a document open in Google Docs. Assuming your document has no comments, a correct execution of the script should state, "not on a comment". When the script fails, you'll hear some audible noise, and VoiceOver will maybe announce a word or letter.

If you have a document with comments, repeated successful executions should move you, in turn, to each successive comment, and read the comment text as you go. This is the behavior I'm after, a single keypress to move to and read the next comment. Back when I wrote that blog, the only way I could get it to work was by executing the script from an Automater workflow.

Was that too much information? Are you sorry you signed up for this already? LOL.

By Brian on Tuesday, April 2, 2024 - 19:26

Though it has been a while since I have done anything with Google docs. Though I am up for the challenge. Give me a couple of days, as I've got some other things going on, but I will get back to you.

Count on it.

By Brian on Tuesday, April 2, 2024 - 19:26

Decided to play with this a little. First and foremost, I absolutely hate Google Docs on a Mac.

Hate, it.

however, I made a very simple new test document and threw in a few comments just to test things out.

I had to allow permissions for 2 items; Script Editor and Scrod in order to allow the script to function. However, while I did not get any errors as you mentioned above, I also did not get my comments read back to me.

A couple things to note here.

1. While I am on Monterey, I am using the latest Safari (v17.4.1).
2. I am wondering if there are some changes between Safari on my version of macOS vs your version? Because again, no errors, but also no actual result either.

I will play around more with it after the Easter holiday, and let you know if I figure something out.

Stay tuned. . .

By PaulMartz on Tuesday, April 2, 2024 - 19:26

Thanks Brian. Yeah, Google Drive and Docs are a disaster in Safari. It all works fine in Chrome, though, which is what got me moving on this project.

Did you try running the script from the script menu in menu extras? You have to enable that menu. To do that, open script editor, Preferences, and select the checkbox to show the script menu in the menubar. Scripts saved into ~/Library/Scripts can be executed from that menu.

Have a great weekend.

By Brian on Tuesday, April 2, 2024 - 19:26

No, I did not enable the script in the mini extras. I will give that a try, and let you know how that goes. I did, however, add a shortcut via keyboard commander.

I also did not use Google Chrome, I will also give that a try as well, and let you know my findings. For now, I'm going to recover from eating way too much food because today, was rabbit day! 🐰

By Nuno on Tuesday, April 2, 2024 - 19:26

Hey Paul,
the solution is actually pretty simple but not so obvious. Is your script placed in a folder indexed by iCloud Drive? If yes, move it to an entirely local folder. The reason for that is VoiceOver cannot for some reason request a file that was previo`usly offloaded from iClou drive and menu extra can.

By PaulMartz on Tuesday, April 2, 2024 - 19:26

Brian, much thanks for even taking a look at this.

I had a couple of hours free this afternoon, so I managed to get this working from an Automator quick action, and believe me, this was a real challenge. That UI is not friendly at all.

But the nice thing is that I left the Safari-specific shortcuts in place and tied the Chrome-specific shortcuts to different keys. So, depending on which browser seems to work best with Google Docs on any given day, I can invoke the appropriate scripts.

For fans of the old B&W Addams Family series, picture Uncle Fester with his head in a vice.