Skip to content

Instantly share code, notes, and snippets.

@ckazu
Last active October 19, 2023 06:50
Show Gist options
  • Save ckazu/f7c4f022f33baad7c2a45ea2f79fe557 to your computer and use it in GitHub Desktop.
Save ckazu/f7c4f022f33baad7c2a45ea2f79fe557 to your computer and use it in GitHub Desktop.
Hammerspoon
local left = "DELL U2723QE (1)"
local right = "DELL U2723QE (2)"
local windowLayout1 = {
{"Slack", nil, left, {x=0.0, y=0.0, w=0.5, h=1.0}, nil, nil},
{"Brave Browser", nil, left, {x=0.499, y=0.0, w=0.5, h=1.0}, nil, nil},
}
-- local windowLayout2 = {
-- {"iTerm2", nil, wide34, {x=0.0, y=0.0, w=0.2, h=1.0}, nil, nil},
-- {"Code", nil, wide34, {x=0.199, y=0.0, w=0.4, h=1.0}, nil, nil},
-- {"Brave Browser", nil, wide34, {x=0.599, y=0.0, w=0.4, h=1.0}, nil, nil},
-- }
-- local windowLayout3 = {
-- {"Brave Browser", nil, wide34, hs.layout.left30, nil, nil},
-- {"Code", nil, wide34, hs.layout.right70, nil, nil},
-- }
-- local windowLayout4 = {
-- {"iTerm2", nil, wide34, hs.layout.left30, nil, nil},
-- {"Code", nil, wide34, hs.layout.right70, nil, nil},
-- }
local unhideWindows = function()
hs.application("iTerm2"):unhide()
hs.application("Brave Browser"):unhide()
-- hs.application("Google Chrome"):unhide()
hs.application("Code"):unhide()
hs.application("Finder"):unhide()
hs.application("Bitwarden"):unhide()
end
-- unhideWindows()
-- hs.layout.apply(windowLayout1)
hs.hotkey.bind({"command", "ctrl"}, "1", function()
hs.layout.apply(windowLayout1)
unhideWindows()
end)
hs.hotkey.bind({"command", "ctrl"}, "2", function()
hs.layout.apply(windowLayout2)
unhideWindows()
end)
hs.hotkey.bind({"command", "ctrl"}, "3", function()
hs.layout.apply(windowLayout3)
unhideWindows()
hs.application("iTerm2"):hide()
end)
hs.hotkey.bind({"command", "ctrl"}, "4", function()
hs.layout.apply(windowLayout4)
unhideWindows()
hs.application("Brave Browser"):hide()
end)
hs.hotkey.bind({"command", "ctrl"}, "5", function()
local windows = hs.application.get('Brave Browser'):allWindows()
local chromeWindows = {}
local chromeSize = 0
for i, window in ipairs(windows) do
if window:title() ~= "" then table.insert(chromeWindows, window) end
chromeSize = chromeSize + 1
end
local layout = {}
for i, chrome in ipairs(chromeWindows) do
table.insert(layout, {"Brave Browser", chrome:title(), left, {x=((i -1) / chromeSize), y=0.0, w=(1 / chromeSize), h=1.0}, nil, nil})
end
hs.layout.apply(layout)
unhideWindows()
hs.application("iTerm2"):hide()
hs.application("Code"):hide()
end)
hs.hotkey.bind({"command", "ctrl"}, "6", function()
local windows = hs.application.get('Google Chrome'):allWindows()
local chromeWindows = {}
for i, window in ipairs(windows) do
if window:title() ~= "" then table.insert(chromeWindows, window) end
end
local chromeSize = #chromeWindows -- タイトルがあるウィンドウの数だけをカウント
local layout = {}
for i, chrome in ipairs(chromeWindows) do
table.insert(layout, {"Google Chrome", chrome:title(), right, {x=((i -1) / chromeSize), y=0.0, w=(1 / chromeSize), h=1.0}, nil, nil})
end
hs.layout.apply(layout)
end)
hs.hotkey.bind({"command", "ctrl"}, "7", function()
local windows = hs.application.get('Google Chrome'):allWindows()
local chromeWindows = {}
for i, window in ipairs(windows) do
if window:title() ~= "" then table.insert(chromeWindows, window) end
end
local layout = {}
local numberOfWindows = #chromeWindows
local activeWindow = hs.window.focusedWindow()
-- アクティブなウィンドウをスクリーンの上部に配置(高さは1/3)
table.insert(layout, {"Google Chrome", activeWindow:title(), "current",
{x=0.0, y=0.0, w=1.0, h=1/3}, nil, nil})
-- 残りのウィンドウを下部に等間隔で配置(高さは2/3)
local counter = 0
for i = 1, numberOfWindows do
if chromeWindows[i]:id() ~= activeWindow:id() then
counter = counter + 1
table.insert(layout, {"Google Chrome", chromeWindows[i]:title(), "current",
{x=((counter-1) / (numberOfWindows-1)), y=1/3, w=(1 / (numberOfWindows-1)), h=2/3}, nil, nil})
end
end
hs.layout.apply(layout)
end)
hs.hotkey.bind({"command", "ctrl"}, "Left", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end)
hs.hotkey.bind({"command", "ctrl"}, "Right", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + max.w / 2
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end)
hs.hotkey.bind({"command", "ctrl"}, "Up", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w
f.h = max.h
win:setFrame(f)
end)
hs.hotkey.bind({"command", "ctrl"}, "Down", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + max.w / 6
f.y = max.y
f.w = max.w * 2 / 3
f.h = max.h
win:setFrame(f)
end)
-- 右のディスプレイに移動
hs.hotkey.bind({"cmd", "ctrl", "shift"}, "right", function()
local win = hs.window.focusedWindow()
local screen = win:screen()
local nextScreen = screen:next()
if nextScreen then
win:moveToScreen(nextScreen)
end
end)
-- 左のディスプレイに移動
hs.hotkey.bind({"cmd", "ctrl", "shift"}, "left", function()
local win = hs.window.focusedWindow()
local screen = win:screen()
local prevScreen = screen:previous()
if prevScreen then
win:moveToScreen(prevScreen)
end
end)
switcher = hs.window.switcher.new()
hs.hotkey.bind('alt','tab','Next window',function()switcher:next()end)
hs.hotkey.bind('alt-shift','tab','Prev window',function()switcher:previous()end)
-- cmd + tab で切り替えた後に マウスをアクティブウィンドウの中央に移動させる
local cmdPressed = false
local tabPressed = false
local function moveMouseToActiveWindow()
local win = hs.window.focusedWindow()
if win then
local frame = win:frame()
hs.mouse.absolutePosition({x = frame.x + frame.w / 2, y = frame.y + frame.h / 2})
end
end
_tap = hs.eventtap.new({ hs.eventtap.event.types.keyDown, hs.eventtap.event.types.flagsChanged }, function(e)
local keyCode = e:getKeyCode()
if e:getType() == hs.eventtap.event.types.keyDown then
if keyCode == hs.keycodes.map["tab"] and e:getFlags().cmd then
tabPressed = true
elseif keyCode == hs.keycodes.map["escape"] and cmdPressed then
tabPressed = false -- `esc`キーでキャンセルした場合、フラグをリセット
end
end
if e:getType() == hs.eventtap.event.types.flagsChanged and not e:getFlags().cmd and tabPressed then
hs.timer.doAfter(0.1, function()
moveMouseToActiveWindow()
end)
tabPressed = false
end
return false
end)
_tap:start()
-- reset
hs.hotkey.bind({"command", "ctrl"}, "R", function()
hs.reload()
end)
hs.alert.show("Config loaded")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment