4

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

  • Are you dead set on using that script as a service? Why not just save the script as an application through script editor? Then you only need to add that one app to allow accessibility in system preferences. Then you can take it one step further and make the app speakable and launch it whenever you want with a dictation command. – wch1zpink Feb 4 '17 at 3:02
  • Thanks for the suggestion. I didn't want to post the full code, as it's not related to my question, but in it, you would see that the code accepts the currently highlighted text as its input. Short of first copying the selected text to the clipboard, an AppleScript as an application file cannot access the selection. Also, I am unaware of even one third-party program that can get the selected text and send it as a variable to an AppleScript. – rubik's sphere Feb 4 '17 at 3:21
  • On the second point, at one time, I was a (brief) user of Mac's Dictation Commands feature. But, I eventually found that it was not reliable enough to use. If my speaking fails to trigger the dictation even only 10% of the time, it's simply easier (IMO) to launch the application from the menu bar or by keyboard shortcut than to rely on my dictation being heard correctly. – rubik's sphere Feb 4 '17 at 3:22
  • That's one of the drawbacks of automation in that one has to give permission for every app that controls the computer through an automation process. So you'll have to add apps as necessary. I'm sure that's not the answer you wanted to hear but does that answer the question you asked, or am I not understanding what your asking? – user3439894 Feb 4 '17 at 3:54
  • @user3439894 I think you're understanding correctly. Here's another way that I can put my question: Is there a substitute for "System Events" that I can use to accomplish the same effect? The problem with "System Events" in AppleScript is that it actually uses whatever app that one currently has open to execute the command, as opposed to using the "System Events" app to execute the command (located at /System/Library/CoreServices/System Events.app). – rubik's sphere Feb 4 '17 at 4:02
2

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.

  • 1
    Don't forget to mark this as the accepted answer! :) +1 for a nice workaround! – user3439894 Mar 14 '18 at 10:44
  • Thank you, I got a different error if I tried to activate a workflow, but your work around still seems to work. – The Matt Dec 12 '18 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .