Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hammerspoon config
brew tap homebrew/versions
brew install lua53
## Don't remember now whether local works, possibly need to install via sudo
luarocks-5.3 install --local set
# sudo luarocks-5.3 install set
---- Definitions
-- Set objects
local Set = require("Set")
-- Reload config
function reload()
hs.alert('Reloading Config')
hs.reload()
end
-- Launch App
launch = function(appname)
hs.alert(appname)
hs.application.launchOrFocus(appname)
k.triggered = true
end
-- Show window picker
expose = hs.expose.new()
-- set up your windowfilter
-- default windowfilter: only visible windows, all Spaces
-- expose2 = hs.expose.new(hs.window.filter.new():trackSpaces(true):setDefaultFilter()) -- include minimized/hidden windows, current Space only
-- expose_browsers = hs.expose.new{'Safari','Google Chrome'} -- specialized expose for your dozens of browser windows :)
function toggle_expose ()
expose:toggleShow()
end
-- Window Movement --
function LeftHalf ()
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
function RightHalf ()
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
function UpperHalf ()
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
function LowerHalf ()
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
function Maximize ()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:fullFrame()
win:setFrame(max)
end
function Rotate ()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
if (f.w > max.w - 10) then -- Window is maximized
LeftHalf()
elseif (f.x > max.x - 10) and (f.x < max.x + 10)then -- Window is on the left
RightHalf()
else
win:maximize()
end
end
function move_win_to_next_screen ()
local win = hs.window.focusedWindow()
local screen = win:screen()
local nxt = screen:next()
win:moveToScreen(nxt)
end
-- Paste!
function paste_keystrokes ()
hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end
function list_windows ()
local apps = hs.application.runningApplications()
local app_set = Set:new(apps)
for n,app in pairs(app_set:tolist()) do
for m,win in pairs(app:allWindows(app)) do
local app_title = app:title()
local win_title = win:title()
if win_title ~= "" then
hs.alert(app_title .. " | " .. win_title, 1)
end
end
end
end
------ Bootstrap ------
-- Package manager init
require("luarocks.loader")
-- Load our functions
require("functions")
-- Diable Animations
hs.window.animationDuration = 0
----- Config -----
-- Meaning of Hyper
full_hyper = {"cmd", "alt", "ctrl"}
slim_hyper = {"cmd", "alt"}
-- Just in case, have a simple reloader.
hs.hotkey.bind(slim_hyper, "R", reload)
---- Config for the actual 'Hyper', i.e. F18, i.e. caps lock.
-- App Shortcuts
app_keys = {
E = "Emacs";
F = "Finder";
C = "Google Chrome";
M = "Mathematica";
A = "Evernote";
T = "Todoist";
K = "Skim";
}
-- Function running shortcuts
func_keys = {
{'Left', LeftHalf},
{'Right', RightHalf},
{'Up', UpperHalf},
{'Down', LowerHalf},
{'X', Maximize},
{'R', reload},
{'L', hs.caffeinate.lockScreen},
{'V', paste_keystrokes},
{'Return', move_win_to_next_screen},
{';', list_windows},
{'Z', toggle_expose}
}
-- Shortcuts sending fake hyper
full_hyper_keys = {'s', 'p', '`', 'f1', 'f2'}
slim_hyper_keys = {'d'}
full_hyper_keys_exit = {{'Tab', {{},'Return'}}}
require("modal")
hs.alert('Config loaded')
---- Definitions
-- Set objects
local Set = require("Set")
-- Reload config
function reload()
hs.alert('Reloading Config')
hs.reload()
end
-- Launch App
launch = function(appname)
hs.alert(appname)
hs.application.launchOrFocus(appname)
k.triggered = true
end
-- Show window picker
expose = hs.expose.new()
-- set up your windowfilter
-- default windowfilter: only visible windows, all Spaces
-- expose2 = hs.expose.new(hs.window.filter.new():trackSpaces(true):setDefaultFilter()) -- include minimized/hidden windows, current Space only
-- expose_browsers = hs.expose.new{'Safari','Google Chrome'} -- specialized expose for your dozens of browser windows :)
function toggle_expose ()
expose:toggleShow()
end
-- Window Movement --
function LeftHalf ()
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
function RightHalf ()
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
function UpperHalf ()
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
function LowerHalf ()
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
function Maximize ()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:fullFrame()
win:setFrame(max)
end
function Rotate ()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
if (f.w > max.w - 10) then -- Window is maximized
LeftHalf()
elseif (f.x > max.x - 10) and (f.x < max.x + 10)then -- Window is on the left
RightHalf()
else
win:maximize()
end
end
function move_win_to_next_screen ()
local win = hs.window.focusedWindow()
local screen = win:screen()
local nxt = screen:next()
win:moveToScreen(nxt)
end
-- Paste!
function paste_keystrokes ()
hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end
function list_windows ()
local apps = hs.application.runningApplications()
local app_set = Set:new(apps)
for n,app in pairs(app_set:tolist()) do
for m,win in pairs(app:allWindows(app)) do
local app_title = app:title()
local win_title = win:title()
if win_title ~= "" then
hs.alert(app_title .. " | " .. win_title, 1)
end
end
end
end
@vjpr

This comment has been minimized.

Copy link

@vjpr vjpr commented Nov 3, 2016

See Hammerspoon/hammerspoon#363 (comment) for how to get luarocks.loader as used in this gist working.

@sprig

This comment has been minimized.

Copy link
Owner Author

@sprig sprig commented Nov 6, 2016

Thanks @vjpr - I added the setup part to this gist now :-)

Note in particular that symlinking as in the comment is not necessary, turns out.

@technicalpickles

This comment has been minimized.

Copy link

@technicalpickles technicalpickles commented Dec 8, 2020

The homebrew/versions tap was deprecated at some point. To do the same now:

brew install lua@5.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment