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
| hs.hotkey.bind({ "alt" }, "1", function() toggleAppByBundleId("com.bohemiancoding.sketch3") end) hs.hotkey.bind({ "alt" }, "2", function() toggleAppByBundleId("com.sequelpro.SequelPro") end)
hs.hotkey.bind({ "alt" }, "q", function() toggleAppByBundleId("com.tencent.qq") end) hs.hotkey.bind({ "alt" }, "w", function() toggleAppByBundleId("com.tencent.xinWeChat") end) hs.hotkey.bind({ "alt" }, "e", function() toggleAppByBundleId("com.apple.Notes") end)
hs.hotkey.bind({ "alt" }, "a", function() toggleAppByBundleId("com.microsoft.VSCode") end) hs.hotkey.bind({ "alt" }, "s", function() toggleAppByBundleId("com.apple.finder") end) hs.hotkey.bind({ "alt" }, "d", function() toggleAppByBundleId("com.microsoft.edgemac") end)
hs.hotkey.bind({ "alt" }, "z", function() toggleAppByBundleId("notion.id") end) hs.hotkey.bind({ "alt" }, "x", function() toggleAppByBundleId("com.jetbrains.intellij") end)
hs.hotkey.bind({ "alt" }, "m", function() toggleAppByBundleId("com.apple.mail") end)
mousePositions = {}
function toggleAppByBundleId(appBundleID)
local frontMostApp = hs.application.frontmostApplication() local mainWindow = frontMostApp:mainWindow() if mainWindow:isStandard() then mousePositions[frontMostApp:mainWindow():id()] = hs.mouse.getAbsolutePosition() end
if frontMostApp:bundleID() == appBundleID then local wf = hs.window.filter.new{frontMostApp:name()} local locT = wf:getWindows({hs.window.filter.sortByFocusedLast}) if locT and #locT > 1 then local windowId = frontMostApp:mainWindow():id() for _, value in pairs(locT) do if value:id() ~= windowId then value:focus() end end else frontMostApp:hide() end else local launchResult = hs.application.launchOrFocusByBundleID(appBundleID)
if not launchResult then return end end
frontMostApp = hs.application.applicationsForBundleID(appBundleID)[1]
local point = mousePositions[appBundleID] if point then hs.mouse.setAbsolutePosition(point) local currentSc = hs.mouse.getCurrentScreen() local tempSc = frontMostApp:mainWindow():screen() if currentSc ~= tempSc then setMouseToCenter(frontMostApp) end else setMouseToCenter(frontMostApp) end
end
function setMouseToCenter(frontMostApp) local mainWindow = frontMostApp:mainWindow() if not mainWindow then return end local mainFrame = mainWindow:frame() local mainPoint = hs.geometry.point(mainFrame.x + mainFrame.w /2, mainFrame.y + mainFrame.h /2) hs.mouse.setAbsolutePosition(mainPoint) end
|