7

I'm trying to refine a script I use within an Automator program and I would appreciate your help on this.

The program is designed to mark a telephone number into clipboard make a tel:link and send it to an telephone application, which works fine. I only have a problem if the marked phonenumber looks something like this: +49(1)123/12345 because then I have to remove all the special characters and leave only the digits and the + in front of the number.

The problem is I'm not that good at scripting, so how to do it?

  • See answer below. If you need help in integrating this into whatever you already have please add the relevant parts of it to the question (for Automator, screenshots are fine). – nohillside May 9 at 12:32
  • This should be a comment, but I have not enough reputation yet. Removing all special characters of "+49 (0)30 12345-67" (common format in Germany and Austria) would result in an invalid number. The number is either "030 123 1234567" (inside Germany) or "+49 30 1234567" (outside Germany), but removing every special characters results in "490301234567" (note the extra "0") which is not the same number. – andii1997 May 9 at 21:15
  • @andii1997 good point! Which is why one never should use the +49[0)30 notation, especially because nowadays phone systems are smart enough to do the right thing when dialing +4930… within Germany. – nohillside May 10 at 5:58
13

The shell snippet

pbpaste | tr -cd "[:digit:]+" | pbcopy

will take the content of the clipboard, remove all non-digits and put the result back into the clipboard.

5

An AppleScript solution:

set the clipboard to the words of (the clipboard) as text
  • 2
    Without seeing the workflow it's hard to say which of the two answer so far are better, but a +1 is in order nonetheless. – user3439894 May 9 at 14:15
  • 2
    I'd say the shell snippet is the "catch all" solution that will work in the instances where there are letters as well, e.g. if the OP copied a person's name and number to the clipboard. But, if it's only to cater for the non-digits that typically can be found in telephone numbers, then this is fine. – CJK May 9 at 14:30
  • 1
    Does this answer leave the + sign behind, per the requirements? – trlkly May 9 at 18:34
  • 2
    @trlkly, Yes it does; however, this method is only good if what's on the clipboard is e.g. +49(1)123/12345 . If it e.g. John Doe +49(1)123/12345 it will return: JohnDoe+49112312345 So as CJK mentioned in his comment "I'd say the shell snippet is the "catch all" solution that will work in the instances where there are letters as well". – user3439894 May 9 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .