Taking screenshots with the command line of a specific window

I wanna take a screenshot of a specific application using the command line. Doing

screencapture -h

Shows option "-o"

-o in window capture mode, do not capture the shadow of the window

Which sounds like it might do what I need, but I am lost on how to actually use this option. Google did not turn up any answers

Thanks!


Solution 1:

To capture a window using screencapture in Terminal, you have two options:

Interactively

-i         capture screen interactively, by selection or window
               control key - causes screen shot to go to clipboard
               space key   - toggle between mouse selection and
                             window selection modes
               escape key  - cancels interactive screen shot

You can use the -o option in conjunction with the -i option to not capture the shadow of the window, e.g.: -oi

Programmatically

-l<windowid> capture this windowsid

The -i option appears to be self-explanatory while the -l<windowid> option gives no additional info as with the -i option.


The <windowid>, for some windows can be ascertained by using AppleScript in the following example:

screencapture -l$(osascript -e 'tell app "Safari" to id of window 1') test.png

You can use the -o option in conjunction with the -l option to not capture the shadow of the window, e.g.: -ol

Some applications will not work with this method, however here are some alternatives:

  • screenshot: Better macOS screenshots via the Terminal
  • GetWindowID

Reference: How do I find the windowid to pass to screencapture -l?
Note: I am not affiliated with these projects.