1

I bind some key to execute script. I need to activite bind only when Visual Studio Code window is focused.

I see Hammerspoon filters functions but I don't understand how use it.

Application name: Code

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

        mycode here

    end)
end)

but I get the error attempt to call a nil value (method 'setAppFilter')

Is this the correct way? Is there way to put all bind into approved filter?

Thnak you in advance

Lorenzo
  • 41
  • 9

3 Answers3

1

The final solution is that one:

local frontmostApplication = hs.application.frontmostApplication()

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

   {my code here}

end
Lorenzo
  • 41
  • 9
0

According to the docs here (http://www.hammerspoon.org/docs/hs.window.filter.html):

You need to follow this structure:

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

OR

Create a new window filter as is described here:

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...

Therefore, change your code to be this:

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

The .new() method creates a new window filter. The :setAppFilter calls a method on the new object, and then returns the modified window filter object.

Jeff Mikels
  • 713
  • 1
  • 6
  • 14
  • Thank you for you help, but I have problem with the next step. How to use the filter? If I use `local codeWindowFilter = hs.window.filter.new():setAppFilter('Code') codeWindowFilter:isAppAllowed(hs.application:title(), function() mycode here end)` "mycode" is not executed and it get error. My goal is enable the shortcut or it's contained code only if a window of "Code" application is focused – Lorenzo Jan 23 '19 at 08:47
0

You can filter app like this

    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
tjfdfs
  • 729
  • 10
  • 26