Hammerspoon:使用中的过滤器应用程序

Hammerspoon: filter application in use

我绑定了一些键来执行脚本。仅当 Visual Studio 代码 window 被聚焦时,我才需要激活绑定。

我看到了 Hammerspoon 过滤器功能,但我不明白如何使用它。

应用程序名称:Code

hs.hotkey.bind({"ctrl"}, "b", function()
    local codeWindowFilter = hs.window.filter:setAppFilter('Code')
    codeWindowFilter:isAppAllowed("Code", function()

        mycode here

    end)
end)

但我收到错误 attempt to call a nil value (method 'setAppFilter')

这是正确的方法吗? 有没有办法将所有绑定放入已批准的过滤器中?

提前谢谢你

根据此处的文档 (http://www.hammerspoon.org/docs/hs.window.filter.html):

您需要遵循以下结构:

local wf=hs.window.filter
wf.default:setAppFilter('My IDE',{allowTitles=1})

创建一个新的 window 过滤器,如下所述:

While you can customize the default windowfilter, it's usually advisable to make your customizations on a local copy via mywf=hs.window.filter.new(); the default windowfilter can potentially be used in several Hammerspoon modules and changing it might have unintended consequences...

因此,将您的代码更改为:

local codeWindowFilter = hs.window.filter.new():setAppFilter('Code')

.new() 方法创建一个新的 window 过滤器。 :setAppFilter 在新对象上调用方法,然后 returns 修改后的 window 过滤器对象。

最后的解决办法是那个:

local frontmostApplication = hs.application.frontmostApplication()

if frontmostApplication and frontmostApplication:name() == "Code" then

   {my code here}

end

您可以像这样过滤应用程序

    wf_terminal = hs.window.filter.new{'Terminal','iTerm2'}
    for _, win in ipairs(wf_terminal:getWindows()) do
        if win ~= nil and string.find(win:title(), "nvim") then
            moveWindowToLeftHalf(win)
            win:focus()
        end
    end