The screencapture utility has a -w switch which allows capturing the front window, but only after highlighting the window and getting a click from the user.

Is there any way to capture the front window non-interactively using some shell command?

  • I can't find any screenshot command on my Mac running OS X 10.8.3. Is it an addition you've installed? Which is your OS version? – jaume May 16 '13 at 12:47
  • @jaume corrected to screencapture – GJ. May 16 '13 at 16:45

The -l option allows capturing a window by ID. It is only shown in the help message and not on the man page.

I don't know any way to find the window IDs that would work with all applications though. This doesn't work with Preview, Activity Monitor, or Chrome:

screencapture -l $(osascript -e 'tell application (path to frontmost application as text) to id of window 1') /tmp/screencapture.png

You must log in to answer this question.

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