Skip to content

Instantly share code, notes, and snippets.

@honmaple
Created March 25, 2024 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honmaple/84205e7992879ac3ff019d602250e57d to your computer and use it in GitHub Desktop.
Save honmaple/84205e7992879ac3ff019d602250e57d to your computer and use it in GitHub Desktop.
Like i3wm's scratchpad
local _M = {
scratchpad = {}
}
local yabai = string.gsub(hs.execute("which yabai", true), "%s+", "")
_M.init = function()
local output, status = _M.exec("query --windows")
if not status then return end
local windows = hs.json.decode(output)
for _, window in pairs(windows) do
if window["is-hidden"] then
table.insert(_M.scratchpad, window)
end
end
end
_M.exec = function(args)
local command = string.format("%s -m %s", yabai, args)
print(command)
return hs.execute(command)
end
_M.show_scratchpad = function(name)
for index, window in pairs(_M.scratchpad) do
if not name or name == window["app"] then
table.remove(_M.scratchpad, index)
local app = hs.application.find(window["pid"])
app:unhide()
app:activate(true)
-- 先移动,再切换浮动
-- _M.exec(string.format("window %d --move abs:%d:%d", window["id"], window["frame"]["x"], window["frame"]["y"]))
if not window["is-floating"] then
_M.exec(string.format("window %d --toggle float", window["id"]))
end
return
end
end
if name then
hs.notify.new({informativeText="应用未找到"}):send()
return
end
local output, status = _M.exec("query --windows --window")
if not status then return end
local window = hs.json.decode(output)
if window and window["is-floating"] then
table.insert(_M.scratchpad, window)
local app = hs.application.find(window["pid"])
return app:hide()
-- return _M.exec("window --move abs:10000:10000")
end
hs.notify.new({informativeText="没有隐藏应用"}):send()
end
_M.move_to_scratchpad = function()
local output, status = _M.exec("query --windows --window")
if not status then return end
local window = hs.json.decode(output)
if window and window["has-focus"] then
table.insert(_M.scratchpad, window)
if not window["is-floating"] then
_M.exec("window --toggle float")
end
local app = hs.application.find(window["pid"])
return app:hide()
-- return _M.exec("window --move abs:10000:10000")
end
hs.notify.new({informativeText="应用未找到"}):send()
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment