LoginSignup
2
1

macでマルチディスプレイのときにウィンドウを1つのディスプレイに集める

Posted at

実現できること

  • マルチディスプレイ利用時にウィンドウを1つのディスプレイに集める(移動する)

作った動機

  • 昔存在していた ウィンドウを集める が無くなっていた
  • 既存のアプリケーションで目的を満たすものが見つからなかった

主な用途

以下のように使い分けたい時に使う。

  • 外部ディスプレイを共有用/投影用
  • 内蔵ディスプレイを自分のメモ/隠したい情報

動作確認環境

  • macOS Sonoma 14.2
  • Intel or AppleSilicon

使うもの

  • AppleScript
  • Automator
  • キーボードショートカット
  • アクセシビリティの設定

スクリプト

※改善を繰り返してるので最終形のみ見たい方は最後の方までスキップしてください。

段階1 単一のアプリケーションを指定の位置に移動

tell application "System Events"
    set theWindows to every window of application process "Slack"
    repeat with aWindow in theWindows
        set thePosition to position of aWindow
        set position of aWindow to {0, 0}
    end repeat
end tell
  • ... application process "xxxxx" の部分でアプリケーションのプロセス名を指定する
    • アプリケーション名はアクティビティモニタ等で確認できる プロセス名 を指定
  • 移動先は {0, 0} を変えることで変更可能

段階2 指定したプロセス名が存在しないときにエラーを無視する

tell application "System Events"
	try
		set theWindows to every window of application process "Slack"
		repeat with aWindow in theWindows
			set thePosition to position of aWindow
			set position of aWindow to {0, 0}
		end repeat
	end try
end tell
  • tryで括る
  • catchを記載してないのでエラーを無視するだけ

段階3 複数のアプリケーションを対象とする

tell application "System Events"
	try
		set theWindows to every window of application process "Slack"
		repeat with aWindow in theWindows
			set thePosition to position of aWindow
			set position of aWindow to {0, 0}
		end repeat
	end try
end tell

tell application "System Events"
	try
		set theWindows to every window of application process "Google Chrome"
		repeat with aWindow in theWindows
			set thePosition to position of aWindow
			set position of aWindow to {0, 0}
		end repeat
	end try
end tell
  • コピペしてプロセス名の変更を行っただけ

段階4 まとめる

set appList to {"Slack", "Google Chrome", "Finder", "MacVim"}

repeat with a in appList
	log a
	moveWindowToDisplay(a)
end repeat

on moveWindowToDisplay(appName)	
	tell application "System Events"
		try
			set theWindows to every window of application process appName
			repeat with aWindow in theWindows
				set thePosition to position of aWindow
				set position of aWindow to {0, 0}
			end repeat
		end try
	end tell
end moveWindowToDisplay

余裕があるなら改善したい部分

  • プロセス名の自動取得
  • 移動先の動的変更
    • ディスプレイの配置次第で {0, 0} が必ず内蔵ディスプレイの左上になるわけではない

使い方

  1. Automatorのクイックアクションでスクリプトを登録
    1. ワークフローはAppleScriptを実行のみ
    2. スクリプトの中身に上記のスクリプトを記載
    3. で動作確認
      1. Automatorに対してアクセシビリティの許可をするためのダイアログが出るので許可する
    4. 任意の名称で保存
  2. キーボードショートカットでキーを登録
    1. キーボードショートカット -> サービス -> 一般 のところにAutomatorで保存した名前があるので、それに対してショートカットキーを設定する
  3. ショートカットキーを発動させたいアプリケーションに対してアクセシビリティを許可する
    1. ショートカットキーを発動した時のアクティブなアプリケーションに対してアクセシビリティの許可が必要
    2. Finderとかターミナルとかお好みで
    3. 最初の実行時に確認が出るのでこの手順はスキップしても問題なし
2
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
2
1