Skip to content

Instantly share code, notes, and snippets.

@akbo
Created December 15, 2021 22:30
Show Gist options
  • Save akbo/fa0029d583c54c8c0992cc1fd65c3995 to your computer and use it in GitHub Desktop.
Save akbo/fa0029d583c54c8c0992cc1fd65c3995 to your computer and use it in GitHub Desktop.
Runs Python code formatter on selected text and pastes it back
// Name: black
// Shortcut: command+shift+b
import "@johnlindquist/kit";
// Change the below to fit your system
const tempFile = "~/scriptkit_black_temp.py";
const blackPath = "~/miniconda3/bin/black";
// copy selected text to clipboard
await getSelectedText();
// run black on text
exec(
`pbpaste > ${tempFile} && ${blackPath} ${tempFile} && cat ${tempFile} | pbcopy && rm ${tempFile}`
);
// remove newline at the end of the text and paste
const blackened = await paste();
await setSelectedText(blackened.slice(0, blackened.length - 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment