patrick.wtf

๐Ÿ“

TIL: using hammerspoon to launch any app with a hotkey

TIL: using hammerspoon to launch any app with a hotkey

Iโ€™ve recently started using Logseq for writing daily notes and I found myself needing to open it very often (which is a good thing I hope), so I decided to spend a bit of time making a hotkey that opens Logseq and shows it in the active desktop (I wanted to prevent any kind of animation).

Iโ€™ve used hammerspoon and hs.spaces to make this work. It did take a bit of time to figure out how to move the app to my active desktop, but in the end I got it working quite well. The code is actually pretty short, so Iโ€™ll probably start using this for other apps soon.

Hereโ€™s the code if youโ€™re curious:

spaces = require("hs.spaces")

hs.hotkey.bind({"cmd", "alt", "ctrl", "shift"}, "L", function()
    local app = hs.application.find("logseq")

    if app == nil then
        hs.alert.show("Launching logseq")
        hs.application.launchOrFocus("logseq")
    elseif app:isFrontmost() then
        app:hide()
    else
        local win = app:mainWindow()

        local activeSpaces = spaces.activeSpaces()

        spaces.moveWindowToSpace(win:id(), spaces.activeSpaceOnScreen())
        win:focus()
    end
end)

Basically every time I press CMD+ALT+CTRL+SHIFT+L I trigger the code that checks if Logseq is running, if is it toggles it based on its status, otherwise it starts the app ๐Ÿ˜Š

Webmentions