A 'hello world' for calling the FoldingText/TaskPaper CLI from Applescript


#1

The CLI for FoldingText TaskPaper and MD enables us to filter and query across several different text files (temporarily concatenated with suitable delimiters) producing reports and perspectives which give a useful overview.

It works very well from the Terminal.app command line, and from shell scripts. Here are a couple of basic notes in case anyone else also wants to call it from Applescript, perhaps to make use of Applescript’s choose from list menus.

Using Applescript’s ‘do shell script’ – three things to take account of:

  1. The shell used by do shell script is not your default Terminal.app shell
  2. Your Terminal.app search paths, working directory, and other settings are not exported to it.
  3. Search paths and other such settings don’t persist between successive calls to do shell script – you need to set things up each time you use it.

Point 1 means that you may occasionally need to watch out for slightly different responses to command line switches.

For example, both Terminal.app and Applescript’s do shell script respond to:

wc notes-2014-07-11.txt

with something like

      49     280    1828 notes-2014-07-11.txt

but while wc -lm notes-2014-07-11.txt in Terminal.app (standard Bash) yields:

      49    1828 notes-2014-07-11.txt

from AS, do shell script "wc -lm notes-2014-07-11.txt"

we just get:

      49 notes-2014-07-11.txt

So if you notice any slight departures from what is happening in Terminal.app, just experiment and test.

Point 2 means that you need to tell the Applescript about any search paths & working directories and perhaps full paths to any executables that you need. In particular:

  • the NPM files needed by the FT CLI are usually installed to /usr/local/bin
  • and you will need to specify exactly where the ft CLI binary is installed on your system.

For example, to run the simple command line test:

$ ft get "//not @done" myfile.ft

from Applescript, you could assemble and pass to do shell script a string like:

export PATH=$PATH:/usr/local/bin
cd "$(dirname '/Users/houthakker/Desktop/test.scpt')"
'/usr/local/lib/node_modules/foldingtext/bin/ft' get '//not @done' myfile.ft

which you could do with Applescript along the lines of:

property pstrPathtoFTCLI : "/usr/local/lib/node_modules/foldingtext/bin/ft"

on run
	callFT("get '//not @done' myfile.ft")
end run

on callFT(strArgs)
	set strScriptPath to POSIX path of (path to me) -- path to this script
	
	set strCMD to "export PATH=$PATH:/usr/local/bin
	cd \"$(dirname " & quoted form of strScriptPath & ")\"
	" & quoted form of pstrPathtoFTCLI & space & strArgs
	
	return (do shell script strCMD)
end callFT

Point 3 means that if you follow this up with another do shell script in the following line, your paths and working directories will not have been remembered - you need a little setup header for every call.


#2

PS, the briefest summary of the differences between the Terminal.app default shell and that of Applescript’s do shell script command probably comes from running the following in both, and comparing their responses:

echo $BASH; echo $PATH