Sub topic 12
Capture from Terminal with screencapture
This chapter assumes you can open Terminal, run a command, and read a basic command with flags. You should also know the keyboard shortcuts from earlier chapters. The screencapture command is built into macOS. It does the same work as the shortcuts, but you can call it from scripts, schedule it, or add a delay.
How the command works
The general form is screencapture [options] [file]. If you give a file name, the capture is written there. The file extension does not choose the format. Use the -t option for that, or the default format is used.
With no options, screencapture file.png captures the whole screen immediately. Options change what is captured and where it goes.
| Option | Effect |
|---|---|
| `-i` | Interactive mode. You drag to select an area. Press Space to switch to window selection. |
| `-T 5` | Wait 5 seconds before capturing. |
| `-c` | Send the capture to the clipboard instead of a file. |
| `-w` | Window mode only, used together with `-i`. |
| `-x` | Do not play the shutter sound. |
Capture a selected area
Start an interactive capture and save it to the Desktop:
screencapture -i ~/Desktop/selection.pngThe pointer turns into a crosshair. Drag over the area you want. Release to save the file. Press Esc to cancel, and no file is created.
Capture after a delay
Menus close when you click away from them. A delay solves this. Start the command, open the menu, and wait for the capture:
screencapture -T 5 ~/Desktop/delayed.pngThis captures the full screen after five seconds.
Copy to the clipboard
Add -c to skip the file. Paste the result into a document or chat window afterward:
screencapture -c -iHere no file name is given, because the image goes to the clipboard.
Use it in a script
Fixed file names overwrite earlier captures. A timestamp in the name avoids this:
#!/bin/bash
mkdir -p ~/Pictures/Screenshots
screencapture -x ~/Pictures/Screenshots/shot-$(date +%Y%m%d-%H%M%S).pngSave this as shot.sh, run chmod +x shot.sh, then run ./shot.sh. Each run creates a new file such as shot-20261127-122414.png.
Common errors
- A blank or wallpaper-only image. Your terminal app lacks Screen Recording permission. Grant it in System Settings, under Privacy & Security, then restart the terminal app.
- No file appears after
-i. You pressed Esc, or you clicked without dragging. Run the command again. - The format is wrong. A
.jpgextension does not guarantee JPEG data. Add-t jpgto set the format explicitly. - The delay captured the wrong thing. The timer starts when you press Return. Arrange your windows before then, or lengthen the delay.
Exercise
Write one command that waits 3 seconds, then saves a JPEG of the full screen to the Desktop as test.jpg without playing the shutter sound.
Show answer
screencapture -x -T 3 -t jpg ~/Desktop/test.jpg
Exercise
You run screencapture -c -i, drag over an area, and find no new file in the Desktop folder. Where is the image, and how do you use it?
Show answer
The -c option sent it to the clipboard. Paste it with Command-V into an app that accepts images, such as Notes, Mail, or Preview (File, New from Clipboard).