# TextExpander: Inline Calculation

There are many options these days for carrying out ad hoc calculations. Personally I ditched the desktop calculator as soon as I got a personal computer for my day to day work - after all a computer is a very capable calculator. But that left me with various options of how to carry out a calculation. On the Mac there’s the calculator app and following in the steps of launchers like Alfred even Spotlight allows you to carry out quick calculations. If you have a browser open you could even tap into the calculation potential of engines such as Google search of Wolfram-Alpha. However in order to retain my clipboard and tap into something that allowed for some more complex calculations I created a TextExpander snippet to help with this.

The snippet calls a BASH script that begins by prompting the user to enter a calculation. This calculation is passed to a command line utility called bc (bench calculator) which carries out the calculation. The result of the calculation isn’t always formatted in a nice way so the output is held in a variable so we can give it a bit of format based post-processing.

The next step uses printf to set the output to what I find a reasonable size and scale to work with. This is then stripped of any spaces that this might introduce to ensure we only have a numeric value to work with.

Finally the result can still have vestigial zeros at the end so I used a sed (stream editor) instruction (which I cribbed from a thread on linuxmisc.com) to deal with this.

The resulting snippet places the result of the calculation directly into the current application and the clipboard remains untouched.

 Label Calculate (BASH) Description Inserts the result of a calculation. Abbreviation bccc Content ``````#!/bin/bash #Enter calculation tecalc=\$(echo "%filltext:name=Calculation:width=50%" | bc -l) #Format for 12 characters & decimals and remove spaces tecalc=\$(printf %12.12f \$tecalc | tr -d ' ') #Remove trailing zeros after the decimal point tecalc=\$(echo \$tecalc | sed 's/\(\.[0-9]*[1-9]\)0*\$/\1/;s/\.0*\$//') #Output without a newline echo -n \$tecalc``````

This snippet can be downloaded as part of the Thought Asylum Shell commands snippet group.

Author: Stephen Millard
Tags: | textexpander |

• ## Automation with Templater for Obsidian

10 Jul 2021

Obsidian is my current personal knowledge management tool of choice. The primary reason for this is undoubtedly because it utilises plain text Markdown files, which gives me flexibility for the future, and access to easily process notes using any other text processing tool of choice. A second factor is the range of plugins available for the application. One of my absolute favourites is Templater, a plugin for templating within Obsidian, and I’m going to explain in this post one of the ways I use it to automate my use of Obsidian.

• ## Automators Episode 68 - Guest

20 Feb 2021

Towards the end of 2020 I was caught by surprise and asked to appear as a guest on one of my favourite podcasts; Automators from Relay.fm. As the name implies, this is a podcast about automation, and is very much based around end-user personal automation rather than larger business and enterprise automations. Having never been on a podcast before, I took the plunge and accepted.

• ## TextExpander: Mac Version Details

30 Jul 2018

Occasionally, I have a need to know the precise version of the OS of the Mac I’m using. This is particularly true when I get involved with beta testing software for developers. Whilst I can get the information manually each time, this was something that was ripe for automation. To that end I put together a shell script-based TextExpander snippet to output the information for me.

• ## TextExpander and External Keyboards on iOS

24 Sep 2017

TextExpander is a fantastic tool that fundamentally allows you to enter a string of characters and trigger a response that can replace the text with different content (e.g. plain text, formatted text, an image). On some platforms it can do even more, triggering scripts; though generally the intent is to then return and output some content.