These tools also run shells which differ from that of Terminal.app, which means that a couple of tweaks may be needed to run scripts which use the FoldingText CLI
With default Terminal.app settings, the command line:
Explicitly export any paths your FT CLI script may need:
# A shell run by tools like KeyBoard Maestro, LaunchBar, Alfred etc may not pick up the user's bash paths
if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then
export PATH=$PATH:/usr/local/bin
fi
don’t rely on the default directory being the same as in Terminal.app, and
be aware that some familiar tools may differ slightly (in their handling of command line switches) between /bin/bash and /bin/sh
For example, instead of
wc -lm
you might have to run wc twice
wc -l
wc -m
As an example, I have updated txtquery.sh to run with KeyBoard Maestro
and I’ve given a simple example of how you might pass command line switches to it through a Keyboard Maestro string variable.
I’ve updated the example in the repository, and the screenshot above, to reflect the discovery that the character count returned by wc -mfilename can diverge significantly between Terminal.app and a KeyBoard Maestro Execute Shell Script action, unless we make sure that a UTF-8 setting is explicitly exported for the $LC_CTYPE locale setting.
(KM’s /bin/sh seems to default to LC_CTYPE="C" which deprives wc of the capacity to identify multi-byte characters, and makes the behaviour of wc -m simply fall back to that of the wc -c byte count.
Thus we also need something like:
# Ensure that the locale is a UTF-8 setting
if [[ "$LC_CTYPE" != *"UTF-8"* ]]; then
export LC_CTYPE="UTF-8"
fi
(Doing this will enable the txtQuery.sh script to correctly keep track of the boundaries between different files in a multi file query)
I’m glad that seems useful. As an update - the following (slightly more fine-brushed) version should, I think, ensure that all language_region settings are preserved (and in UTF-8) in /bin/sh shells called by tools like KM:
#!/bin/bash
LANGSTATE="$(defaults read -g AppleLocale).UTF-8"
if [[ "$LC_CTYPE" != *"UTF-8"* ]]; then
export LC_ALL="$LANGSTATE"
fi
or in an Applescript:
do shell script "
LANGSTATE=\"$(defaults read -g AppleLocale).UTF-8\"
if [[ \"$LC_CTYPE\" != *\"UTF-8\"* ]]; then
export LC_ALL=\"$LANGSTATE\"
fi"