hammerspoon and karabiner Change 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 -- Reacting to application events
14 -- Move all Finder in front
15 function applicationWatcher(appName, eventType, appObject)
16     if (eventType == hs.application.watcher.activated) then
17         if (appName == "Finder") then
18             -- Bring all Finder windows forward when one gets activated
19             appObject:selectMenuItem({"Window", "Bring All to Front"})
20         end
21     end
22 end
23 appWatcher = hs.application.watcher.new(applicationWatcher)
24 appWatcher:start()
25
26
27
28 ---- Sample - Drawing on the screen - http://www.hammerspoon.org/go/#simplereload
29 --mouseCircle = nil
30 --mouseCircleTimer = nil
31 --function mouseHighlight()
32 --    -- Delete an existing highlight if it exists
33 --    if mouseCircle then
34 --        mouseCircle:delete()
35 --        if mouseCircleTimer then
36 --            mouseCircleTimer:stop()
37 --        end
38 --    end
39 --    -- Get the current co-ordinates of the mouse pointer
40 --    mousepoint = hs.mouse.getAbsolutePosition()
41 --    -- Prepare a big red circle around the mouse pointer
42 --    mouseCircle = hs.drawing.circle(hs.geometry.rect(mousepoint.x-40, mousepoint.y-40, 80, 80))
43 --    mouseCircle:setStrokeColor({["red"]=1,["blue"]=0,["green"]=0,["alpha"]=1})
44 --    mouseCircle:setFill(false)
45 --    mouseCircle:setStrokeWidth(5)
46 --    mouseCircle:show()
47 --
48 --    -- Set a timer to delete the circle after 3 seconds
49 --    mouseCircleTimer = hs.timer.doAfter(3, function() mouseCircle:delete() end)
50 --end
51 --hs.hotkey.bind({"cmd","alt","shift"}, "D", mouseHighlight)
52 --
53 --
54 ---- window movement
55 --hs.hotkey.bind({"cmd", "alt", "ctrl"}, "H", function()
56 --  local win = hs.window.focusedWindow()
57 --  local f = win:frame()
58 --
59 --  f.x = f.x - 10
60 --  win:setFrame(f)
61 --end)
62
63 -- Window Movement
64 -- https://andrich.blog/2016/11/20/hammerspoon-an-awesome-tool-to-automate-your-mac/
65 -- CTRL + ALT + Left - Move current window to the left half of the screen.
66 -- CTRL + ALT + Right - Move current window to the right half of the screen.
67 -- CTRL + ALT + Up - Go "fullscreen".
68 -- CTRL + ALT + Down - Center window, covering 2/3 of screen size.
69 --
70  
71 function move_window(direction)
72     return function()
73         local win      = hs.window.focusedWindow()
74         local app      = win:application()
75         local app_name = app:name()
76         local f        = win:frame()
77         local screen   = win:screen()
78         local max      = screen:frame()
79  
80         if direction == "left" then
81             f.x = max.x
82             f.y = max.y
83             f.w = max.w / 2
84             f.h = max.h
85         elseif direction == "right" then
86             f.x = max.x + (max.w / 2)
87             f.y = max.y
88             f.w = max.w / 2
89             f.h = max.h
90         elseif direction == "up" then
91             f.x = max.x
92             f.y = max.y 
93             f.w = max.w
94             f.h = max.h / 2
95         elseif direction == "down" then
96             f.x = max.x 
97             f.y = max.y + (max.h / 2)
98             f.w = max.w 
99             f.h = max.h / 2
100         elseif direction == "max" then
101            if f.w > (max.w * 3/4) and f.h > (max.h * 3/4) then
102                 f.x = max.x + (max.w / 6)
103                 f.y = max.y + (max.h / 6)
104                 f.w = max.w * 2 / 3
105                 f.h = max.h * 2 / 3
106            else
107                 f.x = max.x 
108                 f.y = max.y
109                 f.w = max.w 
110                 f.h = max.h
111            end
112         else
113             hs.alert.show("move_window(): Freaky parameter received " .. direction)
114         end
115  
116         win:setFrame(f, 0)
117     end
118 end
119  
120 local hyper = {"ctrl", "cmd"}
121 hs.hotkey.bind(hyper, "h", move_window("left"))
122 hs.hotkey.bind(hyper, "l", move_window("right"))
123 hs.hotkey.bind(hyper, "k", move_window("up"))
124 hs.hotkey.bind(hyper, "j", move_window("down"))
125 hs.hotkey.bind(hyper, "m", move_window("max"))
126
127 -- Search and dictionary
128 -- Past and copy
129 -- App quick startup
130
131 -- Use SpoonInstall,
132 -- http://zzamboni.org/post/using-spoons-in-hammerspoon/
133 -- https://github.com/Hammerspoon/Spoons/raw/master/Spoons/SpoonInstall.spoon.zip
134 if hs.spoons == nil then
135    hs.spoons=require('hs.spoons')
136 end
137
138
139 hs.loadSpoon ("SpoonInstall")
140 spoon.SpoonInstall.use_syncinstall = true
141 INSTALL = spoon.SpoonInstall
142
143 --------------------------------------------------------------------------------
144 -- MouseCircle  http://www.hammerspoon.org/Spoons/MouseCircle.html
145 INSTALL:andUse("MouseCircle",
146                 {
147                    disable = false,
148                    config = {
149                       color = hs.drawing.color.x11.rebeccapurple
150                    },
151                    hotkeys = {
152                       show = { {"cmd", "alt", "ctrl"}, "m"}
153                    }
154                 }
155 )
156
157 --------------------------------------------------------------------------------
158 -- TextClipboardHistory
159 INSTALL:andUse("TextClipboardHistory",
160                {
161                   config = {
162                      show_in_menubar = false,
163                   },
164                   hotkeys = {
165                      toggle_clipboard = { { "cmd", "shift" }, "v"}
166                   },
167                   start = true;
168                }
169 )
170
171
172 --------------------------------------------------------------------------------
173 -- Seal -- http://www.hammerspoon.org/Spoons/Seal.html
174 INSTALL:andUse("Seal",
175                {
176                   hotkeys = { show = { {"cmd", "ctrl", "alt"}, "space" } },
177                   fn = function(s)
178                      s:loadPlugins({"apps", "calc", "safari_bookmarks", "screencapture", "useractions"})
179                      s.plugins.safari_bookmarks.always_open_with_safari = false
180                      s.plugins.useractions.actions =
181                         {
182                            ["Hammerspoon docs webpage"] = {
183                               url = "http://hammerspoon.org/docs/",
184                               icon = hs.image.imageFromName(hs.image.systemImageNames.ApplicationIcon),
185                               -- hotkey = { hyper, "h" }
186                            },
187                            ["Leave corpnet"] = {
188                               fn = function()
189                                  spoon.WiFiTransitions:processTransition('foo', 'corpnet01')
190                               end,
191                               icon = swisscom_logo,
192                            },
193                            ["Arrive in corpnet"] = {
194                               fn = function()
195                                  spoon.WiFiTransitions:processTransition('corpnet01', 'foo')
196                               end,
197                               icon = swisscom_logo,
198                            },
199                            ["Translate using Leo"] = {
200                               url = "http://dict.leo.org/ende/index_de.html#/search=${query}",
201                               icon = 'favicon',
202                               keyword = "leo",
203                            },
204                            ["Tell me something"] = {
205                               keyword = "tellme",
206                               fn = function(str) hs.alert.show(str) end,
207                            }
208                         }
209                      s:refreshAllCommands()
210                   end,
211                   start = true,
212                }
213 )