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).

Automator Services for finding coordinates in DNA/AA strings

Do you write code to analyze or modify DNA or proteins?  Do you do your work on a Mac?  If so, I have a few Automator Services, written in AppleScript, that you may find very handy:

  1. Get the sequence (& alignment) length of a selected nucleotide string
  2. Get the sequence length of any selected string (e.g. protein or quality string)
  3. Show where a coordinate is in any selected string (including white-spaces)
  4. Show where a coordinate is in a selected sequence (e.g. protein or quality string)
  5. Show where a nucleotide coordinate is in a selected sequence
  6. Show where an alignment coordinate is in a selected nucleotide sequence
  7. Get the reverse complement of a selected nucleotide sequence
  8. Guess the barcodes present in a FastQ file *NEW

With these, you can highlight a sequence anywhere in any application and either get the selection length or show where a supplied coordinate is in the selected sequence.

Each service, once installed, will show up in the contextual menu that shows up anytime you right-click any selected text, system-wide on your mac, under the services sub-menu, e.g.:

countntsservicemenu.png

Here are the full details of how to use each service and what it does:

1. Get the sequence (& alignment) length of a selected nucleotide string

Name: Count Nucleotides

countntsoutput.png

This service does a bit more than count nucleotides.  As seen in the example on the right, it reports the number of nucleotides in the selection (sequence length), the length of the selected alignment, the number of discrete & ambiguous nucleotides, and a breakdown of all case-insensitive sequence characters (including gaps and ambiguous nucleotides).

Spaces, tabs, newlines, carriage returns, numbers, or any other non-sequence characters are completely ignored, so you can select any sequence, even if it is formatted & displayed with coordinates.  Only the sequence found inside the highlighted text is considered.  The first selected sequence character is coordinate 1.

2. Get the sequence length of any selected string (e.g. protein or quality string)

Name: Count Sequence Characterscountnonwschars.png

This service counts every character selected except for spaces, tabs, newlines, and carriage returns.  It’s good for getting the length of selected unaligned protein sequences (with no formatted coordinates in the selection) or of quality strings.  Note, there is currently no service for aligned amino acid sequences, but if you would like such a service, let me know in the comments and I’ll whip one up.  I work mostly with DNA, thus I haven’t had much need for aligned protein coordinate determinations.

3. Show where a coordinate is in any selected string (including white-spaces)

Name: Select N Characters

This service works only on “solid sequence” (i.e. having no whitespaces, hard returns, or for that matter: any non-sequence characters).  See services 4-6 for sequence-specific functions.  The way this service shows where a coordinate is, is by changing the length of the selection.  The resulting last character of the selection after the length modification is the length supplied by you, the user.  For example, if you tell it to select 4 characters in this string you selected: ATGCCGTAG, the selection will end up as: ATGCCGTAG.

lengthprompt.png

There are 2 ways to supply the coordinate.  The default way is to grab the coordinate from the clipboard, so all you have to do is copy the number you want to use to set the selection length.  However, if the content of the clipboard is not a number, a popup window will appear to ask you to enter the desired selection length.

 

To use this service:

  1. [Optional] Copy a number/length indicating the amount of the sequence you want to select.
  2. Select any length of sequence from the start position (position 1)
  3. Right-click the selection and select Services -> Select N Characters
  4. [If you didn’t do step 1] Enter the length of sequence you want to select

This script alters the selection length of the selected text you right-clicked on in the window in which you clicked on the sequence, regardless of application.  However, if you are in Terminal.app, it displays the selection result in a popup window instead of in the Terminal itself.  This is because the modification of the selection length is accomplished by shift-arrow keystroke emulation and this is not a means by which you can modify a selection in the Terminal app.  This has 1 side effect.  Normally, if the entered length is longer than the selection, in any other app, that’s not a problem and the selection just expands, but in Terminal’s popup result window, all the service has access to is the selected text, so placeholder ‘N’s are appended to show the desired sequence length.

lengthresult-terminal.pngThere are other drawbacks to this Terminal selection-length work-around.  The font is not fixed-width and the width of the popup window is fixed at a fairly narrow size, thus large sequences cannot be displayed very well.

Since the selection modification happens via emulated keystrokes, it takes a little time for the final selection to be made, but you’ll see how fast it goes as you watch the selection being made.  Since it’s not instantaneous, the script will either adjust the selection from the end or select anew from the beginning for efficiency.

4-8. New services

Names: Select N Sequence Characters, Select N Nucleotides, Select N Alignment Characters, Reverse Complement, & Guess barcodes

These services operate just like Select N Characters, but take the character type into account.

Select N Sequence Characters doesn’t include whitespace characters such as spaces, tabs, and newlines in the calculation of a coordinate in a string.

Select N Nucleotides doesn’t include non-nucleotide characters such as white spaces, gap characters, numbers, etc. in the calculation of a coordinate in a string.

Select N Alignment Characters behaves just like Select N Nucleotides but includes gap characters in the calculation of a coordinate in a string as the one exception.

Guess Barcodes allows you to right-click on a file and find out what the likely barcodes are.  It takes a little bit to run and makes some common assumptions, but if you think the results are wrong, you are given the opportunity to tweak the parameters and run again.

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. “Count Non-Whitespace Characters.workflow” – the extension will not appear in the menu)
  8. Repeat for the remaining 6 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.

Just try your new services out by right-clicking on selected text anywhere.selectncharsexample.gif

And as you can see from the example above, Select N Characters works on any text.

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 Sierra, 10.12.6.  They may or may not work in other macOS versions.

Today’s Apple Feedback: Messages on an iPhone 6s with Sprint do not convert to SMS like they did on the iPhone 4s

Immediately upon my wife and I upgrading our pair of iPhone 4s’s to 6s’s, I called to complain that I was not receiving messages from my wife while on calls anymore.  I was given the run-around both from Sprint and from Apple, stating that, as I already knew, you can’t do data and phone calls at the same time on Sprint’s CDMA network. BUT, you can get SMS’s while on a call and after some testing, I figured out that there was nothing wrong with our new phones receiving SMS messages.  The problem was on the other end – with sending them.

With our 4s’s, both with iMessages enabled (you can confirm this from our message history), if the message wouldn’t go through (because I was on a call), her phone would convert the message to a (green) SMS and try sending it again. I’d receive it while on a call and hear the message chime over my phone call.

This does not work on an iPhone 6s, though I don’t know if it was specific to the hardware itself or if it could have had to do with a newer iOS that came with the 6s’s that couldn’t be installed on a 4s.  On a 6s, Apple appears to have dumbed down message sending.  It will only attempt to send a message as an SMS is the sender has no wifi or data connection.  It no longer pays any mind to the receiver.  If they’re on a call, the message just gets buffered somewhere and once the other person gets off their call and again connects to a data signal, whether it’s a phone carrier’s data connection or a wifi signal, they get all their buffered messages at once.

I commute to and from work on my bike.  It’s an hour ride and I like to pass the time by talking on the phone.  My wife invariably messages me while I’m on my call, either about dinner or wanting to know when I’m getting home or whatever.  I pretty much expect to hear from her every time.  I would usually hang up whatever call I was on (usually some family member) and ask Siri to read my messages to me while I rode.

I thought that when we bought the 6s’s, they would be better and more capable.  I did not expect their capabilities to regress, so I’ve called to complain about this numerous times. I’ve tested with numerous iPhones and Android phones.  My latest feedback to Apple is below:

Convert iMessages sent to me to SMSs during calls on sprint

I’m submitting this issue again. When my wife and I bought our iPhone 6s’s about a year ago (staying with sprint), the first thing I noticed that they could not do that our 4s’s could was automatically convert an iMessage to an SMS when the other one of us was on the phone. E.g. I ride my bike for an hour commute and I talk on the phone the whole time. With our 4s’s (on Sprint), when my wife sent me a message (with iMessage enabled), which she always does at some point during my ride, the message would get converted to an SMS and I would receive it while on the call. But with our 6s’s set up the same way, I do not get her messages until I hang up the phone, i.e. her phone does not convert the iMessages to SMS’s. We’ve been with sprint for forever and our 4s’s could do this.

iOS 11.0.3

In fact, some Apple support people I’ve spoken to had the same complaint about not receiving messages.  Apple has been unresponsive on this issue and it’s ridiculous to have to turn off iMessages to be able to get around this issue.  So I am blogging this out there to raise awareness that it doesn’t have to be this way!

Complain to Apple about this issue and have them fix their newer phones to restore the capability of converting iMessages to SMS’s the way the 4s used to do.  If enough of us complain, maybe they’ll deign to get around to it.

How to make Safari 10 auto-open specific file types

I’ve been annoyed for awhile now that Safari was auto-opening text files that I was trying to download in rapid succession via option-click on a series of links. The text documents kept popping up in front of my browser window and getting in the way.  Safari used to have preferences that you could set to tell it what to do with specific file types, but not anymore – at least not since Safari 10 on MacOS Sierra (10.12.x). The only options were to make Safari auto-open “safe” files or not auto-open any files at all. I was about to give up again.  I’ve tried to figure this out on multiple other occasions, but I’d finally had enough and decided to roll my own using Automator and some clever scripting… Continue reading

Today’s Apple Feedback: Group Emails Send to All Email Addresses of Each Contact on iOS

I used to use Groups in the Contacts app a lot, sending a weekly newsletter to over a thousand people each week.  Yes, I know that’s a silly way to do it, but it was a long event email that was mostly automatically generated (via a perl script I wrote) and I had a cron job that emailed me the automatically generated portion each week.  Plus, this was something I’d set up over a decade ago, before MailChimp was really a thing.  All I had to do was add my little blurb and type in the name of the group, and I’d be done in under 5 minutes. It worked very well for me. Eventually, when Mobile Me got sunsetted and iCloud took over, Smart Groups were no longer supported, which was rather annoying, so I ended up maintaining 2 copies of the group: the smart group on one computer, and a static copy of the group that I occasionally refreshed that I could use on any iCloud-synced computer. Ever since then, bugs pop up all the time relating to Contacts and I’ve reported each one. Most of them have to do with Siri’s usage of Contacts, but they also often have to do with the propagation of duplicate Contact Cards.  This new bug however, has to do with how the Apple iOS Mail app handles contact Cards in a static group.  Here was my feedback to Apple today after a chat session with a Senior Apple Support Advisor:

I created a group in my iCloud contacts called “Settlers of Catan”, which includes my wife, for whom I have 4 email addresses. When I type “Catan” into the “To:” field of an email in Apple’s iOS Mail app and select the group from the list of options in the drop-down, it adds all 4 of her email addresses, which I hadn’t noticed until she complained that I was sending her 4 copies of each email to the group. This does not happen when I do the same thing on my MacBook Air (the Apple Mail.app on the computer only adds the first email). It does also happen on my iPhone 6s. I chatted with a Senior Support Advisor from Apple and it turns out he has the same problem and was unable to resolve the issue. He submitted this same feedback as well. Please fix this.

The advisor I chatted with had had me try a number of things, including creating a new group, etc.  I even upgraded my version of iOS to the latest (11.0).  He was unable to fix the issue. All he could do was suggest I create a duplicate contact card for my wife that only had one of her email addresses.  I told him I’d rather manually edit the email’s “To” field after selecting the group, but thanks.  So he suggested I submit the feedback above.

I took a screenshot which shows that by selecting the group, it would add 4 email addresses for my wife (Zoë) even though the group had only 1 Contact Card for her in it:

IMG_0146.PNG

While chatting with the specialist, I mentioned another Contact-related issue and the advisor believed that it was related (because it had to do with determining what to do when dealing with ambiguous situations and clarifying what the user wants). And it had to do with how Siri responds when instructed “Tell my _____…”, such as: “Tell my mom I’m running late”. This is an instruction that relies on relationships stored in my Contact card to link it with other contact cards, such as my mom “Jane Doe”.  Often times, Siri’s response is something like this: “Which Jane Doe? Tap the one you want”, and it gives me a list containing 2 names: my mom’s name and my own name.

Here’s what I think is happening here… Siri is supposed to clear up ambiguities if she doesn’t have enough information. If I said “Tell John…” and I have multiple contacts named John, Siri should ask me which John she’s supposed to send the message to. Likewise, if I happened to accidentally add another mom relationship to my card, Siri should ask me which mom I want.  Or if I try to message mom, but she has 2 mobile numbers listed, Siri should ask me which number to use. However, I think that in the case of relationships, Siri is blindly searching for occurrences of the name I specify in my contact cards and it’s finding them in my contact card because it’s there as a relationship. So when I instruct Siri to tell my mom…, she finds the card for Jane Doe because the name matches and she finds my card because her name is entered in my card as a relationship!

I have a number of relationships saved, like my mom, dad, sister, wife, doctor, dentist, etc..  I even have a few relationships I created for an IFTTT phone number.  I added these contacts so that I could ask Siri to send commands via text to IFTTT using hashtags. I have one named “Groceries” that appends text I send it to a groceries.txt file I have on Dropbox that I share with my wife. I have 2 others: “Air Conditioners” to control my AC window units via WeMo and “Mac Mini” to send text to my computer hooked up to my entertainment center so I can enter searches on Netflix without using a keyboard (very handy!). I used to instruct Siri to send messages to these contacts just using their “names”, however Apple eventually tried to make Siri too smart and so when she heard “Tell Groceries bread hashtag add”, she started searching google instead of texting my contact named “Groceries”.  I got around this erroneous behavior by adding the “Groceries” contact as a relationship of type “groceries”. So now I just tell Siri “Tell my Groceries bread hashtag add”.  And that used to work well until this latest issue popped up where Siri asks me now “Which Groceries? Tap the one you want”, listing Groceries and me as the two options to choose from.

