Years ago, I discovered the convenience of treating the Spotlight search as a calculator. If you paste in an equation, the top result will be what the calculator app produces given the math you’ve typed or paste in. I take a lot of notes in my work and frequently have a simple equation to calculate or numbers to sum or subtract. It’s easy enough to copy and paste an equation into spotlight or paste a set of numbers (with some manual clean-up) to Excel or even search & replace with plus or minus signs & paste into spotlight, but you’d be surprised how much more efficient it feel to do this automatically. You can even have it paste the result after the end of the highlight.
Combine this with the fact that many text editing apps allow you to select columns of text, and it becomes a trivial matter to do things with masses of numbers in any text and any app.
The sum & subtract automator services also conveniently strip out non-number characters, so you can highlight any numbers that have text in between and sum or subtract them.
Let’s start with the Sum service. Here’s an example of how it works:
As seen above, you simply:
- Select text with the numbers you want to sum
- Right-click the selection, and
- Select Services->Sum.
The numbers are parsed and added using Spotlight. In the demo above, I simply tap the escape ket to exit spotlight after seeing the result. But, you could select & copy the result to use it how you wish, directly from the spotlight result listing.
The subtract service works the same way as the Sum Service above, but instead of adding all the numbers, it simply takes the first number and subtracts all the rest. Both services ignore non-numbers and can handle decimals. It even knows the difference between hyphens in words versus negative numbers. However, they may have some trouble with non-standard hyphen characters and periods in full sentences.
Calculate (& Append Result) Service(s)
The Calculate Service (or “Calculate & Append Result”) will take any equation that Spotlight (i.e. the Calculator app) can handle and compute the result. And if you know already that you will want to append the result after the highlighted text in the form of ” = answer”, you can alternatively select the “Calculate & Append Result” Service. Here is a demo using the service:
- Go to the github gist containing the Applescript code for each service
- Copy the code from one of the 3 files in the github gist
- Open Automator.app
- Select service “Service”/gear icon from the dropdown sheet & click “Choose”
- Drag the “Run AppleScript” action into your workflow
- Replace the purple code in the Run AppleScript action with the code you copied in step 1
- Save the workflow and name it however you would like it to appear in the services contextual menu (E.g. “Calculate & Append Result.workflow” – the extension will not appear in the menu)
- Repeat for the remaining 3 services.
The workflows/services will be saved automatically to your Library/Services directory in your home directory. If you right-click the file name at the top of the window in Automator, you can select the Services folder to reveal it in the Finder. You can then copy that file and send it to any other computer you would like to also have that service.
Disclaimers: These services are only intended as a quick and dirty solution to work in any context, & any app. If you have a repeated common use-case, consider other solutions. Note also that any application which reserves the arrow keys for some function when the shift key is held down, other than modifying the most recent selection, this service will fail. Some applications, such as java applications, modify selections using shift-arrow navigation differently, depending on the direction of the mouse drag during text selection. This can produce unexpected results. A work-around for both such issues can be to use the strategy used for Terminal.app, but this would required modification to the code. A few of the features in the script rely on some tricks such as statically set delays and command-line calls, necessary to either wait for an application to respond or to control the focus of various windows. If your computer is very busy or has any configuration issues, the proper functioning of these services may be disrupted. These services were developed and tested on macOS High Sierra, 10.13.4. They may or may not work in other macOS versions.