Services to sum or subtract highlighted numbers and calculate equations

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.

Sum Service

Let’s start with the Sum service.  Here’s an example of how it works:

sum_demo.gif

As seen above, you simply:

  1. Select text with the numbers you want to sum
  2. Right-click the selection, and
  3. 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.

Subtract Service

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:

calc_demo.gif

Installation

  1. Go to the github gist containing the Applescript code for each service
  2. Copy the code from one of the 3 files in the github gistcopygistcode.png
  3. Open Automator.appautomatordock.png
  4. Select service “Service”/gear icon from the dropdown sheet & click “Choose”selectservice.png
  5. Drag the “Run AppleScript” action into your workflowautomatorapplescript.gif
  6. Replace the purple code in the Run AppleScript action with the code you copied in step 1
  7. 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)
  8. 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.

Have fun!

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.

Advertisements

Today’s Apple Feedback: High Sierra is a Heavy Sleeper

Since upgrading recently to High Sierra, I’ve been dealing with the fact that my keyboard would no longer wake my computer from the screen saver.  All keyboard presses (other than modifier keys) would only cause the computer to beep (and screen-flash, since I set that in the accessibility preferences).  I finally decided to resolve this issue today, since I am between projects.  It took awhile to dig up a solution via google search-term tinkering, so I thought I would share a work-around I found here.

Briefly, to work-around this issue and get your mac to wake up upon any keyboard press, you must open up System Preferences, click the Mission Control preferences pane, and turn on “Displays have separate spaces”:

mcsepspaces.png

Note, you will have to log out and back in (no restart required though), as is noted in the gray message next to the setting seen in the image above.  After that, the keyboard should start being able to wake your computer.  If you turn this setting back off & log out/in, the problem will recur, so unfortunately, if you have multiple displays, are experiencing this issue, and don’t want them to have separate spaces, you’re out of luck.  I suggest you submit feedback to Apple, as I did below:

Keyboard presses & mouse motion don’t wake from screen saver since High Sierra upgrade

Since installing High Sierra, I have been unable to wake my computer from the screen saver using the keyboard. All keyboard presses result in a beep/screen-flash (since I have that set). A work-around described here works:

https://forums.macrumors.com/threads/screensaver-behavior-in-high-sierra.2075712/

By turning on “Displays have separate spaces” in the Mission Control system prefs, logging out, & back in, the problem no longer occurs. The problem can be made to recur by reversing that process.

I have 10.13.3 (which is not listed below in the select list).