Sending keystrokes in Applescript via Automator in any application - applescript

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.

There are issues at play here with using global keyboard shortcuts with Automator workflows saved as a Service/Quick Action.
The keyboard shortcut assigned to the Service/Quick Action needs to not conflict with a default keyboard shortcut for whichever application is frontmost at the time it's pressed, otherwise there may be unwanted behavior.
Every application that is frontmost when the keyboard shortcut is pressed on a Service/Quick Action using UI Scripting in the Run AppleScript action will need to have accessibility privileges granted for it (as you've already found this out and hence the question).
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:
FastScripts
System Events
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.

Related

Automator permissions error when running AppleScript in Safari – Alternatives and security risks?

I want to use Automator and AppleScript to fill out a form in Safari. I have a functioning script that looks something like this toy example:
on run {}
tell application "System Events" to keystroke "Hello"
tell application "System Events" to keystroke tab
tell application "System Events" to keystroke "World"
end run
I wish to execute it with a keyboard shortcut when I'm at a specific point in my browser. However, when I do this, I run into permission issues:
The action "Run AppleSCript" encountered an error:
"System Events got an error: Automator Workflow Runner
(WorkflowServiceRunner, my_script_name) is not allowed to send keystrokes"
In System Prefrences -> Security & Privacy -> Privacy -> Accessibility I have allowed both Automator and AppleScript Utility, and under the Automation tab, I have allowed System Events for Safari.
I can get it to work, at least temporary, if I allow Safari in Accessibility too, but that seems to be too drastic and a security risk to have Safari to always have full control just to run a script from time to time.
How should I approach this? I want the script to be easy to use, and wouldn't want to go into the settings each time to temporary allow Safari. Or can this be automated too? Is there a completely different approach than Automator and AppleScript that would allow me to do this in a more user friendly and safe way? Basically, I want to do something similar to what AutoHotKey would be able to do in Windows.
I’m guessing that what you’ve done is set up a Quick Action and then given that Quick Action a shortcut under “Services” in “Keyboard:Shortcuts” in System Preferences. I was able to duplicate the problem by doing that: attempting to run the script requires that Safari itself have access to “control your computer”. If that is not what you’ve done, this solution should still work, but you may also want to update your question, in case a better solution is available.
What you can do is use redirection. Create an application that can be given permission to control your computer, and then create a Service that will launch the application.
Use Automator (or Script Editor) to create an Application with the above steps, adding as the first step tell application "Safari" to activate. You need that step because opening the app will take the focus away from Safari.
Save this Application. (If using Automator, you tell Automator that it’s an application when you start creating the script; if using Script Editor, you tell Script Editor that it’s an application when you save the script.)
Use Automator to create a Quick Action. Give it the action “Launch Application” and tell it to launch the application you just created.
Drag the application you created in step 2 into the “Accessibility” list in System Preferences’s “Security & Privacy” settings. (You may have to unlock those settings to allow changes.) It should be checked automatically when you drag it in, but if not you can check it.
In the “Services” list in the “Keyboard:Shortcuts” settings of System Preferences, the Quick Action you created in step 3 should be listed. Give it a keyboard shortcut.
At this point, you should be able to use the keyboard shortcut to run the application, and only that application needs to have permission to control your computer.

Launch/raise application and cycle through its windows using AppleScript and keyboard shortcut

How can I use AppleScript to create a keyboard shortcut which will:
Launch an application if it's not running.
Bring the application to the foreground if it is running.
Cycle through only the application's windows if I keep pressing the shortcut keys.
For example, when I press Option-Y, it will launch Terminal or bring it to the foreground, and if I keep pressing Option-Y it will cycle through the multiple open Terminal windows if I have more than one open.
I have successfully created an AppleScript using Automator as a service. I have successfully mapped this script to a keyboard shortcut by going to System Preferences, Keyboard, Shortcuts, Services and under General, checking and assigning a keyboard shortcut to the Automator service BringTerminalToTheForeground.
Here is the script to launch the Terminal:
tell application "Terminal"
activate
end tell
I want to do this only with the tools which come with the Mac by default (e.g. AppleScript, Automator). I'm running El Capitan.
Command-"`" will cycle through an application's windows.
tell application "Terminal" -- try other applications. This script might work in other cases. It happens to work with Terminal but I haven't tested it.
activate
set c to count of windows -- we need to know how many windows the application has so we only cycle through all of them once.
tell application "System Events"
repeat c times
keystroke "`" using command down -- this is what cycles through the windows. This doesn't work in every application.
delay 1 -- this is a 1-second delay. Change this to whatever delay you want.
end repeat
end tell
end tell
I have exactly what you are asking for, but I'm using hammerspoon, Karabiner Elements and GokuRakuJoudo (optional but highly recommended).
First you have to create this lua module using Hammerspoon https://github.com/fabiomcosta/keyboard/blob/73b22906b31dfc12cc834c382515e30c4450d0f5/hammerspoon/hyper.lua#L76 then on my karabiner.edn (using Goku) I define a keymapping calling that function using Hammespoon's cli hs.
It's not easy to setup and in fact that's why I'm looking for a new way to achieve the same behavior without this much setup haha.
But it definitely works.

Allow JavaScript from Apple Events in Safari through Terminal Mac

I'm writing a program that executes do javascript in Safari. The only problem is that I'm trying to make the app give its self permission to do it. I'm trying to locate the file that handles the Safari developer preferences so that I can do this. Does anyone have any idea where this might be or how to change these settings?
It's in Safari's preferences plist at ~/Library/Preferences/com.apple.Safari.plist. The key you want is AllowJavaScriptFromAppleEvents. You can set it using defaults:
#to turn it on
defaults write -app Safari AllowJavaScriptFromAppleEvents 1
#to turn it off
defaults write -app Safari AllowJavaScriptFromAppleEvents 0
The virtual keyboard thing did not work for me. As StarPlayr at apple's develepoer forum has found out the problem is in something else.
For me problem occurred when i tried to do that on remote mac.
For some people plugging in a keyboard and mouse to the Server allowed to turn on JavaScript Apple Events in Safari and set the password.
However, for me that wasn't an option, so the next best thing is use an accessbility scripting feature and have the machine think a user is doing the clicks, allowing you to set the password:
-- The delays can be shorter, coordinates may vary
-- Best way to get the coordinates is with Apple screen capture (command-shift-3) from upper right to lower left (the coordinates will be shown)
-- if one spends the time, the click events can be converted to Accessibility AppleScript objects by capturing them as variables, or checking the events and using the events instead of the click coordinates
tell application "System Events"
tell application "Safari"
activate
end tell
delay 1
-- click develop menu (make sure its on first)
click at {430, 12}
delay 1
-- click Allow Javascript menu from Apple Events
click at {615, 615}
delay 1
-- Click the Allow Button
click at {1010, 386}
end tell

Accessing an Applescript from 2 different menus produces different results. Why?

I've got an application that has an embedded Script menu for running Applescripts. However, if you try to run scripts with certain functions (mostly UI related) they won't work unless you run them from the System script menu (in menu bar).
For example, if in theApplication you say :
tell application "System Events" to tell application process "theApp" to get all windows
it will return an empty list if run from the program's script menu, but 2 if run from the system script menu. I've also tried:
tell application "System Events"
tell application process "theApp"
set allElements to UI elements
display dialog (count of allElements)
end tell
end tell
-- returns 2 when run from System script menu but 0 when run from within theApp.
Also if you run from the Script Editor it will work fine. GUI scripting is enabled for the application in System Preferences, so I'm curious as to why this is happening, and any workarounds for it (other than run the script from the System Script menu)? The Dictionary shows Standard Suite so it should have access to windows...
Any ideas?
I believe you haven't provisioned the app in question to use assistive devices.
You should really open System Preferences, then click the button for "Security", then unlock, and click the button for Assitive Devices, (the blue icon with a white man), then drag your app onto it (reveal your app from its Dock menu), and permit it to control your machine, remember to lock the pane afterwards.
I can not guaranttee this to work, but it is certainly worth a try.

How to grant Applescript permissions through Applescript?

If I give my Applescript to another person, they would have to manually allow Applescript control of their computer by going into System Preferences, clicking Security & Privacy, then clicking Privacy and then Accessibility, then finally add Applescript. Is there anyway I can make Applescript add its self so they don't have to? Is there another solution? Because without them doing this, Applescript can't click things.
Without applescript added so it can have control, I get the error, "Script Editor is not allowed assistive access."
I believe that your are trying to enable assistive access for devices. For instance, GUI scripting requires this to be enabled. You can't enable it directly, but you can point the user to the place they need to go. The following finds out if assistive access is enabled and brings up the System Preference pane where the option is.
tell application "System Events"
set UI_enabled to UI elements enabled
end tell
if UI_enabled is false then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "This script utilizes the built-in Graphic User Interface Scripting architecture of Mac OS x which is currently disabled." & return & return & "You can activate GUI Scripting by selecting the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 1 buttons {"Cancel"} default button 1
end tell
end if
That should be enough for your users to get the hint.
You can theoretically do it via the command line:
https://raymii.org/s/snippets/OS-X-Enable-Access-for-assistive-devices-via-command-line.html
For example, in Lion and Mountain Lion it's a simple as this:
touch /private/var/db/.AccessibilityAPIEnabled

Resources