Skip to content

Instantly share code, notes, and snippets.

@banhill
Created April 18, 2018 09:31
Show Gist options
  • Save banhill/6e548eca3a31f3da25f4996b41384318 to your computer and use it in GitHub Desktop.
Save banhill/6e548eca3a31f3da25f4996b41384318 to your computer and use it in GitHub Desktop.
hammerspoon init
local mash = {
split = {"ctrl", "alt", "cmd"},
corner = {"ctrl", "alt", "shift"},
focus = {"ctrl", "alt"}
}
-- Resize windows
local function adjust(x, y, w, h)
return function()
local win = hs.window.focusedWindow()
if not win then return end
local f = win:frame()
local max = win:screen():frame()
f.w = math.floor(max.w * w)
f.h = math.floor(max.h * h)
f.x = math.floor((max.w * x) + max.x)
f.y = math.floor((max.h * y) + max.y)
win:setFrame(f)
end
end
-- top half
hs.hotkey.bind(mash.split, "I", adjust(0, 0, 1, 0.5))
-- -- right half
hs.hotkey.bind(mash.split, "L", adjust(0.5, 0, 0.5, 1))
-- -- bottom half
hs.hotkey.bind(mash.split, "K", adjust(0, 0.5, 1, 0.5))
-- -- left half
hs.hotkey.bind(mash.split, "J", adjust(0, 0, 0.5, 1))
-- -- top left
hs.hotkey.bind(mash.corner, "I", adjust(0, 0, 0.5, 0.5))
-- -- top right
hs.hotkey.bind(mash.corner, "L", adjust(0.5, 0, 0.5, 0.5))
-- -- bottom right
hs.hotkey.bind(mash.corner, "K", adjust(0.5, 0.5, 0.5, 0.5))
-- -- bottom left
hs.hotkey.bind(mash.corner, "J", adjust(0, 0.5, 0.5, 0.5))
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
hs.reload()
end)
hs.alert.show("Config loaded")
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "M", function()
local win = hs.window.focusedWindow()
win:maximize()
end)
-- hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Y", function()
-- hs.execute("open " .. "youtube.com")
-- end)
--Bring focus to next display/screen
hs.hotkey.bind({"alt"}, "`", function ()
focusScreen(hs.window.focusedWindow():screen():next())
end)
-- hs.hotkey.bind({"alt", "shift"}, "I", function ()
-- focusScreen(hs.window.focusedWindow():screen():toNorth())
-- end)
-- hs.hotkey.bind({"alt", "shift"}, "K", function ()
-- focusScreen(hs.window.focusedWindow():screen():toSouth())
-- end)
-- hs.hotkey.bind({"alt", "shift"}, "J", function ()
-- focusScreen(hs.window.focusedWindow():screen():toWest())
-- end)
-- hs.hotkey.bind({"alt", "shift"}, "L", function ()
-- focusScreen(hs.window.focusedWindow():screen():toEast())
-- end)
--Bring focus to previous display/screen
hs.hotkey.bind({"alt", "shift"}, "`", function()
focusScreen(hs.window.focusedWindow():screen():previous())
end)
--Predicate that checks if a window belongs to a screen
function isInScreen(screen, win)
return win:screen() == screen
end
-- Brings focus to the screen by setting focus on the front-most application in it.
-- Also move the mouse cursor to the center of the screen. This is because
-- Mission Control gestures & keyboard shortcuts are anchored, oddly, on where the
-- mouse is focused.
function focusScreen(screen)
--Get windows within screen, ordered from front to back.
--If no windows exist, bring focus to desktop. Otherwise, set focus on
--front-most application window.
local windows = hs.fnutils.filter(
hs.window.orderedWindows(),
hs.fnutils.partial(isInScreen, screen))
local windowToFocus = #windows > 0 and windows[1] or hs.window.desktop()
windowToFocus:focus()
-- Move mouse to center of screen
local pt = geometry.rectMidPoint(screen:fullFrame())
mouse.setAbsolutePosition(pt)
end
-- hs.hotkey.bind({"cmd"}, "J", function()
-- local app = hs.application.frontmostApplication()
-- if app:name() == "Mail" then
-- hs.eventtap.keyStroke({}, "down")
-- end
-- end)
hs.hotkey.bind({"cmd"}, "K", function()
local app = hs.application.frontmostApplication()
if app:name() == "Mail" then
hs.eventtap.keyStroke({}, "up")
end
end)
local function appl(appName)
return function()
hs.application.launchOrFocus(appName)
end
end
hs.hotkey.bind(mash.focus, "H", appl("HipChat"))
hs.hotkey.bind(mash.focus, "C", appl("Google Chrome"))
hs.hotkey.bind(mash.focus, "I", appl("iTerm"))
hs.hotkey.bind(mash.focus, "M", appl("Mail"))
-- hs.hotkey.bind({"cmd", "alt", "ctrl"}, "H", 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({"cmd", "alt", "ctrl"}, "J", 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 + (max.h / 2)
-- f.w = max.w
-- f.h = max.h / 2
-- win:setFrame(f)
-- end)
--
-- hs.hotkey.bind({"cmd", "alt", "ctrl"}, "K", 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 / 2
-- win:setFrame(f)
-- end)
--
-- hs.hotkey.bind({"cmd", "alt", "ctrl"}, "L", 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({"shift", "cmd", "alt", "ctrl"}, "J", function()
local win = hs.window.focusedWindow()
local nextScreen = win:screen():previous()
win:moveToScreen(nextScreen)
end)
hs.hotkey.bind({"shift", "cmd", "alt", "ctrl"}, "L", function()
local win = hs.window.focusedWindow()
local nextScreen = win:screen():next()
win:moveToScreen(nextScreen)
end)
hs.hotkey.bind({"shift", "cmd", "alt", "ctrl"}, "R", function()
win:rotate(90)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment