Skip to content

Instantly share code, notes, and snippets.

@hluk
Created June 3, 2019 12:54
Show Gist options
  • Save hluk/b7830fe6c247a465e5f884711c8597a7 to your computer and use it in GitHub Desktop.
Save hluk/b7830fe6c247a465e5f884711c8597a7 to your computer and use it in GitHub Desktop.
CopyQ Command: Replace selected text with a script
[Command]
Command="
copyq:
// path to python script
// (always use forward slash as path separator)
script = 'c:/scripts/modify_text.py'
// get old text from clipboard
var oldText = str(clipboard())
try {
// copies selected text
copy()
// get the copied text from clipboard
var text = str(clipboard())
// do something with the text
// - this launches a python script and writes the text to standard input
// - you can remove the null to pass the text as argument
var result = execute('python', script, null, text)
// handle script errors
if (!result)
throw 'Failed to launch script'
if (result.exit_code != 0)
throw 'Script failed: ' + str(results.stderr)
// - get the new text from script's standard output
var newText = str(result.stdout)
popup('Script input', text)
popup('Script output', newText)
// copy and paste the new text
// - some sleep/wait may be required required
copy(newText)
sleep(200)
paste()
sleep(200)
} catch (e) {
popup('Failed to replace text', e)
}
// revert the old text to clipboard
copy(oldText)"
GlobalShortcut=ctrl+shift+z
Icon=\xf3e2
IsGlobalShortcut=true
Name=Replace Text With Script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment