2
1

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 1 year has passed since last update.

【備忘録】HammerSpoonにウィンドウ操作設定をちょっとだけした話

Posted at

やりたかったこと

HammerSpoonで下記

  • ShiftItで行なっている画面分割
  • Windowsチックなタブ移動
  • マウスを触らずにウィンドウを画面移動
  • etc..

HammerSpoonをインストール

HammerspoonをHomebrew経由でインストール。

brew install --cask hammerspoon

ShiftIt Spoonをインストール

ShiftIt spoonをダウンロード。

画面分割の設定

  • loadSpoonShiftIt Spoonのロードを行う。
  • setWindowCyclingSizesで複数回ショートカットキーを押した時の画面サイズを指定できる。
    • 第一引数:横サイズのサイクルを指定
      • {50,33,25}であれば1回目:50%、2回目:33%、3回目:25%のサイズとなる
    • 第二引数:縦サイズのサイクルを指定
      • {50} であれば何回押しても50%のサイズとなる。
  • bindHotkeysにHotkey設定を行うことで特定のショートカットキーを選択できる
    • 例:left = { { 'ctrl', 'shift' }, 'a' }:ctrlキー, shiftキー、aキーの同時押しで左寄せ
init.lua
 hs.loadSpoon("ShiftIt")
 spoon.ShiftIt:setWindowCyclingSizes({ 50, 33, 25 }, { 50 })
 spoon.ShiftIt:bindHotkeys({
    left = { { 'ctrl', 'shift' }, 'a' },
    right = { { 'ctrl', 'shift' }, 'd' },
    up = { { 'ctrl', 'shift' }, 'w' },
    down = { { 'ctrl', 'shift' }, 'x' },
    upleft = { { 'ctrl', 'shift' }, 'q' },
    upright = { { 'ctrl', 'shift' }, 'e' },
    botleft = { { 'ctrl', 'shift' }, 'z' },
    botright = { { 'ctrl', 'shift' }, 'c' },
    maximum = { { 'ctrl', 'shift' }, 'm' },
    center = { { 'ctrl', 'shift' }, 's' }
 })

ウィンドウ切り替えの設定

  • HammerSpoonに標準装備されているswitcherを使用して切り替えが行える。
  • switcher.uiに設定を行うことで画面表示を変えることができる。
  • ウィンドウ移動をHotkeyに設定
    • next()で次のウィンドウへ移動
    • previous()で前のウィンドウへ移動
init.lua
 switcher = hs.window.switcher.new()
 switcher.ui.showTitles =false
 switcher.ui.showExtraKeys = false
 switcher.ui.thumbnailSize = 200
 switcher.ui.showSelectedThumbnail = false
 switcher.ui.backgroundColor = {0, 0, 0, 0.8}
 switcher.ui.highlightColor = {0.3, 0.3, 0.3, 0.8}
 hs.hotkey.bind('option', 'k', function()switcher:next()end)
 hs.hotkey.bind('option', 'j', function()switcher:previous()end)

デスクトップ移動

  • hs.window.focusedWindow()フォーカスしているウィンドウを取得
  • 画面サイズが変わることを考慮してウィンドウのサイズを変更する必要がある。。
    • サイズ情報を取得
      • app:frame():フォーカスしているウィンドウのサイズ
      • app:screen():frame():現在の画面のサイズ
      • app:screen():next():frame():移動さきの画面のサイズ
  • app:moveToScreen(app:screen():next()):次の画面へウィンドウを移動する。(2画面しか使わないから切り替えているだけ。。)
  • HotKey設定を施し完了!!
init.lua
 function moveToChangeScreen()
   local app = hs.window.focusedWindow()
   local appSize = app:frame()
   local focusScreenSize = app:screen():frame()
   local changeScreenSize = app:screen():next():frame()
   
   app:moveToScreen(app:screen():next())
   appSize.x = ((((appSize.x - focusScreenSize.x) / focusScreenSize.w) * changeScreenSize.w) + changeScreenFrame.x)
   appSize.y = ((((appSize.y - focusScreenSize.y) / focusScreenSize.h) * changeScreenSize.h) + changeScreenFrame.y)
   appSize.h = ((appSize.h / focusScreenSize.h) * changeScreenSize.h)
   appSize.w = ((appSize.w / focusScreenSize.w) * changeScreenSize.w)
 end
 hs.hotkey.bind({"ctrl", "shift"}, "1", moveToChangeScreen)

さいごに

設定をReloadして使えるようになるで!!!
もっと良い設定があれば追記予定。。。。。。

参考文献

https://www.hammerspoon.org/docs/index.html
https://kawase-k.hatenablog.com/entry/2022/02/19/070000
https://blog.apiless.com/2023/02/02/mac-hammerspoon-switcher/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?