Skip to content

Instantly share code, notes, and snippets.

@blindFS
Last active January 21, 2019 15:29
Show Gist options
  • Save blindFS/0e24b9ce5f509edfc7af7315ef9f8531 to your computer and use it in GitHub Desktop.
Save blindFS/0e24b9ce5f509edfc7af7315ef9f8531 to your computer and use it in GitHub Desktop.
hs_window.lua
local obj={}
obj.__index = obj
obj.name = "Windows"
obj.version = "1.0"
obj.author = "blindFS"
-- Internal function used to find our location, so we know where to load files from
local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
obj.spoonPath = script_path()
-- Define the source's overview. A unique `keyword` key should exist, so this source can be found.
obj.overview = {text="Type w ⇥ to search windows.", image=hs.image.imageFromPath(obj.spoonPath .. "/resources/windows.png"), keyword="w"}
-- Define the notice when a long-time request is being executed. It could be `nil`.
obj.notice = {text="Requesting data, please wait a while …"}
local wf = hs.window.filter.new()
local wl
local function windowsRequest()
wl = wf:getWindows()
-- Remove currently focused window
table.remove(wl, 1)
local chooser_data = {}
chooser_data = hs.fnutils.imap(wl, function(item)
local app = item:application()
return {text=app:title(), subText=item:title(), image=hs.image.imageFromAppBundle(app:bundleID()), output="window", arg=item:id()}
end)
return chooser_data
end
-- Define the function which will be called when the `keyword` triggers a new source. The returned value is a table. Read more: http://www.hammerspoon.org/docs/hs.chooser.html#choices
obj.init_func = windowsRequest
-- As the user is typing, the callback function will be called for every keypress. The returned value is a table.
obj.callback = nil
obj.new_output = {name="window", func=function(id)
for idx,w in pairs(wl) do
if w:id() == id then
w:focus()
-- Dirty hack for multi-window in multi-display of single app
hs.timer.usleep(50000)
w:focus()
return
end
end
end}
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment