Reputation: 699
I setup an Applescript quick action in Automator to leave a zoom meeting. No matter what application is in the forefront, I would like to check if zoom is running and if so, leave the meeting.
set appName to "Zoom.us"
if application appName is running then
tell application id (id of application appName)
activate
end tell
tell application "System Events"
keystroke "w" using command down
keystroke tab
keystroke return
end tell
end if
It works! The problem is, if I'm in a zoom meeting and another app is in the forefront, I have to get permission from system preferences for that app to access system preferences. Like if I'm in Chrome, I have to allow Chrome to send keystrokes. Then, Chrome will always work.
I have to do this for every single possible app. Is there a way to get keystrokes in there without going thru this security stuff in Big Sur? I don't mind bringing up zoom to the forefront.
Upvotes: 7
Views: 12692
Reputation: 7555
There are issues at play here with using global keyboard shortcuts with Automator workflows saved as a Service/Quick Action.
To workaround the accessibility privileges issue, here are three methods to achieve the goal that come to mind.
The first, my preferred method for running AppleScript scripts with a keyboard shortcut, is to use a third-party application named FastScripts, as it would not need to have every application that's frontmost, that hasn't yet been granted privileges, to be granted accessibility privileges to run the AppleScript code shown in your question. I'd imagine other similar type third-party applications that allowed assigning keyboard shortcuts and running a script would bypass the issue too, but have only tested the aforementioned.
The second method, can be done with Automator as a Service/Quick Action using a Run Shell Script action, then assigned a keyboard shortcut and will work without having to give accessibility privileges to the application that is frontmost when the keyboard shortcut is pressed.
The third method, can be done with Automator as a Service/Quick Action using a Run AppleScript action if changing a zoom.us default preference under zoom.us > Preferences… > General by unchecking [] Ask me to confirm when I leave a meeting, then when assigned a keyboard shortcut and will work without having to give accessibility privileges to the application that is frontmost when the keyboard shortcut is pressed.
All testing was done under macOS Big Sur using zoom.us, (Version: 5.4.7 (59780.1220)) with my Language & Region setting in System Preferences set to US English using the various methods presented.
Method 1
The first method using the following example AppleScript code, show further below, and FastScripts with the keyboard shortcut ⌃⌥⌘W assigned and works for me as coded.
In System Preferences > Security & Privacy > Privacy > Accessibility I had the following added and checked:
Then with zoom.us running and several other applications which were frontmost when the keyboard shortcut was pressed I did not have to grant accessibility privileges to those other applications, zoom.us was brought to the front and closed.
Example AppleScript code:
if application "zoom.us" is running then
tell application "zoom.us" to activate
delay 0.5
tell application "System Events" to ¬
tell application process "zoom.us"
keystroke "w" using command down
delay 0.5
key code 36
end tell
end if
Note for testing purposes, after testing Method 1, I quit FastScripts as it would have been triggered by the same keyboard shortcut which was assigned when testing the next two methods.
FastScripts can be run as a free app, up to 10 keyboard shortcuts, or upgraded for $24.95 USD to unlock unlimited keyboard shortcuts. I have no affiliation with Red Sweater Software, LLC, other then as a user of FastScripts.
Method 2
The second method was tested using Automator and a Service/Quick Action with setting Workflow receives [no input] in [any application] using a Run Shell Script action with its default settings, and the following example shell script code is all that was used:
[[ -z $(pgrep -x 'zoom.us') ]] || pkill −x 'zoom.us'
In System Preferences > Keyboard > Shortcuts > Services I assigned it the keyboard shortcut: ⌃⌥⌘W
Then with zoom.us running and several other applications which were frontmost when the keyboard shortcut was pressed I did not have to grant accessibility privileges to those other applications, zoom.us was closed.
Method 3
The third method was tested using Automator and a Service/Quick Action with setting Workflow receives [no input] in [any application] using a Run AppleScript action replacing its default code with just the following example AppleScript code:
tell application "zoom.us" to quit
In System Preferences > Keyboard > Shortcuts > Services I assigned it the keyboard shortcut ⌃⌥⌘W after removing it from the Service/Quick Action created in Method 2.
Then with zoom.us running and several other applications which were frontmost when the keyboard shortcut was pressed I did not have to grant give accessibility privileges to those other applications, zoom.us was closed.
This of course works because the [] Ask me to confirm when I leave a meeting preference was unchecked in the preferences for zoom.us.
To recap, if you do not mind changing the mentioned default preference in zoom.us, then Method 3 is probably the easiest and best way to resolve your issue as it allows for a graceful quit over Method 2, does not require any third-party application, and does not require any accessibility privileges be granted or zoom.us being frontmost, it just works.
I mentioned the other methods first as method one addresses the UI Scripting issue with Automator and a Service/Quick Action, and method two works with the default settings in zoom.us.
Upvotes: 7