🌇

プレビューの「すべてのウインドウを結合」を自動化する

2023/01/04に公開

日々の手作業が面倒だったので自動化の方法を調べた
今後、コピペじゃ動かない場合もあるだろうから応用が効くように手順を重視した

雛形を用意する

foo.scpt
#!/usr/bin/osascript
tell application "Preview"
  activate
end tell

これでプレビューが起動してアクティブになったら次へ
以降はこのスクリプトの中で試行錯誤していく

その前にコードに色付けする

M-x package-list-packages から applescript-mode をインストールする
これで scpt 拡張子に反応してコードがカラフルになる
それよりも M-; で正確にコメントトグルが効くようになるのがありがたい

メニューバーの項目を確認する

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview" to tell menu bar 1
    get name of menu bar items
  end tell
end tell

実行するとメニューバーの項目がわかる

Apple, プレビュー, ファイル, 編集, 表示, 移動, ツール, ウインドウ, ヘルプ

menu bar 1 の 1 がなんのことかわからないがここはお約束っぽい
メニューバーが複数あったとき用?

正確かつ着実に項目を選択する

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview" to tell menu bar 1
    get name of menu bar items
    get name of menu bar item "ウインドウ"
  end tell
end tell

実行すると指定した名前が表示される

ウインドウ

"ウインドウ" の部分は必ず一つ前で表示させた項目リストの中からコピペしなければならない
なぜなら自分はこの後で直接「ウィンドウ」と書いてしまいはまったから

「ウインドウ」の中に降りる

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview" to tell menu bar 1
    get name of menu bar items
    get name of menu bar item "ウインドウ"
    tell menu bar item "ウインドウ" to tell menu 1
      get name of menu items
    end tell
  end tell
end tell

実行するとウインドウ内の項目一覧がわかる

しまう, すべてしまう, 拡大/縮小, すべてを拡大/縮小, ウインドウを画面左側にタイル表示, ウインドウを画面左側に移動, ウインドウを画面右側にタイル表示, ウインドウを画面右側に移動, タイル表示されたウインドウを置き換える, missing value, 前のタブを表示, 次のタブを表示, タブを新しいウインドウに移動, すべてのウインドウを結合, missing value, missing value, すべてを手前に移動, ウインドウを整理, missing value, 0.pdf (1 / 9 ページ), 1.pdf (1 / 12 ページ), 2.pdf (1 / 8 ページ), 3.pdf (1 / 4 ページ)

menu 1 の 1 が謎だが考えないことにする
ちなみに get なんとかは最後に実行したものしか表示してくれないらしい

ここでも着実に項目を選択する

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview" to tell menu bar 1
    get name of menu bar items
    get name of menu bar item "ウインドウ"
    tell menu bar item "ウインドウ" to tell menu 1
      get name of menu items
      get name of menu item "すべてのウインドウを結合"
    end tell
  end tell
end tell

次のように表示されたら次へ

すべてのウインドウを結合

"すべてのウインドウを結合" は必ず一つ上で表示した項目一覧の中からコピペすること
自分はウインドウをウィンドウと書いてしまい……

やっと完成

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview" to tell menu bar 1
    get name of menu bar items
    get name of menu bar item "ウインドウ"
    tell menu bar item "ウインドウ" to tell menu 1
      get name of menu items
      get name of menu item "すべてのウインドウを結合"
      click menu item "すべてのウインドウを結合"
    end tell
  end tell
end tell

リファクタリング

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview" to tell menu bar 1
    tell menu bar item "ウインドウ" to tell menu 1
      click menu item "すべてのウインドウを結合"
    end tell
  end tell
end tell

get name なんとかの部分は確認用なので取ってもいい

click menu item は一行で書ける

tell application "Preview"
  activate
  tell application "System Events" to tell process "Preview"
    click menu item "すべてのウインドウを結合" of menu "ウインドウ" of menu bar 1
  end tell
end tell

この辺がよくわかってないのだけど試行錯誤していたら
click menu item "B" of menu "A" of menu bar 1
のシンタックスというか文法で A の B を選択する処理になった

ネストをやめる

tell application "Preview"
  activate
end tell

tell application "System Events" to tell process "Preview"
  click menu item "すべてのウインドウを結合" of menu "ウインドウ" of menu bar 1
end tell

別々に書いてもいいらしい

ブロックをやめる

tell application "Preview" to activate
tell application "System Events" to tell process "Preview" to click menu item "すべてのウインドウを結合" of menu "ウインドウ" of menu bar 1

ブロック内が一行なら to を前置して一行にできるらしい

スクリプトをやめる

osascript -e 'tell application "Preview" to activate' -e 'tell application "System Events" to tell process "Preview" to click menu item "すべてのウインドウを結合" of menu "ウインドウ" of menu bar 1'

やめなくてもいいけど -e を連続して書けば一つのコマンドして実行できることがわかった

ついでに必要だった他の自動化操作

最大化する

osascript -e '
tell application "Finder"
  set displayBounds to bounds of window of desktop
  set bounds of window 1 of application "Preview" to displayBounds
end tell
'

なぜ Finder が出てくるのかわからないがそれ経由でデスクトップのサイズが取れる
コマンドラインでもこうやれば複数行書けることもわかった

タブをすべて閉じる

osascript -e 'tell app "Preview" to close (every window)'

これをやらないとタブがどんどん溜まっていく
Previewを終了させてもタブが生き返る

終了する

osascript -e 'tell app "Preview" to quit'

すべてのタブを閉じてから終了させるとなおよし

たいへん参考になったサイト

https://www.web-cte.co.jp/kozakuralab/?p=518
https://allabout.co.jp/gm/gc/2972/

Discussion