Cleaning phone numbers
December 31, 2024 at 10:56 AM by Dr. Drang
It’s been a while since I ran into a website that asked for a phone number and didn’t know how to clean it of non-numeric stuff like hyphens, parentheses, etc. But today I did, and I realized that I didn’t have a Keyboard Maestro macro that would clean a telephone number on my clipboad before pasting it. Now I do.
There’s basically nothing to it, and I’m not sure why a macro like this wasn’t already in my KM library. The macro has just one step:
It takes what’s on the clipboard, passes it to the shell command,
tr -dC [:digit:]
and then pastes the output.
The ‘tr’ command is generally used to substitute (translate) one set of characters for another. If the -d
option is given, it deletes the characters given as the argument; but if the -C
option is also given, it deletes the complement of the given characters. The [:digit:]
argument means the given set of characters consists of all the digits, so the command as a whole deletes every character that isn’t a digit.
I was going to trigger this with some keyboard shortcut involving V, since that’s the letter commonly associated with pasting, but I already have enough of those. Instead, I set the trigger to the typed string ;pbn
, which I will try to remember as “pasteboard number.” The idea is to make it similar to the pbcopy
and pbpaste
commands, which I use frequently. We’ll see how that works.
I should mention that I already have a Typinator snippet that enters my phone number (digits only) when I type ;cell
. The ;pbn
macro is for times when the website needs another phone number. Today, for example, I was entering an emergency contact number that I’d copied out of the Contacts app.
Given that most websites are smart enough to clean up telephone numbers on their own, I probably won’t need this macro very often. But if I get in the habit of using it whenever I need to paste in a phone number that isn’t my own, maybe it’ll stick in my head.