2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

マルチモニタ環境とシングルモニタ環境を切り替える

Last updated at Posted at 2017-07-12

メインの開発環境として MacBook Pro (13-inch, Early 2011) を使っています。

6 年目になりますが、これまでに

  • メモリ 8GB → 16GB 換装
  • HDD → SSD 480GB 換装
  • バッテリー交換
  • ファン交換

を自前でやっていて、快適に利用できています。

現行 MacBook Pro と比べると Retina ディスプレイじゃない、 Bluetooth 4 未対応、 USB 3 未対応などの劣る部分はありますが、何より自前でバッテリー・HDD・メモリの換装が可能なことで 5 年以上の長期に渡って使い続けることが出来ています。いいマシンです。


自宅の作業机では外部モニタ (LG 24MP76) を使ってマルチディスプレイな環境で作業、それ以外では MacBook Pro 単体のシングルディスプレイな環境での作業が基本です。

自宅内でも作業机以外での作業はシングルモニタなので、それなりの頻度でマルチモニタ環境とシングルモニタ環境の切り替えがあります。

Retina じゃない MacBook Pro 13-inch の画面は 1200 x 800 は流石に手狭に感じることがあり、シングルモニタ環境では常時立ち上げているアプリケーションをフルスクリーンモードで利用するようにしています。

Mac OS X 10.7 Lion から導入されたフルスクリーンモードは、シングルモニタ環境では便利でよく利用します。 macOSでディスプレイ1枚で作業する技術 でも触れられている通り「フルスクリーンモードにするタイミングで並び順が変わる」のがちょっと厄介です。

私のようにマルチ・シングルの切り替えがそこそこある、シングルモニタ環境ではいくつかの特定のアプリケーションをフルスクリーンモードで利用したい ... できればアプリケーションの並び順も固定したいという場合、いちいち並び替えるのは面倒です。

... という訳で、私はマルチモニタ環境とシングルモニタ環境を切り替えるスクリプトを用意してターミナルからのコマンドで切り替えできるようにしています。

on run argv
	if item 1 of argv is "true" then
		set needsToTurnFullscreen to true
	else
		set needsToTurnFullscreen to false
	end if
	set myList to {"Google Chrome", "Reeder", "Slack", "Mail", "Evernote"}
	if needsToTurnFullscreen is not true then
		set myList to reverse of myList
	end if
	repeat with theItem in myList
		if application theItem is running then
			tell application theItem
				activate
				set isfullscreen to false
				tell application "System Events" to tell process theItem
					set gotStatus to false
					set tryCount to 0
					repeat until gotStatus is true
						set tryCount to tryCount + 1
						try
							set isfullscreen to value of attribute "AXFullScreen" of window 1
							set gotStatus to true
						end try
						if tryCount is 45 then
							log "[applescript] failed to get full screen status."
							exit repeat
						end if
						delay 0.1
					end repeat
				end tell
				if isfullscreen is not needsToTurnFullscreen then
					tell application "System Events" to keystroke "f" using {command down, control down}
					delay 1
				end if
			end tell
		end if
	end repeat
	set theApps to {"Terminal", "iTerm2"}
	repeat with theApp in theApps
		if application theApp is running then
			tell application theApp
				activate
				delay 0.5
			end tell
		end if
	end repeat
end run

こんな感じの AppleScript (make-fullscreen.scpt) を用意します。

set myList to {"Google Chrome", "Reeder", "Slack", "Mail", "Evernote"}

が、フルスクリーンモードで動作させたいアプリケーションリストで、基本的にこの順番で並ぶようになります。

このスクリプトに対して、 mf コマンド (make fullscreen の略のつもり) を以下のように alias 設定します。

alias mf="osascript /path/to/make-fullscreen.scpt"

/path/to/ は作成した make-fullscreen.scpt へのパス。

これで

$ mf true

とすれば、リストアップしたアプリケーションがフルスクリーンモードに切り替わって、

$ mf no

とすれば、リストアップしたアプリケーションのフルスクリーンモードを解除します。

ターミナルからのコマンド起動を前提しているので、ターミナルへのフォーカスを維持するため、いずれの場合も最後にターミナル (Terminal もしくは iTerm) にフォーカスを戻します。

... スクリプトによる力技なので、時折うまくいかない場合もありますが、まぁ、九割以上うまくいくので、とりあえず、このまま使っています。

2
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?