Skip to content

Instantly share code, notes, and snippets.

@typoman
Last active September 21, 2023 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typoman/6bdd3bd6cbd3afb00ed83f5b2c429910 to your computer and use it in GitHub Desktop.
Save typoman/6bdd3bd6cbd3afb00ed83f5b2c429910 to your computer and use it in GitHub Desktop.
UI form for adding snippets to espanso
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)
- 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%"
@typoman
Copy link
Author

typoman commented Feb 15, 2023

Instructions

  • Inside the espanso configurations path create a folder named scripts and add addSnippet.py to that folder. You can find the espanso config path by typing espanso path in the shell.
  • Put the code from base.yml to the file base.yml in the match folder of your espanso configurations.
  • Make sure to change the python path inside the base.yml to your python path.

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:

  • Add {cb} anywhere inside the replacement to make it replace with the contents of the clipboard.

Source

Based on a code shared on reddit.

@danrejto1
Copy link

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

@jdnguyenswin
Copy link

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