Encoding Problems when using Execute Shell Script

This shell script takes the contents of the clipboard, then converts it using pandoc, then pastes the output back to the clipboard. The problem is that unicode characters get mangled:

Input: An—emdash

Output: AnÑemdash

If I run the same command from my terminal, the output is fine:

> echo "An—emdash" | pbcopy
> pbpaste | /opt/homebrew/bin/pandoc -d text-refs | pbcopy
> pbpaste
An—emdash

Note -d text-refs is a local config, you should be able to get the same result if the pandoc command is pandoc -f markdown -t plain. In the terminal unicode is preserved, but in BTT the output wrongly encoded...

Device information:

  • Type of Mac: M2 macbook air
  • macOS version: 15.3.1
  • BetterTouchTool version: 5.204

I tried running this using Alfred:


And the encoding is fine...

Input: An—emdash

Output: An—emdash

I think I solved this. The technical background is that BTT runs scripts in an empty environment, not the user's environment. That means no shell setup is available. So the $LANG variable is empty, and thus the encoding problem (I think it defaults to latin1, surely utf8 should be default by now!?). The solution is to define $LANG, so the script would be:

export LANG=en_US.UTF-8
pbpaste | /opt/homebrew/bin/pandoc -f markdown -t plain | pbcopy

This seems to preserve unicode characters now.

You add it to this field too:

2 Likes