TextExpander: Inline Calculation30 Nov 2014
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.
The resulting snippet places the result of the calculation directly into the current application and the clipboard remains untouched.
|Inserts the result of a calculation.
This snippet can be downloaded as part of the Thought Asylum Shell commands snippet group.