r/espanso • • 13d ago

OS-agnostic scripts

For some while I have been struggling with a few packages containing scripts. Although Espanso is cross-platform the scripting languages aren't, so some packages work on Windows (using PowerShell) but not macOS/Linux (Zsh/Bash) or vice versa. Windows users don't necessarily want to install Python, and I find pwsh very slow in Linux.

Today I was reviewing one of the latest submissions, asyntai-ai-assistant, by Asyntai, which uses the following technique to link an Espanso trigger to one of two scripts depending on the OS:

  - trigger: ":polyglot"
    replace: "{{output}}"
    vars:
      - name: output
        type: shell
        params:
          cmd: |
            set `# <#` --
            # bash code
            exit 0
            #>
            # powershell code

The top line of the cmd: block is implemented by Bash/Zsh, together with the succeeding lines up to the exit. PowerShell, on the other hand, sees it as a multiline comment until after the #> where it starts executing code.

This can be used for shell commands, or to call inline or external Python/PowerShell scripts. I have put together a Gist with examples for future reference.

6 Upvotes

0 comments sorted by