入门

官方入门文档:https://www.hammerspoon.org
github地址:https://github.com/Hammerspoon

插件

官方插件下载:https://www.hammerspoon.org/Spoons

完整配置仓库地址

完整的配置已上传到码云公开
https://gitee.com/Cai_Programmer/hammerspoon-config.git

窗口管理配置

快捷键截图来自 Rectangle,为了延续 Rectangle 的操作习惯,我使用 hammerspoon 配置的窗口管理快捷键与 Rectangle 保持一致
图片.png
hammerspoon窗口管理配置lua脚本:

  1. local window = require "hs.window"
  2. local hotkey = require "hs.hotkey"
  3. local layout = require "hs.layout"
  4. local fnutils = require "hs.fnutils"
  5. local mouse = require "hs.mouse"
  6. local geometry = require "hs.geometry"
  7. local screen = require "hs.screen"
  8. local hyperCtrlAlt = {"control", "option"}
  9. local hyperCtrlAltAltCmd = {"control", "option", "command"}
  10. -- 设置窗口动画时长,默认是0.2
  11. window.animationDuration = 0
  12. -- 窗口移动到左1/2
  13. hotkey.bind(hyperCtrlAlt, "Left", function()
  14. window.focusedWindow():moveToUnit(layout.left50)
  15. end)
  16. -- 窗口移动到右1/2
  17. hotkey.bind(hyperCtrlAlt, "Right", function()
  18. window.focusedWindow():moveToUnit(layout.right50)
  19. end)
  20. -- 窗口移动到上1/2
  21. hotkey.bind(hyperCtrlAlt, "Up", function()
  22. window.focusedWindow():moveToUnit'[0,0,100,50]'
  23. end)
  24. -- 窗口移动到下1/2
  25. hotkey.bind(hyperCtrlAlt, "Down", function()
  26. window.focusedWindow():moveToUnit'[0,50,100,100]'
  27. end)
  28. -- 窗口移动到左上1/2
  29. hotkey.bind(hyperCtrlAlt, "U", function()
  30. window.focusedWindow():moveToUnit'[0,0,50,50]'
  31. end)
  32. -- 窗口移动到左下1/2
  33. hotkey.bind(hyperCtrlAlt, "K", function()
  34. window.focusedWindow():moveToUnit'[50,50,100,100]'
  35. end)
  36. -- 窗口移动到右上1/2
  37. hotkey.bind(hyperCtrlAlt, "I", function()
  38. window.focusedWindow():moveToUnit'[50,0,100,50]'
  39. end)
  40. -- 窗口移动到右下1/2
  41. hotkey.bind(hyperCtrlAlt, "J", function()
  42. window.focusedWindow():moveToUnit'[0,50,50,100]'
  43. end)
  44. -- 窗口移动到左1/3
  45. hotkey.bind(hyperCtrlAlt, "D", function()
  46. window.focusedWindow():moveToUnit'[0,0,33,100]'
  47. end)
  48. -- 窗口移动到中1/3
  49. hotkey.bind(hyperCtrlAlt, "F", function()
  50. window.focusedWindow():moveToUnit'[33,0,66,100]'
  51. end)
  52. -- 窗口移动到右1/3
  53. hotkey.bind(hyperCtrlAlt, "G", function()
  54. window.focusedWindow():moveToUnit'[66,0,100,100]'
  55. end)
  56. -- 窗口移动到左2/3
  57. hotkey.bind(hyperCtrlAlt, "E", function()
  58. window.focusedWindow():moveToUnit'[0,0,66,100]'
  59. end)
  60. -- 窗口移动到右2/3
  61. hotkey.bind(hyperCtrlAlt, "T", function()
  62. window.focusedWindow():moveToUnit'[33,0,100,100]'
  63. end)
  64. -- switch active window
  65. hotkey.bind(hyperCtrlAltAltCmd, "H", function()
  66. window.switcher.nextWindow()
  67. end)
  68. -- 移动窗口到下一屏幕
  69. hotkey.bind(hyperCtrlAltAltCmd, "Left", function()
  70. local res = window.focusedWindow():moveOneScreenWest()
  71. if res == nil then
  72. window.focusedWindow():moveOneScreenEast()
  73. end
  74. end)
  75. -- 移动窗口到上一屏幕
  76. hotkey.bind(hyperCtrlAltAltCmd, "Right", function()
  77. local res = window.focusedWindow():moveOneScreenEast()
  78. if res == nil then
  79. window.focusedWindow():moveOneScreenWest()
  80. end
  81. end)

显示网络信息

image.png
扩展github地址:https://github.com/Hammerspoon/Spoons/raw/master/Spoons/SpeedMenu.spoon.zip
在入口文件引入扩展并启动:

  1. local SpeedMenu = require "SpeedMenu/init"
  2. -- 在菜单栏显示网速
  3. SpeedMenu:init();
  4. SpeedMenu:start();

剪贴板历史

image.png
扩展github地址:https://github.com/Hammerspoon/Spoons/raw/master/Spoons/ClipboardTool.spoon.zip
在入口文件引入扩展并启动:

  1. local ClipboardTool = require "ClipboardTool/init"
  2. -- 开始记录剪贴板历史
  3. ClipboardTool:start();
  4. -- 显示剪贴板历史
  5. hotkey.bind({"command", "shift"}, "V", function()
  6. ClipboardTool:showClipboard();
  7. end)

需要调整的配置:

  1. --- 是否开启最大字数校验
  2. obj.max_size = true
  3. --- 单次复制最大字数
  4. obj.max_entry_size = 150
  5. --- 扫描剪贴板时间间隔,如果设置的太短会有功耗问题,如果设置的太长可能无法记录到历史
  6. obj.frequency = 0.8
  7. --- 保留历史记录的个数
  8. obj.hist_size = 20