Created
February 4, 2023 19:27
-
-
Save jyc/c1f72e89989dae070c53119d9ec840f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------- | |
-- Window Chooser -- 🚻 | |
-------------------- | |
-- Originally: https://github.com/dmgerman/dmg-hammerspoon/blob/f8da75d121c37df40c0971336eb3f67c73d67187/dmg.spoon/init.lua#L115-L224 | |
WC = {} | |
WC.wf = hs.window.filter.new() | |
WC.wf:setDefaultFilter{} | |
WC.currentWindows = {} | |
for i, v in ipairs(WC.wf:getWindows()) do | |
table.insert(WC.currentWindows, v) | |
end | |
local function on_window_event(w, appName, event) | |
if event == "windowDestroyed" then | |
for i, v in ipairs(WC.currentWindows) do | |
if v == w then | |
table.remove(WC.currentWindows, i) | |
return | |
end | |
end | |
return | |
end | |
if event == "windowCreated" then | |
table.insert(WC.currentWindows, 1, w) | |
return | |
end | |
if event == "windowFocused" then | |
on_window_event(w, appName, "windowDestroyed") | |
on_window_event(w, appName, "windowCreated") | |
end | |
end | |
WC.wf:subscribe(hs.window.filter.windowCreated, on_window_event) | |
WC.wf:subscribe(hs.window.filter.windowDestroyed, on_window_event) | |
WC.wf:subscribe(hs.window.filter.windowFocused, on_window_event) | |
local function list_window_choices() | |
local windowChoices = {} | |
for i, w in ipairs(WC.currentWindows) do | |
if w ~= hs.window.focusedWindow() then | |
local text = w:application():name() | |
if w:title() ~= "" and w:title() ~= text then | |
text = w:title() .. " — " .. text | |
end | |
table.insert(windowChoices, { | |
text = text, | |
subText = w:application():name(), | |
uuid = i, | |
image = hs.image.imageFromAppBundle(w:application():bundleID()), | |
win = w | |
}) | |
end | |
end | |
return windowChoices; | |
end | |
WC.chooser = hs.chooser.new(function (choice) | |
if not choice then return end | |
local v = choice["win"] | |
if v then | |
v:focus() | |
end | |
end) | |
hs.hotkey.bind({"alt"}, "Tab", function () | |
local choices = list_window_choices() | |
WC.chooser:choices(choices) | |
WC.chooser:query('') | |
WC.chooser:show() | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment