Skip to content

Instantly share code, notes, and snippets.

@danelowe
Last active October 1, 2024 08:33
Show Gist options
  • Save danelowe/cf519425413317c2faa89b65dd3fd9d5 to your computer and use it in GitHub Desktop.
Save danelowe/cf519425413317c2faa89b65dd3fd9d5 to your computer and use it in GitHub Desktop.
Hammerspoon copy markdown link
local module = {}
module.activeTabDetails = function()
local success, result = hs.osascript.javascript([[
(() => {
// chromium based browsers will likely have the same API, just different bundle ID or application name.
// For other browsers, can refer to the application's dictionary in script editor
const edge = Application("com.microsoft.edgemac");
if (!edge.frontmost()) {
return
}
const tab = edge.windows()[0]?.activeTab();
if (!tab) {
return
}
// It is possible to execute js on the page, but that would require opening up permissions to osascript
const fullTitle = tab.title();
const match = fullTitle.match(/^(.*?) by danelowe · Pull Request #.*$/);
const title = match?.[1] ?? fullTitle;
return {
url: tab.url(),
title,
}
})()
]])
return success and result or nil
end
return module
-- Creates a modal keybinding, simlar to entering normal mode in vim.
k = hs.hotkey.modal.new('cmd', 'escape')
k:bind('', 'I', function() k:exit() end)
k:bind('', 'A', function()
local tab = edge.activeTabDetails()
local link = string.format(":mag: [%s](%s)", tab.title, tab.url)
hs.pasteboard.setContents(link)
hs.alert.show("Copied to clipboard")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment