Hammmerspoon - add windowmax toggle hotkey
[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 -- Window Movement
51 -- https://andrich.blog/2016/11/20/hammerspoon-an-awesome-tool-to-automate-your-mac/
52 -- CTRL + ALT + Left - Move current window to the left half of the screen.
53 -- CTRL + ALT + Right - Move current window to the right half of the screen.
54 -- CTRL + ALT + Up - Go "fullscreen".
55 -- CTRL + ALT + Down - Center window, covering 2/3 of screen size.
56 --
57  
58 function move_window(direction)
59     return function()
60         local win      = hs.window.focusedWindow()
61         local app      = win:application()
62         local app_name = app:name()
63         local f        = win:frame()
64         local screen   = win:screen()
65         local max      = screen:frame()
66  
67         if direction == "left" then
68             f.x = max.x
69             f.y = max.y
70             f.w = max.w / 2
71             f.h = max.h
72         elseif direction == "right" then
73             f.x = max.x + (max.w / 2)
74             f.y = max.y
75             f.w = max.w / 2
76             f.h = max.h
77         elseif direction == "up" then
78             f.x = max.x
79             f.y = max.y 
80             f.w = max.w
81             f.h = max.h / 2
82         elseif direction == "down" then
83             f.x = max.x 
84             f.y = max.y + (max.h / 2)
85             f.w = max.w 
86             f.h = max.h / 2
87         elseif direction == "max" then
88            if f.w > (max.w * 3/4) and f.h > (max.h * 3/4) then
89                 f.x = max.x + (max.w / 6)
90                 f.y = max.y + (max.h / 6)
91                 f.w = max.w * 2 / 3
92                 f.h = max.h * 2 / 3
93            else
94                 f.x = max.x 
95                 f.y = max.y
96                 f.w = max.w 
97                 f.h = max.h
98            end
99         else
100             hs.alert.show("move_window(): Freaky parameter received " .. direction)
101         end
102  
103         win:setFrame(f, 0)
104     end
105 end
106  
107 local hyper = {"ctrl", "alt"}
108 hs.hotkey.bind(hyper, "h", move_window("left"))
109 hs.hotkey.bind(hyper, "l", move_window("right"))
110 hs.hotkey.bind(hyper, "k", move_window("up"))
111 hs.hotkey.bind(hyper, "j", move_window("down"))
112 hs.hotkey.bind(hyper, "m", move_window("max"))
113
114 -- Search and dictionary
115 -- Past and copy
116 -- App quick startup
117
118 -- Use SpoonInstall,
119 -- http://zzamboni.org/post/using-spoons-in-hammerspoon/
120 -- https://github.com/Hammerspoon/Spoons/raw/master/Spoons/SpoonInstall.spoon.zip
121 if hs.spoons == nil then
122    hs.spoons=require('hs.spoons')
123 end
124
125
126 hs.loadSpoon ("SpoonInstall")
127 spoon.SpoonInstall.use_syncinstall = true
128 INSTALL = spoon.SpoonInstall
129
130 --------------------------------------------------------------------------------
131 -- MouseCircle  http://www.hammerspoon.org/Spoons/MouseCircle.html
132 INSTALL:andUse("MouseCircle",
133                 {
134                    disable = false,
135                    config = {
136                       color = hs.drawing.color.x11.rebeccapurple
137                    },
138                    hotkeys = {
139                       show = { {"cmd", "alt", "ctrl"}, "m"}
140                    }
141                 }
142 )
143
144 --------------------------------------------------------------------------------
145 -- TextClipboardHistory
146 INSTALL:andUse("TextClipboardHistory",
147                {
148                   config = {
149                      show_in_menubar = false,
150                   },
151                   hotkeys = {
152                      toggle_clipboard = { { "cmd", "shift" }, "v"}
153                   },
154                   start = true;
155                }
156 )
157
158
159 --------------------------------------------------------------------------------
160 -- Seal -- http://www.hammerspoon.org/Spoons/Seal.html
161 INSTALL:andUse("Seal",
162                {
163                   hotkeys = { show = { {"cmd", "ctrl", "alt"}, "space" } },
164                   fn = function(s)
165                      s:loadPlugins({"apps", "calc", "safari_bookmarks", "screencapture", "useractions"})
166                      s.plugins.safari_bookmarks.always_open_with_safari = false
167                      s.plugins.useractions.actions =
168                         {
169                            ["Hammerspoon docs webpage"] = {
170                               url = "http://hammerspoon.org/docs/",
171                               icon = hs.image.imageFromName(hs.image.systemImageNames.ApplicationIcon),
172                               -- hotkey = { hyper, "h" }
173                            },
174                            ["Leave corpnet"] = {
175                               fn = function()
176                                  spoon.WiFiTransitions:processTransition('foo', 'corpnet01')
177                               end,
178                               icon = swisscom_logo,
179                            },
180                            ["Arrive in corpnet"] = {
181                               fn = function()
182                                  spoon.WiFiTransitions:processTransition('corpnet01', 'foo')
183                               end,
184                               icon = swisscom_logo,
185                            },
186                            ["Translate using Leo"] = {
187                               url = "http://dict.leo.org/ende/index_de.html#/search=${query}",
188                               icon = 'favicon',
189                               keyword = "leo",
190                            },
191                            ["Tell me something"] = {
192                               keyword = "tellme",
193                               fn = function(str) hs.alert.show(str) end,
194                            }
195                         }
196                      s:refreshAllCommands()
197                   end,
198                   start = true,
199                }
200 )