LoginSignup
6
1

More than 5 years have passed since last update.

【macOS】コマンドラインから現在開いているアプリのスクリーンショットを撮る

Posted at

実行するコマンド

terminal
screencapture -l$(osascript -e 'tell app "App名" to id of window 1') ~/Desktop/image.png

iTerm2のスクリーンショットを撮る場合

screencapture -l$(osascript -e 'tell app "iTerm" to id of window 1') image.png

結果
image.png

解説

ざっくり解説していきます。

screencapture

macOSには、screencaptureコマンドが標準機能として提供されています。
これはcmd + shift + 3など、ショートカットでスクリーンショットを撮った時に実行されるコマンドで、ショートカット以外でもコマンドライン上から実行することが可能です。

screencaptureにはいくつかのオプションがあり、今回は特定のwindowIDのウィンドウをキャプチャする-lオプションを使用しました。
これまでcmd + shift + 4で領域選択でキャプチャを撮っていましたが、このコマンドを知っておくとと結構便利かも :thinking:

$ man screencapture

...中略
-l      <windowid> Captures the window with windowid.
...

特定のアプリケーションからWindowIDを取得する方法

起動しているアプリケーションからWindowIDを取得するには、AppleScriptから実行する必要があります。
そこで、osascriptコマンドをターミナルから実行します。osascriptは、コマンドラインからAppleScriptが実行できるコマンドです。

注意

Chrome・LINE・メッセージなど、一部、撮影できないアプリもありました。
原因はまだしっかり把握できていないので、ご存知の方はご教示いただけると幸いです :pray:

参考

6
1
0

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
6
1