You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator Interactive Screenshot capturing desktop instead of application

HI everyone - I saw a similar thread from last year, but it fizzled out without an answer.


I recently bought an MBP16, and I'm just getting it all set up. I design software, and in my role I frequently need to capture parts of screens, webpages and meetings that I'm in and paste them into a notebook.


I've been doing this by hitting Cmd+Ctrl+Shift+4, then pasting. It works just fine, but I'm finding the key combination really painful to use.


I thought about using Automator, and creation an Action that I could throw onto the Touch Bar. Using the Touch Bar seems like a great way to do this, rather than work out another keyboard shortcut.


It seems simple, and when I run the Action from inside of Automator it does exactly what I'd hoped - allows me to draw a rectangle and captures that image irrespective of what applications are currently shown on my desktop. (This proven by pasting the resultant clipboard into a notebook).


However, when I add the same Action to my Touch Bar, it seems to run, but actually drills through all Applications and takes a screenshot of my desktop background. I've tested across Safari, Finder, Notability and so on, and it's irrelevant which Applications are open - in all circumstances only the desktop background is captured.


I've tried the Security Options - for example, the specific Action now has Screen Recording permissions as well as Automator. It didn't make any difference.


Anyone got any ideas on why this would work inside of Automator, but not when saved to the Touch Bar?


Thanks in advance!


MacBook Pro 16″, macOS 11.3

Posted on May 5, 2021 3:29 AM

Reply
Question marked as Top-ranking reply

Posted on May 5, 2021 10:52 AM

I took a quick look at this. It's almost certainly related to the privacy controls in MacOS designed to prevent background apps from snooping on others.


I haven't yet narrowed down a complete picture (no pun intended) of what's happening, but one thing is clear - the target application (that you are trying to grab) also has to have Screen Recording enabled.


I think this has something to do with the fact that, technically, the application is in the background when you invoke the script (because Automator/Script Runner is technically the frontmost application at that time and thus your app windows are in the background).

I can validate this using two apps - for example, Terminal.app (which has Screen Recording enabled) and Mail.app (which does not).


With both apps open side by side when I invoke the script, I start my screen grab on a Terminal.app window and it works (usually, although not 100% for reasons I don't yet know). However if I start my grab on the Mail.app window then I capture the background pattern.


I had a little more consistency accessing via the Services menu rather than the Touch Bar, which may have something to do with how the script is getting invoked. So maybe a Service (with a corresponding command-key) would be a better way to go.


I since the target app needs to have screen recording enabled, I still can't see a way to get it to capture any arbitrary application, though (which may be a good thing though since it indicates Apple's security controls are doing something right :) )

Similar questions

4 replies
Question marked as Top-ranking reply

May 5, 2021 10:52 AM in response to inedria

I took a quick look at this. It's almost certainly related to the privacy controls in MacOS designed to prevent background apps from snooping on others.


I haven't yet narrowed down a complete picture (no pun intended) of what's happening, but one thing is clear - the target application (that you are trying to grab) also has to have Screen Recording enabled.


I think this has something to do with the fact that, technically, the application is in the background when you invoke the script (because Automator/Script Runner is technically the frontmost application at that time and thus your app windows are in the background).

I can validate this using two apps - for example, Terminal.app (which has Screen Recording enabled) and Mail.app (which does not).


With both apps open side by side when I invoke the script, I start my screen grab on a Terminal.app window and it works (usually, although not 100% for reasons I don't yet know). However if I start my grab on the Mail.app window then I capture the background pattern.


I had a little more consistency accessing via the Services menu rather than the Touch Bar, which may have something to do with how the script is getting invoked. So maybe a Service (with a corresponding command-key) would be a better way to go.


I since the target app needs to have screen recording enabled, I still can't see a way to get it to capture any arbitrary application, though (which may be a good thing though since it indicates Apple's security controls are doing something right :) )

May 5, 2021 11:11 PM in response to Camelot

Wow, I certainly learned a lot from that, and have a 100% working solution.


A couple of notes:

  1. I had to run the AppleScript without "close window 1" initially or the OS never got to ask me to approve it calling Terminal. Once that had been sorted, then "close window 1" was added back in and worked fine.
  2. Once I added this to the Touch Bar I was able to approve the generic ServicesUIAgent to control the Terminal, which in turn allowed me to disable Terminal Control from all other Apps.


So now I have a Touch Bar Action triggering copy-to-clipboard interactive screen captures.


Thanks again for all your help!

May 5, 2021 2:32 PM in response to inedria

OK, a little further digging and some progress.


I was initially put off by Automator's notice about taking a screenshot - I get why it does this, to make sure the user is aware that a screenshot is being taken, but it gets in the way of the flow.


As a result, I first tweaked the workflow to instead use the shell command screencapture to take the screenshot:



This avoided the pop-up (because Automator is ignorant as far as the commands running in the shell). However, it still has the issue of sometimes capturing the desktop pattern rather than the app window.


So then I stepped back one step further - since (at least in my case) Terminal.app has Screen Recording enabled, I used Run AppleScript to invoke Terminal.app to do the work. This gets around the popup (Automator is unaware of what the AppleScript/shell script is doing), and seems to reliably capture the screen (since Terminal.app has screen recording rights).


The slight downside of this is that it opens a terminal window to run the command, but that can then be closed by additional AppleScript commands, like:



so far this seems to run reliably, capturing any window, and not invoking a popup warning. You will need to make sure Terminal.app has screen recording enabled.



Automator Interactive Screenshot capturing desktop instead of application

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.