Calling the FoldingText Command Line interface from KeyBoard Maestro etc

Following an earlier post on Calling the FT CLI from Applescript, a quick note on tools like KeyBoard Maestro, LaunchBar, Alfred etc

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:

echo $BASH; echo $PATH

might return something like:

/bin/bash
/usr/sbin:/sbin:/usr/bin:/bin:/usr/local/bin:/opt/X11/bin

But in Keyboard Maestro:

So:

  • 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 -m filename 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)

Thanks for this note. That explains why Alfred workflows and LaunchBar actions get results from wc differently. Definitely keep this in my notes!!

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"