hammerspoon简介

这是一个OS X平台上的非常强大的窗口管理工具。Hammerspoon的核心是操作系统和lua脚本的桥梁引擎。用户可以通过Hammerspoon的提供一批接口, 实现很多强大的功能。

Hammerspoon能做什么

你可以通过写lua脚本来与OS X的系统api进行交互,这些系统接口包括:应用,桌面,鼠标,文件系统,电池,屏幕,wifi等。

配置

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
-- Defines for window grid
hs.grid.GRIDWIDTH = 2
hs.grid.GRIDHEIGHT = 2
hs.grid.MARGINX = 0
hs.grid.MARGINY = 0
local moveMaxWidth = hs.grid.GRIDWIDTH / 2 + 1
local moveMinWidth = hs.grid.GRIDWIDTH / 2 - 1
local frameCache = {}

-- key define 快捷键的修饰键
local hyper = {'ctrl', 'cmd', 'shift', 'alt'}
local hyperAlt = {'ctrl', 'cmd'}

-- states
hs.window.animationDuration = 0

-- App shortcuts  程序快捷键,一键启动加切换
-- hyperAlt(ctrl+cmd+alt) + 前面的字母就可以了 
local key2App = {
    i = 'iTerm',
    m = 'Mail',
    b = 'Google Chrome',
    f = 'Finder',
    s = 'System Preferences',
    v = 'Visual Studio Code',
    c = 'RingCentral'
}
for key, app in pairs(key2App) do
  --hs.hotkey.bind(hyper, key, function() hs.application.launchOrFocus(app) end)
  hs.hotkey.bind(hyperAlt, key, function() toggle_application(app) end)
end

function toggle_application(_app)
    -- finds a running applications
    local app = hs.application.find(_app)

    if not app then
        -- application not running, launch app
        hs.application.launchOrFocus(_app)
        return
    end

    -- application running, toggle hide/unhide
    local mainwin = app:mainWindow()
    if mainwin then
        if true == app:isFrontmost() then
            mainwin:application():hide()
        else
            mainwin:application():activate(true)
            mainwin:application():unhide()
            mainwin:focus()
        end
    else
        -- no windows, maybe hide
        if true == app:hide() then
            -- focus app
            hs.application.launchOrFocus(_app)
        else
            -- nothing to do
        end
    end
end

-- Helper functions
-- Replace Caffeine.app with 18 lines of Lua :D 系统咖啡因,禁止系统休眠,几行代码搞定
local caffeine = hs.menubar.new()

function setCaffeineDisplay(state)
    local result
    if state then
        result = caffeine:setIcon("caffeine-on.pdf")
    else
        result = caffeine:setIcon("caffeine-off.pdf")
    end
end

function caffeineClicked()
    setCaffeineDisplay(hs.caffeinate.toggle("displayIdle"))
end

if caffeine then
    caffeine:setClickCallback(caffeineClicked)
    setCaffeineDisplay(hs.caffeinate.get("displayIdle"))
end

function moveToNextScreen()
    local app = hs.window.focusedWindow()
    app:moveToScreen(app:screen():next())
    app:maximize()
end
  
hs.hotkey.bind(hyper, "]", moveToNextScreen)

function move_window(direction)
    return function()
        local win      = hs.window.focusedWindow()
        local f        = win:frame()
        local screen   = win:screen()
        local max      = screen:frame()

        if direction == "center" then
            -- +-----------------+
            -- |  ----------     |        
            -- |  |  HERE  |     |
            -- |  |________|     |  
            -- +-----------------+
            f.x = max.x + (max.w * 3 / 20)
            f.y = max.y + (max.h * 3 / 20)
            f.w = max.w * 7 / 10
            f.h = max.h * 7 / 10
        elseif direction == "max" then
             -- +-----------------+
             -- |                 |        
             -- |      HERE       |
             -- |                 |  
             -- +-----------------+
             f.x = 0
             f.y = 0
             f.w = max.w
             f.h = max.h
        elseif direction == "left" then
            -- +-----------------+
            -- |        |        |
            -- |  HERE  |        |
            -- |        |        |
            -- +-----------------+
            f.x = max.x
            f.y = max.y
            f.w = max.w / 2
            f.h = max.h
        elseif direction == "right" then
            -- +-----------------+
            -- |        |        |
            -- |        |  HERE  |
            -- |        |        |
            -- +-----------------+
            f.x = max.x + (max.w / 2)
            f.y = max.y
            f.w = max.w / 2
            f.h = max.h
        elseif direction == "up" then
            -- +-----------------+
            -- |      HERE       |
            -- +-----------------+
            -- |                 |
            -- +-----------------+
            f.x = max.x
            f.w = max.w
            f.y = max.y
            f.h = max.h / 2
        elseif direction == "down" then
            -- +-----------------+
            -- |                 |
            -- +-----------------+
            -- |      HERE       |
            -- +-----------------+
            f.x = max.x
            f.w = max.w
            f.y = max.y + (max.h / 2)
            f.h = max.h / 2
        elseif direction == "upLeft" then
            -- +-----------------+
            -- |  HERE  |        |
            -- +--------+        |
            -- |                 |
            -- +-----------------+
            f.x = max.x
            f.y = max.y
            f.w = max.w/2
            f.h = max.h/2
        elseif direction == "upRight" then
            -- +-----------------+
            -- |        |  HERE  |
            -- |        +--------|
            -- |                 |
            -- +-----------------+
            f.x = max.x + (max.w / 2)
            f.y = max.y
            f.w = max.w/2
            f.h = max.h/2
        elseif direction == "downLeft" then
            -- +-----------------+
            -- |                 |
            -- +--------+        |
            -- |  HERE  |        |
            -- +-----------------+
            f.x = max.x
            f.y = max.y + (max.h / 2)
            f.w = max.w/2
            f.h = max.h/2
        elseif direction == "downRight" then
            -- +-----------------+
            -- |                 |
            -- |        +--------|
            -- |        |  HERE  |
            -- +-----------------+
            f.x = max.x + (max.w / 2)
            f.y = max.y + (max.h / 2)
            f.w = max.w/2
            f.h = max.h/2
        end
        win:setFrame(f) 
    end
end

-- Notify about the config reload
-- hs.notify.new({title="Hammerspoon", informativeText="Config loaded"}):send()
-- shortcuts 
hs.hotkey.bind(hyper, "h", move_window("left"))
hs.hotkey.bind(hyper, "l", move_window("right"))
hs.hotkey.bind(hyper, "k", move_window("up"))
hs.hotkey.bind(hyper, "j", move_window("down"))
hs.hotkey.bind(hyper, "c", move_window("center"))
-- Toggle a window between its normal size, and being maximized
function toggle_window_maximized()
    local win = hs.window.focusedWindow()
    if frameCache[win:id()] then
       win:setFrame(frameCache[win:id()])
       frameCache[win:id()] = nil
    else
       frameCache[win:id()] = win:frame()
       win:maximize()
    end
 end
hs.hotkey.bind(hyper, 'o', toggle_window_maximized)
hs.hotkey.bind(hyper, 'r', function() hs.window.focusedWindow():toggleFullScreen() end)


local function focus_other_screen() -- focuses the other screen 
    local screen = hs.mouse.getCurrentScreen()
    local nextScreen = screen:next()
    local rect = nextScreen:fullFrame()
    local center = hs.geometry.rectMidPoint(rect)
    hs.mouse.setAbsolutePosition(center)
 end 
 
 function get_window_under_mouse() -- from https://gist.github.com/kizzx2/e542fa74b80b7563045a 
    local my_pos = hs.geometry.new(hs.mouse.getAbsolutePosition())
    local my_screen = hs.mouse.getCurrentScreen()
    return hs.fnutils.find(hs.window.orderedWindows(), function(w)
                  return my_screen == w:screen() and my_pos:inside(w:frame())
    end)
 end
 
 function activate_other_screen()
    focus_other_screen() 
    local win = get_window_under_mouse() 
    -- now activate that window 
    win:focus() 
 end 
 
 hs.hotkey.bind(hyper, "a", function() -- does the keybinding
       activate_other_screen()
 end)

参考

  1. Hammerspoon官网
  2. Hammerspoon config