MacでDockにしまったWindowを復元するショートカットを作った

Cmd + M で「しまう」のショートカットがMacでは共通してあるわけだが、逆に戻すショートカットが無い。

ゴテゴテとウィンドウが重なっている状態があまり好きじゃなくて、都度「しまう」を行っていたりするわけだけど、Cmd + Tab でアプリケーションを切り替えた後、元に戻すのにマウスポインタを動かす必要が出てきて QoL が下がっていた。

ということで、ショートカットを作ろうと努力をしてみたよ。(完全ではないけど概ね動く)

やったこと

  1. AutomatorAppleScript を動かすサービスを作る
  2. システム環境設定 -> キーボード -> ショートカット の「サービス」からショートカットを割り当てる

Automator で AppleScipt

起動

種類を聞かれるので、「サービス」を選ぶ

AppleScript を書く

ユーティリティにある「AppleScriptを実行」を右側にドラッグ&ドロップしてコードを書く。

AppleScript
on run {input, parameters}
  tell application "System Events"
    set frontApplication to a reference to (processes whose visible is true and frontmost is true)
    if (count frontApplication) is 0 then return
    set appId to bundle identifier of (item 1 of frontApplication)
  end tell
  
  tell application id appId
    set hiddenWindows to a reference to (windows whose miniaturizable is true and miniaturized is true)
    try
      if (count hiddenWindows) is not 0 then
        set miniaturized of hiddenWindows to false
      end if
    on error msg number n
      activate
      display dialog msg
    end try
  end tell
  return input
end run
やっていること
  1. System Events から最も全面にあるアプリケーションのidを得る
  2. 得たidのアプリケーションの内、「しまう」ことができるもの、かつ、しまわれた状態のWindowリストを得る
  3. そのWindowリストに対して、miniaturized を false に設定する(しまわれた状態を元に戻す)

しまわれたウィンドウを一つ一つ戻すこともできるだろうけど、そのアプリケーションのは全て戻すほうが使い勝手が良いかな(?)と思ってそうしている。

ショートカット登録

システム環境設定 -> キーボード -> ショートカットを開く
左パネルの「サービス」にAutomatorで作成したものがあるので、それに対してショートカットを割り当てる


:wq