刚接触Mac,自用hammerspoon设置笔记
require "volume"
require "keymap"
require "clipboard"
-- 通过shell执行
local function shellOrder(order)
local shell_command = order
hs.execute(shell_command)
end
-- 通过shell实现休眠
local function sleepByShell(mods, key)
local mods = mods or {}
hs.hotkey.bind(mods, key, function()
shellOrder("pmset sleepnow")
end)
end
-- url encode 和decode函数
local function urlEncode(s)
s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return string.gsub(s, " ", "+")
end
local function urlDecode(s)
s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
return s
end
function keymapp()
-- 修改按键
-- hs.hotkey.bind({'ctrl', 'alt'}, 'a', function() hs.eventtap.event.newKeyEvent({'command'}, 'f', true):post() end)
-- 设置快捷键睡眠
sleepByShell({"alt", "ctrl"}, "`")
-- 快捷唤醒app或打开文件
hs.hotkey.bind({"cmd", "ctrl"}, "h", function() shellOrder("open /.hammerspoon/init.lua") end)
hs.hotkey.bind({"cmd", "ctrl"}, "b", function() hs.application.open("BetterTouchTool.app") end)
hs.hotkey.bind({"cmd", "ctrl"}, "t", function() hs.application.open("Terminal.app") end)
hs.hotkey.bind({"cmd", "alt"}, "e", function() hs.application.open("Finder.app") end)
hs.hotkey.bind({"cmd", "alt"}, "w", function() hs.application.open("WeChat.app") end)
hs.hotkey.bind({"cmd", "alt"}, "s", function() hs.application.open("System Settings.app") end)
hs.hotkey.bind({"cmd", "alt"}, "q", function() hs.application.open("Activity Monitor.app") end)
hs.hotkey.bind({"cmd", "alt"}, "a", function() hs.application.open("Launchpad.app") end)
-- 快捷键输出补全文字
hs.hotkey.bind({"cmd","alt","shift"},".", function() hs.eventtap.keyStrokes(">") end)
-- 百度搜索
hs.hotkey.bind({"cmd", "alt"}, "2", function()
local clipboardText = tostring(hs.pasteboard.getContents())
hs.notify.new({title="百度搜索", informativeText=clipboardText}):send()
hs.urlevent.openURL("https://www.baidu.com/s?wd="..urlEncode(clipboardText))
end)
hs.hotkey.bind({"cmd", "alt"}, "3", function()
local clipboardText = tostring(hs.pasteboard.getContents())
hs.notify.new({title="谷歌搜索", informativeText=clipboardText}):send()
hs.urlevent.openURL("https://www.google.com/search?q="..urlEncode(clipboardText))
end)
hs.hotkey.bind({"cmd", "alt"}, "0", function()
local clipboardText = tostring(hs.pasteboard.getContents())
hs.notify.new({title="打开url", informativeText=clipboardText}):send()
hs.urlevent.openURL(clipboardText)
end)
end
keymapp()