manipulate-the-clipboard-content-using-the-command-line
by Achille Lacoin
// Menu: Pipe Clipboard to Command// Decription: Manipulate clipboard content using the command line// Author: pomdtrprocess.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 fileconst 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);}