Joshua
Joshua

Reputation: 752

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.

Upvotes: 3

Views: 2897

Answers (1)

Jerry Stratton
Jerry Stratton

Reputation: 3456

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.

  1. 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.

First, create the application with the steps you want performed.

  1. 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.)

  2. Use Automator to create a Quick Action. Give it the action “Launch Application” and tell it to launch the application you just created.

Create a Service to launch the application.

  1. 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.

Add it to the accessibility list by dragging and dropping.

  1. 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.

And 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.

Upvotes: 5

Related Questions