[Xcode 8.2][新機能] xcrunコマンドでシミュレーターのスクリーンショットと動画が撮れるようになりました!

2016.12.21

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

xcrunコマンドでシミュレーターのスクリーンショットと動画が撮れるようになった

以下、Xcode Release Notesから引用

You can take videos and screenshots of Simulator using the xcrun Xcode command-line utility. To take a screenshot, run the command xcrun simctl io booted screenshot. To take a video, run the command xcrun simctl io booted recordVideo .. (9887264)

スクリーンショットを撮る

シミュレーターが起動している状態で以下のコマンドを実行します。

xcrun simctl io booted screenshot

すると、以下のようにpngでスクリーンショットが出力されます。

Wrote screenshot to: Screenshot_iPhone-7_iOS-10.2_12-21-2016_00.43.46.043.png

また、以下のようにすればファイル名やファイル形式を指定できます。

xcrun simctl io booted screenshot --type=png test.png

指定できるファイル形式に関しては、

Can be "png", "tiff", "bmp", "gif", "jpeg". Default is png.

とのことだったので全て試してみたところ、gifだけ「Invalid file type: gif」となってしまいました。 他の形式はちゃんと撮れました。

ちなみにファイル名のところに-を指定すれば標準出力となるのでコマンド連携に使えそうです。

xcrun simctl io booted screenshot --type=png -

動画を撮る

シミュレーターが起動している状態で以下のコマンドを実行します。

xcrun simctl io booted recordVideo test.mov

すると以下のようにレコーディング中になります。

Recording... (Press CTL+C to stop)

適当なところでCTL+Cすればレコーディング終了。ファイルが出力されます。

Recording completed.

指定できる形式はこちら。

--type       "h264", "mp4" or "fmp4". Default is "mp4".

ちなみにこちらもファイル名のところに-を指定すれば標準出力となるのでコマンド連携に使えるかもしれません。

参考