【LUA】执行shell命令、绑定快捷键打开App、模拟按键实现复制搜索

刚接触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()
    -- 修改按键
    -- mac自带截图
--     hs.hotkey.bind({'alt'}, '1', function() hs.eventtap.event.newKeyEvent({'cmd','shift'}, '4', true):post() end)  
    -- 浏览器切换标签
--    hs.hotkey.bind({'alt'}, ',', function() hs.eventtap.event.newKeyEvent({'cmd','alt'}, 'left', true):post() end)  
--    hs.hotkey.bind({'alt'}, '.', function() hs.eventtap.event.newKeyEvent({'cmd','alt'}, 'Right', true):post() end)  
    
    
	-- 设置快捷键睡眠
    sleepByShell({"alt", "ctrl"}, "`")

	-- 快捷唤醒app或打开文件
    hs.hotkey.bind({"cmd", "ctrl"}, "h", function() shellOrder("open /Users/lam/.hammerspoon/init.lua") end)
	hs.hotkey.bind({"cmd", "ctrl"}, "b", function() hs.application.open("BetterTouchTool.app") end)
	
	hs.hotkey.bind({"cmd", "alt"}, "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() 
        hs.eventtap.keyStroke({ "cmd" }, "C")
        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() 
    hs.eventtap.keyStroke({ "cmd" }, "C")
    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() 
    hs.eventtap.keyStroke({ "cmd" }, "C")
    local clipboardText = tostring(hs.pasteboard.getContents())
    hs.notify.new({title="打开url", informativeText=clipboardText}):send()
    hs.urlevent.openURL(clipboardText) 
    end)
        
end

keymapp()












  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值