ターミナルからスクリーンキャプチャを取得する
Mac でスクリーンキャプチャ(スクリーンショット)を取得するときは、「⌘ + Shift + 3」で全画面のキャプチャを撮るか、「⌘ + Shift + 4」で範囲指定または、続けて「Space」を押下して選択したウィンドウのキャプチャを撮ることが多いかと思います。
デフォルトの状態だと、スクリーンキャプチャを撮ると、デスクトップに png 形式で「スクリーンショット + 日時 .png」という名前で保存されます。
通常利用では標準のスクリーンキャプチャ機能で良いかと思いますが、一時的に画像の保存形式を変えたり、数秒おきに連続でとりたい場合では少々面倒です。
そんなときに便利な、ターミナル上でのコマンド実行によるスクリーンキャプチャ取得について紹介します。
screencapture コマンド
基本的な使い方
#スクリーンキャプチャをカレントディレクトリにファイル名「test.jpg」として保存する screencapture ./test.jpg
画像のフォーマットは「-t 」オプションで指定することも可能ですが、保存ファイル名の拡張子に合わせて、画像のフォーマットも変えてくれます。
使用可能なオプションは下記のコマンドで確認できます。執筆時点でのオプションは以下のとおり。
usage: screencapture [-icMPmwsWxSCUtoa] [files]
-c force screen capture to go to the clipboard
-b capture Touch Bar - non-interactive modes only
-C capture the cursor as well as the screen. only in non-interactive modes
-d display errors to the user graphically
-i capture screen interactively, by selection or window
control key - causes screen shot to go to clipboard
space key - toggle between mouse selection and
window selection modes
escape key - cancels interactive screen shot
-m only capture the main monitor, undefined if -i is set
-M screen capture output will go to a new Mail message
-o in window capture mode, do not capture the shadow of the window
-P screen capture output will open in Preview
-I screen capture output will in a new Messages message
-s only allow mouse selection mode
-S in window capture mode, capture the screen not the window
-t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats)
-T<seconds> Take the picture after a delay of <seconds>, default is 5
-w only allow window selection mode
-W start interaction in window selection mode
-x do not play sounds
-a do not include windows attached to selected windows
-r do not add dpi meta data to image
-l<windowid> capture this windowsid
-R<x,y,w,h> capture screen rect
-B<bundleid> screen capture output will open in app with bundleidBS
files where to save the screen capture, 1 file per screen
よく使うオプション
- -c キャプチャ画像をクリップボードにコピーします。Photoshop 等の画像編集ソフトと組み合わせると便利。
- -m メインモニタのみキャプチャします。マルチディスプレイ環境等で重宝します。
- -W 「⌘ + Shift + 4」+「Space」と同じく、特定のウィンドウのみキャプチャします。
- -T <任意の秒数> 指定した秒数経過後にスクリーンキャプチャします。所謂、タイマー機能。秒数をしてしなかった場合のデフォルトは5秒です。
#10秒後にスクリーンキャプチャを取得する screencapture -T 10 ./test.png
- -x キャプチャ時の音をならしません。連続でキャプチャするときなどに重宝します。
- -t 保存する画像のフォーマットを指定します。jpg,pdf,tiff 等で指定します。ヘルプには gif や bmp は書かれていませんが指定可能です。
- -R <x,y,w,h> キャプチャする領域の縦、横、深さ、高さを指定します。<x,y> は「⌘ + Shift + 4」したときの始点, <w,h> はドラッグして離したときの終点の座標を指定します。
#指定した範囲のスクリーンキャプチャを取得する screencapture -R 0,0,1000,1000 ./test.png
応用編
コマンドでスクリーンキャプチャが取得できることの一番のメリットは、シェルスクリプト等と組み合わせることができることです。例えば、下記のスクリプトは、2秒ごとに保存ファイル名を変えて、スクリーンキャプチャを連続で 10 回取得します。
シェルスクリプトの例
for i in `seq 1 10` do screencapture -x ./$i.jpg sleep 2 done
他にも、AppleScript や Javascript で利用することで、マウスのクリック操作や特定のキーを押下する操作と組み合わせて自動でスクリーンキャプチャを取得できます。
JavaScript の例
下記の例では Javascript を用いて、Safari の画面を「←」キーを押下しながら、連続でスクリーンキャプチャを取得します。
safari_app = Application("Safari");
safari_app.activate();
delay(0.1);
var se = Application("System Events");
for(var i = 1; i < 246; i++)
{
test = Application.currentApplication()
test.includeStandardAdditions = true
test.doShellScript("screencapture -R 0,0,1452,2000 ./" + i + ".png");
delay(0.2);
se.keyCode(123);
delay(0.5);
}