Skip to content

Instantly share code, notes, and snippets.

@velppa
Last active June 24, 2025 13:59
Show Gist options
  • Save velppa/ff812a75415a771756c8a7e97a2a2375 to your computer and use it in GitHub Desktop.
Save velppa/ff812a75415a771756c8a7e97a2a2375 to your computer and use it in GitHub Desktop.
Hammerspoon - Safari Tabs fuzzy-finder search spoon for Seal
--
local obj = {}
obj.__index = obj
-- Metadata
obj.__name = "seal_safari_tabs"
obj.name = "safari_tabs"
obj.description = "Safari tabs plugin for Seal"
obj.keyword = "safari"
function obj:init()
-- Plugin initialization
end
function obj:commands()
return {}
end
function obj:bare()
return self.choicesTabs
end
function obj.choicesTabs(query)
local choices = {}
-- if query == nil or query == "" then
-- return choices
-- end
local script = [[
tell application "Safari"
set tabList to {}
repeat with w from 1 to count of windows
repeat with t from 1 to count of tabs of window w
set tabTitle to name of tab t of window w
set tabURL to URL of tab t of window w
set end of tabList to {tabTitle, tabURL, w, t}
end repeat
end repeat
return tabList
end tell
]]
local ok, result = hs.osascript.applescript(script)
if ok and result then
for _, tab in ipairs(result) do
local title = tostring(tab[1]) or ""
local url = tostring(tab[2]) or ""
local windowIndex = tab[3]
local tabIndex = tab[4]
-- print("url: " .. url .. ", title: " .. title)
if url and tabIndex and windowIndex and (string.find(title:lower(), query:lower(), 1, true) or string.find(url:lower(), query:lower(), 1, true)) then
table.insert(choices, {
text = title,
subText = url,
uuid = obj.__name .. "__safari_" .. windowIndex .. tabIndex,
plugin = obj.__name,
image = hs.image.imageFromAppBundle("com.apple.Safari"),
type = "safari_tab",
windowIndex = windowIndex,
tabIndex = tabIndex
})
end
end
end
return choices
end
function obj.completionCallback(rowInfo)
if rowInfo.type == "safari_tab" then
local script = string.format([[
tell application "Safari"
activate
set current tab of window %d to tab %d of window %d
set index of window %d to 1
end tell
]], rowInfo.windowIndex, rowInfo.tabIndex, rowInfo.windowIndex, rowInfo.windowIndex)
hs.osascript.applescript(script)
end
end
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment