2386e5820e3dd3ad6fb34a350a35791a1347bc90
[dotfiles.git] / hammerspoon / init.lua
1 -- Sample Hello World 
2 hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function()
3   hs.notify.new({title="Hammerspoon", informativeText="Hello World"}):send()
4 --  hs.alert.show("Hello World!")
5 end)
6
7 -- Reload configuration
8 hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
9   hs.reload()
10 end)
11 hs.alert.show("Config loaded")
12
13
14 -- Sample - Drawing on the screen - http://www.hammerspoon.org/go/#simplereload
15 mouseCircle = nil
16 mouseCircleTimer = nil
17
18 function mouseHighlight()
19     -- Delete an existing highlight if it exists
20     if mouseCircle then
21         mouseCircle:delete()
22         if mouseCircleTimer then
23             mouseCircleTimer:stop()
24         end
25     end
26     -- Get the current co-ordinates of the mouse pointer
27     mousepoint = hs.mouse.getAbsolutePosition()
28     -- Prepare a big red circle around the mouse pointer
29     mouseCircle = hs.drawing.circle(hs.geometry.rect(mousepoint.x-40, mousepoint.y-40, 80, 80))
30     mouseCircle:setStrokeColor({["red"]=1,["blue"]=0,["green"]=0,["alpha"]=1})
31     mouseCircle:setFill(false)
32     mouseCircle:setStrokeWidth(5)
33     mouseCircle:show()
34
35     -- Set a timer to delete the circle after 3 seconds
36     mouseCircleTimer = hs.timer.doAfter(3, function() mouseCircle:delete() end)
37 end
38 hs.hotkey.bind({"cmd","alt","shift"}, "D", mouseHighlight)
39
40
41 -- window movement
42 hs.hotkey.bind({"cmd", "alt", "ctrl"}, "H", function()
43   local win = hs.window.focusedWindow()
44   local f = win:frame()
45
46   f.x = f.x - 10
47   win:setFrame(f)
48 end)
49
50 ------ multi-window layout
51 --local laptopScreen = "Color LCD"
52 --local windowLayout = {
53 --    {"Safari",  nil,          laptopScreen, hs.layout.left50,    nil, nil},
54 --    {"Mail",    nil,          laptopScreen, hs.layout.right50,   nil, nil},
55 --    {"iTunes",  "iTunes",     laptopScreen, hs.layout.maximized, nil, nil},
56 --    {"iTunes",  "MiniPlayer", laptopScreen, nil, nil, hs.geometry.rect(0, -48, 400, 48)},
57 --}
58 --hs.layout.apply(windowLayout)
59
60 ---- Creating a simple menubar item
61 --caffeine = hs.menubar.new()
62 --function setCaffeineDisplay(state)
63 --    if state then
64 --        caffeine:setTitle("AWAKE")
65 --    else
66 --        caffeine:setTitle("SLEEPY")
67 --    end
68 --end
69 --
70 --function caffeineClicked()
71 --    setCaffeineDisplay(hs.caffeinate.toggle("displayIdle"))
72 --end
73 --
74 --if caffeine then
75 --    caffeine:setClickCallback(caffeineClicked)
76 --    setCaffeineDisplay(hs.caffeinate.get("displayIdle"))
77 --end
78
79 -- Reacting to application events 
80 -- wheny active the Finder applications, all of its windows
81 -- will be brought to the front of the display.
82 function applicationWatcher(appName, eventType, appObject)
83     if (eventType == hs.application.watcher.activated) then
84         if (appName == "Finder") then
85             -- Bring all Finder windows forward when one gets activated
86             appObject:selectMenuItem({"Window", "Bring All to Front"})
87         end
88     end
89 end
90 appWatcher = hs.application.watcher.new(applicationWatcher)
91 appWatcher:start()
92
93
94
95
96 --
97 -- Window Movement
98 -- https://andrich.blog/2016/11/20/hammerspoon-an-awesome-tool-to-automate-your-mac/
99 -- CTRL + ALT + Left - Move current window to the left half of the screen.
100 -- CTRL + ALT + Right - Move current window to the right half of the screen.
101 -- CTRL + ALT + Up - Go "fullscreen".
102 -- CTRL + ALT + Down - Center window, covering 2/3 of screen size.
103 --
104  
105 function move_window(direction)
106     return function()
107         local win      = hs.window.focusedWindow()
108         local app      = win:application()
109         local app_name = app:name()
110         local f        = win:frame()
111         local screen   = win:screen()
112         local max      = screen:frame()
113  
114         if direction == "left" then
115             f.x = max.x
116             f.y = max.y
117             f.w = max.w / 2
118             f.h = max.h
119         elseif direction == "right" then
120             f.x = max.x + (max.w / 2)
121             f.y = max.y
122             f.w = max.w / 2
123             f.h = max.h
124         elseif direction == "up" then
125             f.x = max.x
126             f.y = max.y 
127             f.w = max.w
128             f.h = max.h / 2
129         elseif direction == "down" then
130             f.x = max.x 
131             f.y = max.y + (max.h / 2)
132             f.w = max.w 
133             f.h = max.h / 2
134         elseif direction == "max" then
135             f.x = max.x 
136             f.y = max.y
137             f.w = max.w 
138             f.h = max.h
139         elseif direction == "center" then
140             f.x = max.x + (max.w / 6)
141             f.y = max.y + (max.h / 6)
142             f.w = max.w * 2 / 3
143             f.h = max.h * 2 / 3
144         else
145             hs.alert.show("move_window(): Freaky parameter received " .. direction)
146         end
147  
148         win:setFrame(f, 0)
149     end
150 end
151  
152 local hyper = {"ctrl", "alt"}
153 hs.hotkey.bind(hyper, "h", move_window("left"))
154 hs.hotkey.bind(hyper, "l", move_window("right"))
155 hs.hotkey.bind(hyper, "k", move_window("up"))
156 hs.hotkey.bind(hyper, "j", move_window("down"))
157 hs.hotkey.bind(hyper, "m", move_window("max"))
158 hs.hotkey.bind(hyper, "c", move_window("center"))