While the standard macOS keyboard shortcuts (Command+Shift+3 and Command+Shift+4) are excellent for capturing quick screenshots during daily usage, they are entirely useless when you need to automate visual documentation, build QA testing scripts, or monitor a remote Mac server. For system administrators and developers, Apple includes a powerful, hidden command-line utility called screencapture.
By leveraging the screencapture binary via the Terminal or within bash scripts, you can programmatically capture specific windows, record screen activity, hide the mouse cursor, and save the output directly to custom file paths without any user interaction.
Executing Basic Command-Line Captures
The screencapture utility is located in the standard system binary path (/usr/sbin/screencapture), meaning it can be called directly from any Terminal session. The most basic implementation requires only a single argument: the output file path.
screencapture ~/Desktop/automated_screenshot.png
This command instantly captures the entire primary display and saves it as a PNG file to the current user’s desktop. However, the true power of the utility lies in its extensive array of modifying flags.
Automating Silent Window Captures
If you are writing an automated script that documents the configuration of a specific application, you do not want to capture the entire messy desktop background. You can instruct the utility to capture only a specific window.
screencapture -l $(osascript -e 'tell app "System Events" to id of window 1 of process "Safari"') ~/Desktop/safari_window.png
In standard usage, capturing a window requires the user to press the Spacebar and click. To automate this entirely silently, you can use the -x flag to mute the camera shutter sound, and the -C flag to ensure the mouse cursor is not included in the final image.
screencapture -x -C -W ~/Desktop/clean_window.png
Integrating Delays and Timers
When scripting UI testing, you often need the graphical interface to reach a specific state before the screenshot is taken—such as waiting for a dropdown menu to render or an animation to finish. The -T flag allows you to insert a precise delay, measured in seconds, before the capture executes.
screencapture -T 5 ~/Desktop/delayed_capture.png
This command tells the system to wait exactly five seconds before executing the capture, providing ample time for the script’s previous commands to resolve visually on the screen.
Capturing to the Clipboard for Automated Pasting
Not all automated workflows require saving physical files to the hard drive. If your script interacts with a secondary application that accepts image input—such as a Slack bot or an automated reporting tool—you can direct the screencapture output straight into the macOS clipboard using the -c flag.
screencapture -c
When combined with AppleScript or other command-line automation tools, this allows you to seamlessly capture the screen state and instantly inject the resulting image data into emails, documents, or messaging platforms without cluttering the local file system with temporary image files.