April 29, 2016

Accessing Accordance from the Command Line in Terminal

I was talking with someone today about how you might grab a text reference from Accordance without leaving the command line in Terminal. So, I thought I'd share one way to accomplish it.

You can use Applescript to retrieve the full text of verse references from a given text module, all without bringing Accordance to the front or interacting with its interface in any way. This is how my old scripts and actions have worked, as well as some the OS X Services that OakTree packages with Accordance.
A raw Applescript line to get the text of John 1:1 from the NRSVS module using the citation format (true) is:

tell app "Accordance" to get «event AccdTxRf» {"NRSVS", "John 1:1", true}
You can call that from the shell using osascript:

osascript -e 'tell app "Accordance" to get «event AccdTxRf» {"NRSVS", "John 1:1", true}'
But, no-one wants to type that out each time you want to lookup a verse, so you'll want to create a shell script that passes the verse reference as an argument to the script. Here's an example:

# This script takes a verse reference as an argument to return the full text of the reference to stdout
# chmod +x getverse.sh 
# ./getverse.sh

# set the short name of module to use
# set to "true" to use citation format

# trap for a null argument passed, since Accordance crashes if it receives a null parameter
if [ -n "$*" ] #[ "$#" -gt 0 ] 
# retrieve all passed arguments as one string, so you can pass references with spaces and without quotes: John 1:1

# get the text of the reference from Accordance
textresult=$(osascript -e "tell app \"Accordance\" to get «event AccdTxRf» {\"$modulename\", \"$verseref\", $usecitation}")

echo "$textresult"

    echo "Must pass a text reference as a variable, as in:  getverse John 1:1"
Save that as a text file in your home folder ( ~/ ) with a name like 'getverse.sh'. You'll also need to make it executable by going to the Terminal and using:

chmod +x getverse.sh
After that, you can call the script from the command line in Terminal with:

./getverse.sh John 11:35

An additional, welcome, step is to create an alias that calls that script, to further reduce what you have to type to run it.  Open up your bash profile at ~/.bash_profile (it likely doesn't exist yet, so you'll have to create it),  and then add a line to it to create the alias:

alias gv=./getverse.sh
After that, you'll need to initiate the bash shell environment with this command in Terminal:

source ~/.bash_profile
Then, you can just type: gv John 1:1, 3
to lookup your verse references.

I glossed over some details, assuming that if you're at all interested in doing what this post suggests, you are already familiar with the subjects and functions covered.

1 comment:

Ryan Gustason said...

Very cool stuff. I'll need to play with that Bash script a bit. Looks really interesting, if not terribly useful, unless you write notes in vi. ;)