hammerspoon/modules/launcher.lua
2024-02-27 15:18:15 +08:00

58 lines
2.7 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- http://qtdebug.com/mac-hammerspoon/
-- 键和应用对
-- 提示: 数字作为键,需要使用 [Number] 的格式
local KEY_APP_PAIRS = {
[5] = "QSpace Pro.app",
Q = "QQ.app",W = "WeChat.app",E = "Microsoft Edge.app",T = "iTerm.app",I = "IINA.app",O = "Obsidian.app", P = "/Users/tanpengsccd/Applications/JetBrains Toolbox/PyCharm Professional Edition.app",
A = "ILink.app", S = "Surge.app",F = "/System/Volumes/Data/Applications/Apifox.app" ,G = "https://ai.h6.work",K = "Spotify.app", L = "NeteaseMusic.app",
Z = "QSpace Pro.app",X = "Xcode.app",C = "Google Chrome.app",V = "Visual Studio Code.app",B="iTerm.app",N = "Notes.app",M = "Telegram.app", -- B = "wechatwebdevtools.app"*/
-- [2] = "Notable.app",
-- [3] = "Typora.app",
}
-- 修饰键 这里设置为 f19
local MOFIFER_KEY = f19
-- local MOFIFER_KEY = "alt"
-- 显示 Finder:
hs.hotkey.bind(MOFIFER_KEY, "1", function()
hs.application.open("/System/Library/CoreServices/Finder.app")
hs.application.get("com.apple.finder"):setFrontmost(true)
end)
-- 重新加载配置
hs.hotkey.bind(f19, "R", function()
hs.reload()
end)
--------------------------------------------------------------------------------------
-- 按下 "F19+键" 会打开或激活对应的应用,如果应用不是绝对路径,则指的是 /Applications 中的应用 --
--------------------------------------------------------------------------------------
function bindAppWithHotkey(keyAppPairs,modifierKey)
-- hs.alert.show("modifierKey,key: " .. modifierKey .. "," .. keyAppPairs ..)
-- 确保MODIFIER_KEY已经被传递进来或在这里定义例如MODIFIER_KEY = {"cmd", "alt"}
for key, target in pairs(keyAppPairs) do
hs.hotkey.bind(modifierKey, key, function()
hs.alert.show("" .. target)
-- 如果是以http前缀的就是网址,需要使用 Chrome 打开
if string.find(target, "^http") then -- 使用'^http'来确保字符串以http开始
-- hs.alert.show("URL:" .. target)
local url = target
local scriptPath = os.getenv("HOME") .. "/.hammerspoon/jxa/openInChrome.jxa" -- 使用环境变量获取用户目录
local command = "osascript -l JavaScript " .. scriptPath .. " '" .. url .. "'"
hs.execute(command)
return
elseif string.find(target, "app$") then -- 其它都当APP 处理
-- hs.alert.show("APP:" .. target)
hs.application.launchOrFocus(target) -- 这种可以只传入app name,也可以传入路径
return
else
hs.alert.show("未知类型:" .. target)
end
end)
end
end
-- 这里绑定
bindAppWithHotkey(KEY_APP_PAIRS,MOFIFER_KEY)