Created
April 18, 2025 23:57
-
-
Save akngs/213f9af0f57d5acc32f32c05a4106df9 to your computer and use it in GitHub Desktop.
A Hammerspoon script that, when you select text and input a shell command, executes the command by piping the selected text into standard input and copies the standard output to the clipboard.
This file contains hidden or 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
local os = require("os") | |
local obj = {} | |
function obj:init() | |
self.statusIcon = hs.menubar.new() | |
return self | |
end | |
function obj:setStatus(emoji) | |
self.statusIcon:setTitle(emoji) | |
end | |
function obj:clearStatus() | |
self.statusIcon:setTitle("") | |
end | |
function obj:bindHotkeys(mapping) | |
local spec = { run = hs.fnutils.partial(self.run, self) } | |
hs.spoons.bindHotkeysToSpec(spec, mapping) | |
return self | |
end | |
function obj:run() | |
-- Copy the text to the clipboard | |
hs.eventtap.keyStroke({ "cmd" }, "c") | |
hs.timer.usleep(0.001) | |
-- Get the text from the clipboard | |
local input = hs.pasteboard.getContents() | |
if input == nil then | |
hs.alert("Please select the text first") | |
return | |
end | |
-- Get the command | |
local cmd = self:getCommand() | |
if cmd == nil then | |
return | |
end | |
-- Run the command | |
self:setStatus("⏳") | |
hs.task.new(os.getenv("SHELL"), function(code, out, err) | |
if code ~= 0 then | |
hs.dialog.blockAlert("Error", err, "Ok") | |
else | |
hs.alert("Updated clipboard") | |
end | |
self:clearStatus() | |
end, { | |
"-i", "-c", "pbpaste | " .. cmd .. " | pbcopy" | |
}):start() | |
end | |
function obj:getCommand() | |
local focusedApp = hs.window.frontmostWindow():application() | |
local button, userInput = hs.dialog.textPrompt("Enter the command", "", "llm \"Summerize\"", "Ok", "Cancel") | |
if focusedApp then | |
hs.timer.doAfter(0.01, function() | |
focusedApp:activate() | |
end) | |
end | |
if button == "Ok" then | |
return userInput | |
else | |
return nil | |
end | |
end | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment