LoginSignup
34
26

More than 5 years have passed since last update.

コマンドラインでスクリーンショットを撮影する

Last updated at Posted at 2015-02-10

今までCmd+Shift+4で座標を指定してスクリーンショットを撮影していたのですが、いちいちトラックパッドで範囲を指定するのが面倒だったので「コマンドラインからスクリーンショット撮れたりしないかなあ。引数で座標を渡したりできると最高なんだけどなあ。」と思って調べたところ、どうやらドンピシャの機能が標準であるというので試してみました。

参考にしたところ

screencaptureコマンド

その名もズバリscreencaptureで、man screencaptureするとマニュアルが出てきます。
以下Mavericksで実行したときのマニュアルです。

SCREENCAPTURE(1)          BSD General Commands Manual         SCREENCAPTURE(1)

NAME
     screencapture -- capture images from the screen and save them to a file or the clip-
     board

SYNOPSIS
     screencapture [-SWCTMPcimswxto] file

DESCRIPTION
     The screencapture utility is not very well documented to date.  A list of options
     follows.

     -c      Force screen capture to go to the clipboard.

     -C      Capture the cursor as well as the screen.  Only allowed in non-interactive
             modes.

     -i      Capture screen interactively, by selection or window.  The control key will
             cause the screen shot to go to the clipboard.  The space key will toggle
             between mouse selection and window selection modes.  The escape key will can-
             cel the interactive screen shot.

     -m      Only capture the main monitor, undefined if -i is set.

     -M      Open the taken picture in a new Mail message.

     -o      In window capture mode, do not capture the shadow of the window.

     -P      Open the taken picture in a Preview window.

     -s      Only allow mouse selection mode.

     -S      In window capture mode, capture the screen instead of 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 capture attached windows.

     -r      Do not add screen dpi meta data to captured file.

     files   where to save the screen capture, 1 file per screen

BUGS
     Better documentation is needed for this utility.

SECURITY CONSIDERATIONS
     To capture screen content while logged in via ssh, you must launch screencapture in
     the same mach bootstrap hierarchy as loginwindow:

     PID=pid of loginwindow
     sudo launchctl bsexec $PID screencapture [options]

HISTORY
     A screencapture utility first appeared in Mac OS X v10.2.

Mac OS                           June 16, 2004                          Mac OS

「いい文書がない」とか「もっといい文書が必要だ」とか書いていますが、まあなんとかなりそうです。

さて、どうやら選択範囲を引数で渡してキャプチャしてくれるオプションはないようなので、やりたいことは実現できなさそうです。と、思っていたのですが、オプションを間違えて指定したときに以下のヘルプが表示されました。

usage: screencapture [-icMPmwsWxSCUtoa] [files]
  -c         force screen capture to go to the clipboard
  -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
  -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
  files   where to save the screen capture, 1 file per screen

-Rオプションで範囲指定ができるようです。でも一番上の例文に-Rオプションが含まれていませんね。とりあえず試してみましょう。

$ screencapture -R1000,470,200,200 test.png

これで座標(1000,470)から(200,200)pxのキャプチャ(test.png)が実行したディレクトリに保存されているはずです。

$ ls
test.png

ファイルは生成されたようです。ではopenしてみましょう。

$ open test.png

するとプレビューが立ち上がって、キャプチャした内容が表示されています。やりました。

キーボードショートカットに登録する

ターミナルからはスクリーンショットの撮影に成功しました。しかし、キャプチャしたい瞬間にわざわざターミナルを起動してからコマンドを打ち込んで...としていたら大事な瞬間を逃してしまいます。
なので、キーボードショートカットとして実行できるようにしましょう。

$ screencapture -R567,191,800,480 ~/Desktop/"スクリーンショット `date '+%F'` `date '+%H.%M.%S'`.png"

上記のようなコマンドを用意しておきます。これはファイル名を"スクリーンショット YYYY-MM-DD hh.mm.ss.png"として保存するコマンドになります。
なぜdateが2回呼ばれているかというと、1回のdateで間にスペースを入れる方法がわからなかったためです。

あとは効率的なスクリーンショット作りを試行錯誤する その1:Mac編で紹介されている通り、Automaterでサービスを作成し、システム環境設定でキーボードショートカットを割り当てれば完了となります。

これで座標を毎回指定する作業から解放されます。
撮影したいものが(作業ミスなどで)移動した場合は、座標を指定し直すか自動的に判別してくれるツールを使うほかないのでしょうか。 ないのでしょうねぇ。

追記:ありました。AppleScript(Yosemite以降はJavascript)を使えばウィンドウのある要素の座標を取得できます。

34
26
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
26