Convert Phone Number to XXX-XXX-XXXX Format

I'm looking for a macro that will add the dashes to a 10 digit phone number so it is in the format XXX-XXX-XXXX. I'm guessing that by now someone has already done this so no need to figure it out.

1 Like

I can see that all I need is something like:

substring 1-3 & “-“ & substring 4-6 & “-“ & substring 7-10

but not sure the proper KM syntax for this kind of command

Howdy, this is what I use. Basically it takes 10 digits, removes any spaces or existing dashes it may find (in case in my haste to type I put one where it didn't belong), then adds dashes in the correct places. It's a very simplistic macro that I'm sure could be done better but it works for me.

image

Reformat phone number.kmmacros (7.4 KB)

2 Likes

I'd do the same, great minds think alike :slight_smile:

I appreciate your approach but I maintain that using the substring command if it exists in KM would be simpler since it would define the end result phone format all in one simple step.

To all the experienced macro nuts here, please post the format of the substring command and with a demonstration of how we can concatenate the parts of the string together.

1 Like

Is @cdthomer 's solution easier than a straight regex solution like this one? I can't see the advantage of using substrings to the direct regex approach, there might be one I don't know of.

This was done by @ccstone on another thread. Does anybody know what kind of dash should be used by the way? hyphen, en dash or em dash... ? I have always used the hyphen for dates. I don't format phone numbers this way though.

To get the formatting you want @Life_Crafting I think the last but one action in @ccstone 's macro should have the 'replace with' box changed from ($1) $2-$3 to $1-$2-$3

I tested this and it works fine if you select a single phone number and copy it onto the clipboard. I am sure you could use modified regex that picks up line breaks on a long list if the numbers were on seperate lines and save even more time.

I am not sure exactly how to do that to pick up the pattern line by line as it were. Doing this kind of thing one by one is tedious very often? You could speed up ccstone's macro @cdthomer 's way in fact, automating a lot of the copy paste and select keystrokes. I would use the system clipboard too: why do you use a named clipboard @cdthomer? Is there a specific reason for saving them in such a store?

1 Like

Try the regex solution I found by @ccstone . It could be speeded up, as I say in another reply, using @cdthomer 's keystroke actions. Seems to my mind the neatest solution if you are doing these one at a time so to speak. Are you trying to do them in batches though? I realize that might be what you are going for?

Howdy! Because I have so many copy/paste macros, some of which I run at the same time, that I like to keep certain ones separate from the system clipboard. Hopefully this avoids pasting the wrong information into the wrong variable or the wrong work window.

1 Like

Thanks I can see the rationale. Useful tip.

1 Like

I like his a lot more because it takes out any other characters that might have been included in the string of text and formats the phone number without relying on keystrokes that may or may not be placed in the correct spot.

1 Like

I think that too. I would incorporate some of your keystrokes into the copy paste select procedure though and it makes a fine Macro. I should be able to do this on a list but I can't remember the regex now. I have a lot of keystroke macros like yours in fact, and they work fine mostly to be fair.

I just updated mine doing that very thing, keystrokes to copy the string of text, and then used his portion to format it. It works great.

But yea for a lot of basic things I use keystrokes because they're very easy to create a macro really quick that way.

1 Like

I hope @Life_Crafting finds it useful too!

Here you go. I would use the method by @ccstone, but this answers your question.
Since KM does NOT have a substring function like every other language in the world, it forces us to use an Action, so we have to extract each substring separately.

MACRO:   Add Dashes to All Didgits Phone Number [Example]

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/3/3/339f3f1ea49a3cbabeccd934553bb34bd07feb69.kmmacros">Add Dashes to All Didgits Phone Number [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---


<img src="/uploads/default/original/3X/c/f/cff8c86874e8e84cc97bc1faf1ab83183f51e007.png" width="564" height="1026">
1 Like

Personally, I'd just use a regular expression - get rid of everything except the digits, ensure there are exactly 10 digits, and then add the dashes:

image

4 Likes

thanks, that one was very educational for me. Just out of interest when would the substring method, so to speak, be useful?
I learnt several things. It is useful somehow when a Variable name is transparent like in this example: I don't know why that is. It makes it a lot clearer conceptually to beginners I feel. "VariableName" confused me for a long time.

@JMichaelTX , though I prefer the regex version, this was the first time I have understood or even knew about the 'substring' actions and it was very useful to know. I have really been reanimated regarding regex via Keyboard Maestro in fact: I had a rough idea about it in other contexts.

It would be most useful to people who need to extract a substring and either know, or can get, the character range in the main string, and who do not know RegEx.

Personally, I have rarely used it. The macro I posted above was just for the request of the OP -- it would not be my solution.

By "transparent" I suppose you mean that the name is descriptive of its contents. I always try to do this, while keeping the length of the name to a minimum.

But your comment is why I so dislike the default that KM provides for many Actions when you insert the Action, like this:

image

I think this is especially confusing to new KM users. If an app gives me a default value, I generally take that to be a good value unless I need otherwise. So I think many users might just accept "VarName" as the actual variable name for this action.

They probably don't realize that this is a global variable, and the same default name provided by other Actions, even in other Macros, might conflict with the variable used for this specific Action.

So, I NEVER accept the default variable name. Most of the time I use Local (or Instance) variables which are unique to the Macro where they are used.

1 Like

I agree with you @JMichaelTX , thanks for explaining the substring useage too.

It is only now I am starting to see what I am supposed to do in the variable dialogue box. You create a label really? I realize as well that if you name every variable the same, and I think you are right I think I was doing that at one point, you might get conflicts. I got that you were supposed to enter something of your own in some way in time. Even then it was confusing.

Then I was trying things like "VarPhone" and thinking it was supposed to 'set' something elsewhere or something. I said to Peter I would keep a record of the learning experience. It is very hard to do though, once the fog lifts things look clear ironically.
"VarName" looks to me like a function in some way in other words. Now I am sure it doesn't look that way to modern IT folk, but there you go. It looks to me intuitively, and I still find it hard to shake off, like (2x) would in a math formula. Do you see what I mean?

Thanks @peternlewis that clarified a lot for me incidental to the main topic. See my other reply.

The default variable name of the Set Variable to Text action is the last variable you used.

Of course - almost everything seen the first time is incomprehensible. Just look at a door knob - if you'd never seen one, you would have no idea what to do with it.

1 Like