37

The command line utility screencapture claims to be able to capture a single window without requiring interaction, but I can't figure out what to pass it.

-l<windowid> capture this windowsid

It's not the process id of the the application.

5 Answers 5

31

For some applications you can use AppleScript:

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

It doesn't work with Chrome though.

The IDs are also shown in Quartz Debug (available from developer.apple.com/downloads) if you run defaults write com.apple.QuartzDebug QuartzDebugPrivateInterface -bool true.

5
  • 1
    Unfortunately, this doesn't work with non-Applescriptable apps like Eclipse or Google Chrome.
    – Cajunluke
    Commented Jul 16, 2012 at 18:52
  • 1
    Yeah, my target app is not Applescriptable :( But thank you anyway. Commented Jul 16, 2012 at 19:13
  • 2
    Note that window ids are not guaranteed to correspond to the Quartz window id even in applications that are scriptable.
    – Chris Page
    Commented Aug 11, 2012 at 5:23
  • Quartz debug seems to no longer exist or else it has been renamed or bundled?
    – gman
    Commented May 31, 2018 at 3:37
  • 1
    Quartz Debug is included in "Additional Tools for Xcode" available at developer.apple.com/download/more. Commented Nov 23, 2019 at 15:53
15

I wrote a little command line utility to retrieve the Window ID for apps that don't support AppleScript. Get it here: https://github.com/smokris/GetWindowID

You can then capture a specific window by specifying its bundle name and window title:

screencapture -l$(./GetWindowID "Vuo Editor" "untitled composition") VuoEditorWindow.png
2
  • Hi, I'm trying to use it but I'm not sure on what I'm doing. I've downloaded the Makefile,GetWindowID.m. But when I launch the command ./GetWindowID.m it gives me this error:./GetWindowID.m: line 4: syntax error near unexpected token (' ./GetWindowID.m: line 4: int main(int argc, char **argv)'
    – KingBOB
    Commented Sep 6, 2013 at 22:25
  • 1
    @Giorgio: Open a Terminal window and change to the folder that contains Makefile and GetWindowID.m, then run the command make. It will produce a binary called GetWindowID, which you can then invoke using the command I described in my answer.
    – smokris
    Commented Sep 24, 2013 at 4:37
8

I wrote a command line utility that wraps over screencapture and the Quartz windowing library.

Grab it here: https://github.com/alexdelorenzo/screenshot

Use it like this:

screenshot Chrome -t "Stack Overflow"
1
  • Have you missed double-quote?
    – kenorb
    Commented Aug 17, 2015 at 8:36
4

As far as I can tell, there still isn't an easy way to get the window ID from the command line. However today (in 2022) we have Shortcuts that allow you to easily take a screenshot of a single window MacOS Shortcut

  • Open the Shortcuts app
  • Create a new shortcut
  • Use the "Find Windows" action, and filter until you get the window you want
  • Use the "Save File" action and save the file (it seems to be a bit weird, you choose a folder to save to (in my case reinoud), and then in subpath you put the file name). Just make sure that you set the first variable in this action (which is Window by default) to be "Image", by clicking on "Window" and select "Image" from the "Get" box.
  • Save the Shortcut by giving it a name
  • If you want to run the shortcut from the commandline, just use shortcuts run "Find My to File" (if Find My to File is the name of the shortcut)
3
  • thanks @claude for this, works perfectly! I feel you had a similar idea to what I've just been trying to do? 😉 ... twitter.com/find_my_brolly
    – ChrisB
    Commented Jun 19, 2022 at 15:28
  • Haha nice! I tried to make my position in a marathon known to my iPhone-less family. Didn’t quite work, seems that the “find my” screen doesn’t update that often when the machine is on screensaver…..
    – Claude
    Commented Jun 19, 2022 at 15:32
  • I like this solution, since I could add "Cropped Image" action as I need
    – flyisland
    Commented Aug 30, 2022 at 12:31
2

The window ID does not seem to be generally exposed to AppleScript. However, the window geometries are exposed. So instead of using screencapture -l you can use screencapture -R x,y,w,h to capture the specific portion of the display covered by the target window. With AppleScript, the process can be selected by Unix PID as property "unix id".

Assuming the process has just one window and your PID is in $PID:

screencapture -R \
`osascript -e 'tell application "System Events" to get { position, size } of first window of (first process whose unix id is '$PID')' | tr -d ' '` \
window.png

If the process has multiple windows, you need to select by the target window title:

screencapture -R \
`osascript -e 'tell application "System Events" to get { position, size } of (first window whose title is "My window titlebar text") of (first process whose unix id is '$PID')' | tr -d ' '` \
window.png

Tip: You can list the selectable properties of any object with get properties: osascript -e 'tell application "System Events" to get properties of first process whose unix id is '$PID

You must log in to answer this question.

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