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.
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.
+481234567890
or what in the vCard?