MacOS – How to rework a “System Events” command in AppleScript, so that it automatically works in all applications

applescriptmacospermission

With the following AppleScript code within a "Run AppleScript" action in a Service:

tell application "System Events"
    set theSBounds to {{572, 64}, {332, 515}}
    set position of window "Google Hangouts - myemailaddress@gmail.com" of application process "Google Chrome" to item 1 of theSBounds
    set size of window "Google Hangouts - myemailaddress@gmail.com" of application process "Google Chrome" to item 2 of theSBounds
end tell

the Service will only work if the application from which that Service has been called has been added to the list of applications under "Allow the apps below to control your computer." This list is found at System Preferences → Security & Privacy → Accessibility.

Is it possible to modify the AppleScript code somehow so that adding every application on one's computer to the list is not necessary? Or, so that, one only needs to add one item to the list, as opposed to every application on one's computer?

I thought that something like this might work:

do shell script "osascript -e 'tell application \"System Events\"' -e 'set theSBounds to {{572, 64}, {332, 515}}' -e 'set position of window \"Google Hangouts - myemailaddress@gmail.com\" of application process \"Google Chrome\" to item 1 of theSBounds' -e 'set size of window \"Google Hangouts - myemailaddress@gmail.com\" of application process \"Google Chrome\" to item 2 of theSBounds' -e 'end tell'" with administrator privileges

but this code also requires that the application from which the Service is run is added to the Accessibility list.

Note: I only want to run this AppleScript as a Service, and not in an Automator-created Application, because the code receives selected text as its input (system-wide).

Best Answer

I've found an okay solution (i.e., workaround).

Insert the "System Events" block of code into a "Run AppleScript" action and save it as its own application file in Automator. Note: The file extension of this code must be .app in order for this to work (more on this in a moment).

Go to System Preferences → Security & Privacy → Privacy tab → Accessibility. Add and checkmark the newly created application to the list of apps allowed to control your computer.

Run the newly created app from within your Service. To do so, you may add the following line to your AppleScript code:

do shell script "osascript -e 'tell application \"Application Name\" to activate'"

(I habitually use osascript to activate applications when using a "Run AppleScript" action in a Service, as this circumvents a bug with Services that I've discovered.)

Your Service should now run perfectly in every application on your computer, despite the fact that only one application (the one that contains the "System Events" code) has explicitly been granted the power to control the computer.

The file must be saved as a .app file, and not as a .scpt or .applescript file. This is because it is not possible to add .scpt or .applescript files to the list of apps allowed to control your computer; only true .app files can be selected in the dialog.

Note that the .app file does not have to be created in Automator. If you create the .app file in Script Editor.app and follow the other steps, the .app file will function in the same manner.

There is one difference, however. When the .app file is created in Automator, one can use either activate or launch to run the application in their Service. But, when the .app file is created in Script Editor, one can only use activate to run the application; launch will result in nothing happening.