Manipulate the clipboard content using the command line

This script allows to quickly run and preview any shell command on the clipboard content using the command line. Example usecase -> urlencode a string, extract column from a csv, run a jq command... You can also chain shell commands using pipes.

Install pipe

// Menu: Pipe Clipboard to Command
// Decription: Manipulate clipboard content using the command line
// Author: pomdtr
process.env.PATH = `${process.env.HOME}/.local/bin:/usr/local/bin/:${process.env.PATH}`;
function codeblock(text) {
const triple_backquote = "```";
return `${triple_backquote}shell\n${text}\n${triple_backquote}`;
}
// Persist clipoard inside a file
const clipboardContent = await paste();
const tempfile = `${kit.tempdir()}/input.txt`;
await kit.writeFile(tempfile, clipboardContent, {
flag: "w",
encoding: "utf-8",
});
let command = "";
let [stdout, stderr, code] = [null, null, 0];
let panelContent = await kit.readFile(tempfile, { encoding: "utf-8" });
while (true) {
command = await arg(
{
placeholder: "Input Command:",
className: "p-2",
input: command,
hint: "Hit enter to run command and save output to clipboard, esc to dismiss window."
},
md(codeblock(panelContent))
);
if (command) {
({ stdout, stderr, code } = exec(`cat ${tempfile} | ${command}`));
panelContent = code == 0 ? stdout : stderr;
if (code == 0) {
panelContent = stdout;
} else {
panelContent = stderr;
}
} else {
panelContent = await kit.readFile(tempfile, { encoding: "utf-8" });
}
await copy(panelContent);
}