Mac でのマルチディスプレイの配置を記憶させてショートカット一発で再現可能にしてみた(displayplacer)

2023.09.19

こんにちは、CX事業本部 Delivery部の若槻です。

PC で業務をする際に複数のディスプレイを接続して利用(マルチディスプレイ)している方は多いと思いますが、その際にPC を繋ぎ直すとディスプレイの配置がバラバラになるので並び替えを手動でしているという方もまた多いのではないでしょうか。

今回は、Mac でのマルチディスプレイの配置を記憶させてコマンド一発で再現できる displayplacer というコマンドを見つけたのですが、それがとても便利だったのでご紹介します。

試してみた

インストール

brew tap jakehilborn/jakehilborn && brew install displayplacer

現在の配置を設定するコマンドの取得

まず、記憶させたいディスプレイ配置を手動で設定します。私の場合は下記のようにしました。

displayplacer listコマンドを実行します。

$ displayplacer list
Persistent screen id: 37D8832A-2D66-02CA-B9F7-8F30A301B230
Contextual screen id: 1
Serial screen id: s4251086178
Type: MacBook built in screen
Resolution: 1440x900
Hertz: 60
Color Depth: 8
Scaling: on
Origin: (0,0) - main display
Rotation: 0 - rotate internal screen example (may crash computer, but will be rotated after rebooting): `displayplacer "id:37D8832A-2D66-02CA-B9F7-8F30A301B230 degree:90"`
Enabled: true
Resolutions for rotation 0:
  mode 0: res:960x600 hz:60 color_depth:8 scaling:on
  mode 1: res:1024x640 hz:60 color_depth:8 scaling:on
  mode 2: res:1280x800 hz:60 color_depth:8 scaling:on
  mode 3: res:1440x900 hz:60 color_depth:8 scaling:on <-- current mode
  mode 4: res:1680x1050 hz:60 color_depth:8 scaling:on
  mode 5: res:1920x1200 hz:60 color_depth:8
  mode 6: res:2048x1280 hz:60 color_depth:8
  mode 7: res:2560x1600 hz:60 color_depth:8

Persistent screen id: 204F4A82-3DCD-18C4-0857-D6964E3302DB
Contextual screen id: 2
Serial screen id: s809910604
Type: 49 inch external screen
Resolution: 5120x1440
Hertz: 60
Color Depth: 8
Scaling: off
Origin: (-1965,-1440)
Rotation: 0
Enabled: true
Resolutions for rotation 0:
  mode 0: res:800x600 hz:75 color_depth:8
  mode 1: res:800x600 hz:60 color_depth:8 scaling:on
  mode 2: res:800x600 hz:60 color_depth:8
  mode 3: res:800x600 hz:30 color_depth:8 scaling:on
  mode 4: res:960x540 hz:60 color_depth:8 scaling:on
  mode 5: res:960x540 hz:50 color_depth:8 scaling:on
  mode 6: res:1024x768 hz:75 color_depth:8
  mode 7: res:1024x768 hz:60 color_depth:8
  mode 8: res:1152x864 hz:75 color_depth:8
  mode 9: res:1280x540 hz:60 color_depth:8 scaling:on
  mode 10: res:1280x720 hz:60 color_depth:8
  (中略)
  mode 116: res:3840x1080 hz:60 color_depth:8

Persistent screen id: 3FF39CA8-DDD5-4980-C90B-A2661D91F570
Contextual screen id: 25
Serial screen id: s0
Type: 31 inch external screen
Resolution: 1920x1080
Hertz: 63
Color Depth: 4
Scaling: off
Origin: (-1920,0)
Rotation: 0
Enabled: true
Resolutions for rotation 0:
  mode 0: res:960x540 hz:63 color_depth:4 scaling:on
  mode 1: res:1280x720 hz:63 color_depth:4
  mode 2: res:1920x1080 hz:63 color_depth:4 <-- current mode
  mode 3: res:640x360 hz:63 color_depth:4 scaling:on

Execute the command below to set your screens to the current arrangement. If screen ids are switching, please run `displayplacer --help` for info on using contextual or serial ids instead of persistent ids.

displayplacer "id:37D8832A-2D66-02CA-B9F7-8F30A301B230 res:1440x900 hz:60 color_depth:8 enabled:true scaling:on origin:(0,0) degree:0" "id:204F4A82-3DCD-18C4-0857-D6964E3302DB res:5120x1440 hz:60 color_depth:8 enabled:true scaling:off origin:(-1965,-1440) degree:0" "id:3FF39CA8-DDD5-4980-C90B-A2661D91F570 res:1920x1080 hz:63 color_depth:4 enabled:true scaling:off origin:(-1920,0) degree:0"

現在のディスプレイの情報と設定可能な解像度の一覧が表示されるのですが、ここで確認したいのは最後の行の下記コマンドです。

displayplacer "id:37D8832A-2D66-02CA-B9F7-8F30A301B230 res:1440x900 hz:60 color_depth:8 enabled:true scaling:on origin:(0,0) degree:0" "id:204F4A82-3DCD-18C4-0857-D6964E3302DB res:5120x1440 hz:60 color_depth:8 enabled:true scaling:off origin:(-1965,-1440) degree:0" "id:3FF39CA8-DDD5-4980-C90B-A2661D91F570 res:1920x1080 hz:63 color_depth:4 enabled:true scaling:off origin:(-1920,0) degree:0"

コマンドのオプションは次のようになります。Mac 本体を含む3つのディスプレイに対して各情報を設定しています。

オプション 説明 必須
id ディスプレイのID
res 解像度
hz リフレッシュレート
color_depth 色深度
enabled 有効かどうか

このコマンドを実行することにより、現在のディスプレイ配置を設定することができます。

コマンド動作確認

次のようにディスプレイ配置を意図しない配置に変更します。

先ほど取得したコマンドを実行します。

displayplacer "id:37D8832A-2D66-02CA-B9F7-8F30A301B230 res:1440x900 hz:60 color_depth:8 enabled:true scaling:on origin:(0,0) degree:0" "id:204F4A82-3DCD-18C4-0857-D6964E3302DB res:5120x1440 hz:60 color_depth:8 enabled:true scaling:off origin:(-1965,-1440) degree:0" "id:3FF39CA8-DDD5-4980-C90B-A2661D91F570 res:1920x1080 hz:63 color_depth:4 enabled:true scaling:off origin:(-1920,0) degree:0"

すると記憶させたディスプレイ配置を再現することができました。

BetterTouchTool でタッチバーから一発で配置を再現可能にする

BetterTouchTool を使うと、ジェスチャーやタッチバーボタンで実行できる様々なアクションを Mac 端末に設定することができます。

せっかくなので BetterTouchTool でタッチバーから一発で配置を再現可能にしてみます。

まず displayplacer コマンドのパスを取得します。

$ which displayplacer
/opt/homebrew/bin/displayplacer

先ほどのディスプレイ配置コマンドを BetterTouchTool で設定し、タッチバーから一発で実行できるようにします。

設定は次のように行いました。

Execute Shell Script / Task では次のように指定しました。

  • Lauch Path: /bin/bash
  • Parameters: -c
  • Script: /opt/homebrew/bin/displayplacer "id:37D8832A-2D66-02CA-B9F7-8F30A301B230 res:1440x900 hz:60 color_depth:8 enabled:true scaling:on origin:(0,0) degree:0" "id:204F4A82-3DCD-18C4-0857-D6964E3302DB res:5120x1440 hz:60 color_depth:8 enabled:true scaling:off origin:(-1965,-1440) degree:0" "id:3FF39CA8-DDD5-4980-C90B-A2661D91F570 res:1920x1080 hz:63 color_depth:4 enabled:true scaling:off origin:(-1920,0) degree:0"

MacBook のタッチバーにコマンド実行ボタンを設定できました。

これで、ディスプレイ配置の再現を一発で行えるようになりました。

おわりに

displayplacer を使って Mac でのマルチディスプレイの配置を記憶させてショートカット一発で再現可能にしてみました。

これだけでもとても便利なのですが、加えて BetterTouchTool の強力なショートカット機能を使うことでさらに便利にすることができました。Mac、特に MacBook Pro を使われている方は是非試してみてください。

参考

以上