Last active
September 21, 2023 16:28
-
-
Save typoman/6bdd3bd6cbd3afb00ed83f5b2c429910 to your computer and use it in GitHub Desktop.
UI form for adding snippets to espanso
This file contains 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
import os | |
import sys | |
""" | |
Add {cb} anywhere inside the replacement to make it replaced with the contents | |
of the clipboard. | |
""" | |
transMap = str.maketrans({ | |
"\n": '\\n', | |
"\\": '\\\\', | |
"\"": '\\"', | |
}) | |
trigger = os.environ['ESPANSO_FORM1_TRIGGER'].translate(transMap) | |
replace = os.environ['ESPANSO_FORM1_REPLACE'].translate(transMap) | |
espansoConfigPath = sys.argv[1:][0] | |
variables = '' | |
if "'{{cb}}'" in replace: | |
variables = ' vars:\n - name: "cb"\n type: "clipboard"\n' | |
snippet = f""" | |
- trigger: "{trigger}" | |
replace: "{replace}" | |
{variables} | |
""" | |
with open(f"{espansoConfigPath}/match/base.yml", "a", encoding="utf-8") as f: | |
f.write(snippet) |
This file contains 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
- trigger: ":::" | |
replace: "{{output}}" | |
vars: | |
- name: folderNames | |
type: shell | |
params: | |
cmd: "echo one" | |
- name: form1 | |
type: form | |
params: | |
layout: | | |
Please enter the espanso Triggerphrase, the desired Replacement: | |
Trigger [[trigger]] | |
Replace [[replace]] | |
fields: | |
replace: | |
multiline: true | |
- name: output | |
type: script | |
params: | |
args: | |
- /Users/bman/.pyenv/shims/python | |
- "%CONFIG%/scripts/addSnippet.py" | |
- "%CONFIG%" |
Thanks for sharing. If it helps any other less tech-savvy Mac users adding this, the default path for Python is sometimes /usr/local/bin/python3
Thank you so much for sharing this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions
scripts
and add addSnippet.py to that folder. You can find the espanso config path by typingespanso path
in the shell.base.yml
in thematch
folder of your espanso configurations.Now each time you type three colons
:
consecutively an espanso form will appear to add a new snippet. Couple of notes about this espanso form:Source
Based on a code shared on reddit.