Sending keystrokes to an application using Shortcuts for macOS Monterey

Posted

Updated

13 Nov 2021

I’ve been trying out Shortcuts on macOS 12 Monterey! I have written about Shortcuts on iOS/iPadOS in the past, so was keen to migrate my automation scripts from Automator or osascript to Shortcuts. Here is the first one: A Shortcut plus global hotkey to an application, leveraging AppleScript/JXA.

This post is the first in a series as I document the new Shortcuts I create for macOS! The next is taking a screenshot and using OCR to extract text and then a review / opinion of my thoughts on Shortcuts so far.

Deprecated! Firstly, WebEx now has a global mute shortcut, and secondly, I have a better example of sending keystrokes to an application in the Shortcut to convert the clipboard content to PNG or JPEG and “Paste As” in an MS Office application.

For iOS/iPadOS, check out old posts from 2017, when it was Shortcuts was Workflow.is e.g. get dictionary definition, add IDD prefix, track travel expenses, contact QR code, get exchange rates, calling Google Maps, join WebEx, create home screen shortcuts, etc.

So, as an example to send a keystroke to WebEx leveraging AppleScript / JXA:

  • if WebEx is running, will focus on the WebEx Meeting window and send the mute keystroke Command+Shift+m
  • if WebEx is not running, the script will launch it and still send the keystroke, which will be rejected (“tink”).

Either in AppleScript:

tell application "Meeting Center" to activate
tell application "System Events"
    keystroke "m" using {command down, shift down}
end 

Or Javascript:

Application('Meeting Center').activate()
Application("System Events").keystroke("m", {using: ['command down', 'shift down']})

So in Shortcuts:

Shortcuts for macOS Details settings for Quick Actions

Shortcuts for macOS Privacy settings

Easy and reliable!

You may be asked to allow siriactionsd in /System/Library/PrivateFrameworks/VoiceShortcuts.framework/Versions/A/Support access to control your computer via System Preferences... > Security & Privacy > Privacy > Accessibility.

macOS Security and Privacy settings for siriactionsd

Updated 6-13Nov 21: Post superceeded! Moved the “IMHO” section to a new post, and have a better use of JXA in the aforementioned post.