Sub topic 12
Capture from Terminal with screencapture
The screencapture command is the same tool the keyboard shortcuts and the Screenshot toolbar use, exposed as a command. It lets you capture from scripts, schedule captures, and name files exactly as you want.
This chapter assumes you can open Terminal and run a command, and that you know the capture modes from earlier chapters: full screen, selection, window, clipboard, and recording. It covers the options you will use most, how to combine them, and the errors that come up. The next chapter covers defaults, which changes the settings the shortcuts use.
The basic shape of the command
The general form is:
screencapture [options] [file ...]Options come first and the output path comes last. With no mode option, screencapture captures the whole screen right away and writes it to the file you name.
screencapture ~/Desktop/full.pngThe file extension does not choose the format. By default the data is PNG whatever the name says. To get another format, pass -t, as shown later in this chapter.
If you leave out the file name and do not use -c, there is nothing useful to write to. Always give a path, or send the capture to the clipboard.
Choosing a capture mode
Each mode matches something you already do with the keyboard.
| Option | What it does | Keyboard equivalent |
|---|---|---|
| (none) | Captures the full screen | Shift-Command-3 |
| `-i` | Interactive: drag to select an area, or press Space to switch to window mode | Shift-Command-4 |
| `-s` | Selection mode only, so Space does not switch to window mode | Shift-Command-4 |
| `-w` | Window mode only (use it with `-i`) | Shift-Command-4, then Space |
| `-W` | Start in window selection mode, but allow switching to selection | none |
| `-c` | Send the image to the clipboard instead of a file | add Control to a shortcut |
| `-R x,y,w,h` | Capture a fixed rectangle without any interaction | none |
Here are the three most common forms:
# Drag to select an area, save as a PNG
screencapture -i ~/Desktop/selection.png
# Click a window to capture it (includes the shadow)
screencapture -i -w ~/Desktop/window.png
# Capture a fixed rectangle: x=0, y=0, width=800, height=600
screencapture -R0,0,800,600 ~/Desktop/corner.pngThe -R option is the one that makes scripting practical. It needs no mouse input, so it works in a repeatable job. Its values are in points, not pixels. On a Retina display the saved image has twice as many pixels in each direction as the numbers you gave.
Modifier options
Modifiers change how a capture behaves without changing which mode it uses.
-xturns off the camera shutter sound. Use it in scripts so a background capture is silent.-T secondswaits before capturing, which gives you time to open a menu or arrange windows.-Cincludes the mouse pointer. The shortcuts leave it out by default.-oleaves out the window shadow in window mode. This gives you a tighter image.-t formatsets the format. Accepted values includepng,jpg,pdf,tiffandgif.-Popens the result in Preview when the capture finishes.
Options can be combined in one command:
screencapture -x -T 5 -C -t jpg ~/Desktop/delayed.jpgThis waits five seconds, then silently captures the full screen with the pointer included, and writes JPEG data. The delay lets you set up the screen first.
Window capture without the shadow
By default, window capture adds a soft shadow with transparent margins. For documentation, the shadow is often unwanted. Add -o:
screencapture -i -w -o ~/Desktop/window-clean.pngSending captures to the clipboard
With -c, the image goes to the clipboard. You can then paste it into a message or document with Command-V.
screencapture -c -iThe -c option can be combined with a mode. This command copies a window and plays no sound:
screencapture -c -x -i -wWhen -c is used, no file is written, so do not also expect one.
Building useful file names
Fixed names are overwritten each time. To keep every capture, put a timestamp in the name with the date command:
screencapture -x ~/Desktop/shot-$(date +%Y%m%d-%H%M%S).png$(date ...) runs date and puts its output into the command line, so you get names like shot-20261127-121526.png. These names sort in time order in Finder.
Quote any path that might contain spaces:
screencapture -i "$HOME/Desktop/My Notes/capture 1.png"Without the quotes, the shell splits the path into several arguments, and screencapture fails or writes to the wrong place.
Recording video from Terminal
On current macOS versions, screencapture can also record. Add -v for video. Add -V seconds to stop automatically after a set time.
screencapture -v -V 10 ~/Desktop/clip.movThis records ten seconds of the screen to a QuickTime movie. Related options include -g to include audio and -k to show mouse clicks. Option support varies between macOS releases, so check your own system with man screencapture. For long or detailed recordings, the Screenshot toolbar gives you more control.
A small script
This script captures a selected area, names the file by time, and opens it. It stops cleanly if you press Escape during selection.
#!/bin/bash
set -euo pipefail
dir="$HOME/Pictures/Screenshots"
mkdir -p "$dir"
file="$dir/shot-$(date +%Y%m%d-%H%M%S).png"
screencapture -i -x "$file"
if [ -s "$file" ]; then
open "$file"
else
echo "Capture cancelled."
fiIf you cancel an interactive capture with Escape, screencapture exits without creating a file. That is why the script tests the file with -s (exists and is not empty) instead of assuming success. Save it as snap.sh, run chmod +x snap.sh, then run ./snap.sh.
How the options fit together
Permissions and common errors
Terminal needs permission to see what is on screen. If it lacks that permission, screencapture may still produce a file, but the image shows only the desktop wallpaper and the menu bar. App windows are missing.
To fix this:
- Open System Settings and go to Privacy & Security.
- Open Screen & System Audio Recording. On older versions the item is called Screen Recording.
- Turn on the app that runs your command. This is the app you typed the command in, such as Terminal, iTerm, or your code editor.
- Quit and reopen that app, because the permission applies to new sessions.
Other problems and their causes:
| Symptom | Likely cause |
|---|---|
| Image shows only wallpaper | Screen recording permission is missing for the host app |
| No file appears after `-i` | You pressed Escape, or the path is not writable |
| "cannot create file" style error | The folder does not exist, or the path has unquoted spaces |
| File opens as the wrong type | The name says `.jpg` but `-t jpg` was not given |
| Image is twice the size you expected | Retina display: `-R` uses points, the file uses pixels |
Choosing between the command and the shortcuts
The shortcuts are faster for one-off captures. Use screencapture when you need one of the following:
- A repeatable capture of the same region, through
-R. - Predictable file names, through
date. - A capture started by another script or a keyboard launcher.
- A delay or a silent capture without opening the toolbar.
The command does not offer Markup or trimming. Open the result in Preview or use the floating thumbnail workflow from earlier chapters for editing.
Exercise
Write one command that waits 3 seconds, captures the full screen silently with the mouse pointer included, and saves it as a JPEG with a timestamp in the name in ~/Pictures/Screenshots. What should you check if the saved image shows only the wallpaper?
Show answer
Command: screencapture -x -T 3 -C -t jpg ~/Pictures/Screenshots/shot-$(date +%Y%m%d-%H%M%S).jpg (the folder must already exist; create it with mkdir -p ~/Pictures/Screenshots). If the image shows only the wallpaper, check System Settings > Privacy & Security > Screen & System Audio Recording and make sure the app running the command, such as Terminal, is allowed. Then quit and reopen that app.
Exercise
Why does the script in this chapter test the output file with [ -s "$file" ] instead of assuming the capture worked?
Show answer
Pressing Escape during an interactive capture cancels it, and screencapture then exits without creating a file. Testing that the file exists and is not empty lets the script handle a cancel instead of trying to open a file that is not there.