Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
-- File Location: ~/.hammerspoon/init.lua --
-- DISPLAY FOCUS SWITCHING --
--One hotkey should just suffice for dual-display setups as it will naturally
--cycle through both.
--A second hotkey to reverse the direction of the focus-shift would be handy
--for setups with 3 or more displays.
--Bring focus to next display/screen
hs.hotkey.bind({"alt"}, "`", function ()
focusScreen(hs.window.focusedWindow():screen():next())
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 scren 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
-- END DISPLAY FOCUS SWITCHING --
-- WORK-RELATED AUTOMATION --
-- Open Zenefits Dashboard once connected to WiFi network at work.
local workWifi = "Valtech"
local employeeDashboardUrl = "https://secure.zenefits.com/dashboard/#/"
local defaultBrowser = "Google Chrome"
hs.wifi.watcher.new(function ()
local currentWifi = wifi.currentNetwork()
-- short-circuit if disconnecting
if not currentWifi then return end
local note = hs.notify.new({
title="Connected to WiFi",
informativeText="Now connected to " .. currentWifi
}):send()
--Dismiss notification in 3 seconds
--Notification does not auto-withdraw if Hammerspoon is set to use "Alerts"
--in System Preferences > Notifications
hs.timer.doAfter(3, function ()
note:withdraw()
note = nil
end)
if currentWifi == workWifi then
-- Allowance for internet connectivity delays.
hs.timer.doAfter(3, function ()
-- @todo: Explore possibilities of using `hs.webview`
hs.execute("open " .. employeeDashboardUrl)
--Make notification clickable. Browser window will be focused on click:
hs.notify.new(function ()
hs.application.launchOrFocus(defaultBrowser)
end, {title="Make sure you clock in!"}):send()
end)
end
end):start()
-- END WORK-RELATED AUTOMATION --
-- RAMEN TIMER --
--Schedule a notification in 15 minutes.
function startRamenTimer()
hs.timer.doAfter(15 * 60, function ()
hs.notify.new({
title="Ramen time!",
informativeText="Your ramen is ready!"
}):send()
end)
hs.alert(" Ramen timer started! ")
end
--Bind timer to `hammerspoon://ramentime`:
hs.urlevent.bind("ramentime", startRamenTimer)
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'r', startRamenTimer)
-- END RAMEN TIMER --
--
--
-- https://spinscale.de/posts/2016-11-08-creating-a-productive-osx-environment-hammerspoon.html --
--
--
--[[ function factory that takes the multipliers of screen width
and height to produce the window's x pos, y pos, width, and height ]]
function baseMove(x, y, w, h)
return function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
-- add max.x so it stays on the same screen, works with my second screen
f.x = max.w * x + max.x
f.y = max.h * y
f.w = max.w * w
f.h = max.h * h
win:setFrame(f, 0)
end
end
-- feature spectacle/another window sizing apps
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'Left', baseMove(0, 0, 0.5, 1))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'Right', baseMove(0.5, 0, 0.5, 1))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'Down', baseMove(0, 0.5, 1, 0.5))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'Up', baseMove(0, 0, 1, 0.5))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, '1', baseMove(0, 0, 0.5, 0.5))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, '2', baseMove(0.5, 0, 0.5, 0.5))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, '3', baseMove(0, 0.5, 0.5, 0.5))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, '4', baseMove(0.5, 0.5, 0.5, 0.5))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'M', hs.grid.maximizeWindow)
-- lock screen shortcut
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'L', function() hs.caffeinate.startScreensaver() end)
-- quick jump to important applications
hs.grid.setMargins({0, 0})
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'J', function () hs.application.launchOrFocus("Intellij IDEA") end)
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'C', function () hs.application.launchOrFocus("Google Chrome") end)
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'S', function () hs.application.launchOrFocus("Slack") end)
-- even though the app is named iTerm2, iterm is the correct name
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'K', function () hs.application.launchOrFocus("iTerm") end)
-- reload config
-- hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
-- hs.reload()
-- hs.notify.new({title="Hammerspoon config reloaded", informativeText="Manually via keyboard shortcut"}):send()
-- end)
-- https://gist.github.com/josephholsten/1e17c7418d9d8ec0e783
-- http://thume.ca/2016/07/16/advanced-hackery-with-the-hammerspoon-window-manager/
-- http://thume.ca/howto/2012/11/19/using-slate/#switching-windows
-- https://github.com/williambout/paper-for-mac
-- https://aaronlasseigne.com/2016/02/16/switching-from-slate-to-hammerspoon/
-- https://maxbittker.com/first-order-retrievability/
-- https://spinscale.de/posts/2016-11-08-creating-a-productive-osx-environment-hammerspoon.html
-- https://simonduff.net/hammerspoon/
-- https://github.com/sduff/dotfiles/tree/master/.hammerspoon
-- http://lowne.github.io/hammerspoon-extensions/
-- https://gist.github.com/josephholsten/1e17c7418d9d8ec0e783
-- https://github.com/Hammerspoon/hammerspoon/wiki/ztomer's-init.lua
-- hs.loadSpoon("ReloadConfiguration")
-- spoon.ReloadConfiguration:start()
-- http://lowne.github.io/hammerspoon-extensions/
local HYPER={'ctrl','alt','cmd','shift'}
local function bind(mod,key,fn,msg,dur)
hs.hotkey.bind(mod,key,function()
fn() hs.alert(msg,dur or 2)
end)
end
-- hs.grid.setGrid'5x4'
-- bind(HYPER,'r',hs.grids.show,'move/resize window')
-- http://www.hammerspoon.org/docs/hs.grid.html
hs.grid.setGrid'8x4'
hs.grid.ui.textSize = 100
bind(HYPER,'g',hs.grid.show,'move/resize window')
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'g', hs.grid.show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.