1

Last month, for a temporary event, I've added a Polish number (+48 prefix) to 800 of my regular contacts using a vCard file.

Now, this event is finished and now I'd like to remove all those temporary polish numbers from my contacts and keep all other information intact.

Is there a way to easily do that, for example, using an Automator action?

  • You say you used a vCard to add the "+48 prefix" to the numbers. Can you show an example of a number from the vCard that had been modified? Does it look like +481234567890 or what in the vCard? – user3439894 Jul 28 '16 at 17:22
  • Numbers are like +48 697 107 394 – LuCouto Aug 1 '16 at 15:50
1

I've deleted my original answer because I initially misunderstood how the +48 numbers were added, sorry about that.

Instead of using AppleScript, I'm going to offer you a different solution because while I could and did modify the original AppleScript code to delete the entire number that had a +48 prefix, nonetheless there were errors that stopped the script from executing completely during the testing phase. So I'm opting for a different solution over debugging the AppleScript code.

This solution requires exporting the entire contents of the Contacts.app to a vCard file (.vcf), modifying it using a RegEx in sed, and then importing the modified vCard file (.vcf) back into Contacts. Note that this method will replace all existing contacts with the updated new allcontacts.vcf file and should not be an issue as it's really only removing the information you stated you wanted removed, just in a roundabout way.

NOTE: These directions were tested under OS X 10.11.5 with the Contacts.app using its default preferences.

  • Open Contacts.

  • Backup Contacts, click: File > Export > Contacts Archive…

Now that Contacts have been backed up, do the following...

  • Select the first contact and then press commandA to select all contacts.

  • Click: File > Export > Export vCard…

  • In the Save As dialog box it will show as, the name of the first contact and n others,
    change it to: allcontacts

  • Copy and paste the following command into Terminal, then press the Space bar to add a space afterwards.

    sed -i.bak -E '/^TEL.*\+48.*$/d'

  • From Finder, drag and drop the allcontacts.vcf file onto the Terminal window. Note that in Finder, the .vcf extension may be hidden and it will show in Terminal.

Example: sed -i.bak -E '/^TEL.*\+48.*$/d' /Users/me/Documents/allcontacts.vcf

  • Now press enter to modify the allcontacts.vcf file. A backup of the original is created as: allcontacts.vcf.bak

Now import the updated allcontacts.vcf file into Contacts.

  • File > Import and select the allcontacts.vcf file, then click Open.

  • On the "Are you sure you want to import n cards?" sheet click: Review Duplicates…

  • On the ensuing sheet select Keep New and check the Apply to All checkbox then click Import.

The Contacts telephone entries containing numbers with a +48 prefix have been removed.


The target lines in the allcontacts.vcf file will have the following pattern and note the example below is not the only way it can present and thus the need to use a RegEx aside from the phone numbers themselves being different as the value for type= can vary too.

Example line in the vCard file: TEL;type=CELL;type=VOICE:+48 697 107 394

The RegEx is: ^TEL.*\+48.*$

Explanation:

  • ^ assert position at start of the string.
  • TEL matches the characters TEL literally (case sensitive).
  • .* matches any character (except newline). Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy].
  • \+ matches the character + literally.
  • 48 matches the characters 48 literally.
  • .* matches any character (except newline). Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy].
  • $ assert position at end of the string.

So, the sed command, sed -i.bak -E '/^TEL.*\+48.*$/d' file backups the file (-i.bak) and deletes any line starting with TEL that contains +48. Which based on how lines are stored in the vCard will just remove the lines needed to accomplish the task as lines starting with TEL that have +48 in them can only be the lines you want removed.

NOTE: The also assumes you did not previously have any numbers with the +48 prefix before you updated your Contacts with the original vCard file that contained the numbers with the +48 prefix.

  • I think we're almost there. In fact, I don't want just to remove the +48 prefix but the entire number. I just want to purge the +48 numbers from my contacts. – LuCouto Aug 5 '16 at 7:59
  • @LuCouto, Finally had some time to look back and have updated my answer with a method that while not AppleScript nonetheless it will work. – user3439894 Aug 7 '16 at 17:53
  • Those instructions are very clear and they've worked great! Thanks a lot! – LuCouto Aug 8 '16 at 7:47

You must log in to answer this question.

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