The advisor believed that both issues are related, and have to do with how Apple’s software is handling ambiguity.  I’m not so sure.  Yes, they both have to do with ambiguity, but Siri’s logic happens on a remote Siri server and Apple’s Mail app (I believe) is not accessing a remote server to decide which of my wife’s emails to include when composing an email to a group.

Other issues which have also popped up in the paste couple years have been: Siri’s inability to distinguish “in-laws” from my own parents/siblings and Siri’s ability to send text messages to contacts with short phone numbers, like those used to check when the next NJ Transit bus will arrive at my bus stop.  I used to be able to instruct Siri to “Tell NJ Transit 22222” and she would send “22222” (a bus stop code) to the 5-digit NJ Transit phone number.  Eventually Siri started interpreting this command as “Tell NJ Transit to 2222”, which I got around by adding a “to” to my command: “Tell NJ Transit to 22222”.  (That’s not my bus stop number, but it obviously does start with a 2.)  And then of course, Siri just started wholly refusing to send any messages to NJ Transit at all, and I believe it was because the phone number is only 5 digits.

I believe that Apple is trying to make Siri too smart and they are just getting in my way.  I have seen however that Siri does learn some things.  If I keep trying multiple times to get Siri to for example, send a text to “Groceries”, first she does a web search a couple times. Then I tell Siri simple “Send a text message” When I respond to the question asking who I want to send it to with “Groceries”, it works, and then after doing that a few times, she will respond correctly the next time I ask her to “Tell Groceries…”.  Now I’m at the point where as I said, I have to say “Tell my Groceries…”.  And after having used that work-around for awhile, now Siri can’t distinguish between my own card and the one for groceries (or my mom).  When I complained to Apple about the in-laws issue, the advisor I ended up speaking to confirmed the bug on their end by reproducing it and they actually fixed it!  I insisted to them that it used to work, because I frequently used the feature to text my mother-in-law, who was helping me with a house I was selling remotely.

Apple keeps trying to make Siri smarter, and if you ask me, they keep doing it rather sloppily.  They frequently seem to overlook how their various features will behave.  The in-laws issue was a prime example. Someone obviously wrote some code to always parse out “mother” and connect it to a contact whose relationship is “mother”, regardless of whether a contact exists whose relationship was “mother-in-law”.  The existence of that possibility didn’t occur to some programmer out there.  It used to work and Siri even interpreted what I said by typing it out as “mother-in-law”, but would send a text to my mom starting with “In-law…”.

I can’t help but think that this sloppiness started appearing in Apple’s development environment after Steve Jobs died.  They’ve been removing OS features and mucking up functionality ever since.

Turn On/Off Sighthound Security Cam Rules via IFTTT

We just moved, and in my last apartment, we lived on the second floor and I had my Foscam security camera [~$60] (which is controlled by the Basic version of Sighthound Video [$60]) trained on the only stairwell, which only captured people arriving or leaving. However in our new house, none of the doors are in locations where the camera only captures egress & ingress events.  There’s a bathroom right next to one door and the other is between the living room and the stairs.  Thus, anywhere I set up a security camera is bound to capture the regular activity inside the house. So I first experimented with setting the rules to make the cameras active during certain hours on the weekdays, which turned out pretty annoying, because my wife’s schedule is random.  What I needed was a way to turn the camera rules off & on based on when we arrive & leave the house, the same way I use WeMo to control the window unit air conditioners and the air filter.

I discovered that Sighthound has an IFTTT channel and an iOS app, but their IFTTT channel only has triggers upon motion detection and no “actions” that could be run using Life360 (the app I use to control the AC & filter – e.g. turn on when the first person arrives and off when the last leaves or vice versa).  So I emailed Sighthound to see if they had any tips for enabling and disabling camera rules.  They pointed me to one of their forums called Hacker’s Corner, where I got pointed in the right direction.

Here’s how to enable your security camera (rule) when the last person in your family leaves the house and disable the camera when the first person in your family arrives.

Continue reading