# Calling the FoldingText Command Line interface from KeyBoard Maestro etc

**URL:** <https://support.doubledogsoftware.com/t/calling-the-foldingtext-command-line-interface-from-keyboard-maestro-etc/920>\
**Category:** Uncategorized\
**Created:** [August 5, 2014, 5:47pm UTC](https://support.doubledogsoftware.com/t/calling-the-foldingtext-command-line-interface-from-keyboard-maestro-etc/920 "2014-08-05T17:47:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![complexpoint](https://support.doubledogsoftware.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@complexpoint](https://support.doubledogsoftware.com/u/complexpoint)\
**Post date:** [August 5, 2014, 5:47pm UTC](https://support.doubledogsoftware.com/t/calling-the-foldingtext-command-line-interface-from-keyboard-maestro-etc/920/1 "2014-08-05T17:47:28Z")

</div>

Following an earlier post on [Calling the FT CLI from Applescript](http://support.foldingtext.com/t/a-hello-world-for-calling-the-foldingtext-taskpaper-cli-from-applescript/370), 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:

```auto
echo $BASH; echo $PATH

```

might return something like:

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

```

But in Keyboard Maestro:  
 ![](https://support.hogbaysoftware.com/uploads/db8375/189/d61c299a988ea63d.png)

So:

- Explicitly export any paths your FT CLI script may need:

```auto
# 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

```auto
wc -lm

```

you might have to run `wc` twice

```auto
wc -l
wc -m

```

As an example, I have updated [txtquery.sh](https://github.com/RobTrew/txtquery-tools/tree/master/perspectives.ftplugin) to run with KeyBoard Maestro

and I’ve given a [simple example](https://github.com/RobTrew/txtquery-tools/tree/master/perspectives.ftplugin) of how you might pass command line switches to it through a Keyboard Maestro string variable.

![](https://support.hogbaysoftware.com/uploads/db8375/205/1ade5eff689f7f98.png)

---

<div class="post-metadata">

**Author:** ![complexpoint](https://support.doubledogsoftware.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@complexpoint](https://support.doubledogsoftware.com/u/complexpoint)\
**Post date:** [August 8, 2014, 11:12pm UTC](https://support.doubledogsoftware.com/t/calling-the-foldingtext-command-line-interface-from-keyboard-maestro-etc/920/2 "2014-08-08T23:12:03Z")

</div>

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:

```auto
# 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)

---

<div class="post-metadata">

**Author:** ![raguay](https://support.doubledogsoftware.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@raguay](https://support.doubledogsoftware.com/u/raguay)\
**Post date:** [August 9, 2014, 4:01am UTC](https://support.doubledogsoftware.com/t/calling-the-foldingtext-command-line-interface-from-keyboard-maestro-etc/920/3 "2014-08-09T04:01:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![complexpoint](https://support.doubledogsoftware.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@complexpoint](https://support.doubledogsoftware.com/u/complexpoint)\
**Post date:** [August 9, 2014, 10:12am UTC](https://support.doubledogsoftware.com/t/calling-the-foldingtext-command-line-interface-from-keyboard-maestro-etc/920/4 "2014-08-09T10:12:00Z")

</div>

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:

```auto
#!/bin/bash

LANGSTATE="$(defaults read -g AppleLocale).UTF-8"

if [["$LC_CTYPE" != *"UTF-8"*]]; then
	export LC_ALL="$LANGSTATE" 
fi

```

or in an Applescript:

```auto
do shell script "
LANGSTATE=\"$(defaults read -g AppleLocale).UTF-8\"

if [[\"$LC_CTYPE\" != *\"UTF-8\"*]]; then
	export LC_ALL=\"$LANGSTATE\" 
fi"

```
