Delete Unwanted Field in Contacts App

I have a problem with my Contacts on Mac running Sonoma (14.3.1 ). I have used Outlook and it created a custom field named Outlook on some of my Contacts. I want to delete this contact field and all of its content on all the contacts that have it. In some instances Outlook even duplicated the Outlook field. I have over 3000 Contacts with this issue and fixing it one by one doe s not seem like a solution. The fleld (named Outlook) has a huge link text on it that starts with ms-outlook://people/.

Has anyone come across this or can point me in the direction of a script that can help me?

Hi @sankaman

With any luck, if your spurious “Outlook” fields contain links, they’ll be represented by url objects in the Contacts app. A script will need to find any such objects whose label value is “Outlook” and delete them from the contacts containing them.

There are various ways to handle this. The script below should be fairly fast if you have over 3,000 contacts. If any “Outlook” fields remain after it’s run, we’ll need to identify what class of contact info’s actually involved.

tell application "Contacts"
	-- These will be 3,000+ item lists.
	script o
		property contacts : {}
		property urlLabels : {}
	end script
	
	set {o's contacts, o's urlLabels} to {it, label of urls} of people
	repeat with i from 1 to (count o's contacts)
		if (item i of o's urlLabels contains {"Outlook"}) then
			set thisContact to item i of o's contacts
			delete (thisContact's urls whose label is "Outlook")
		end if
	end repeat
	save -- Save and make visible the changes.
end tell

Thanks, I have tried running the script but I get the following error:

error “The variable contacts is not defined.” number -2753 from “contacts”

Apologies for that! ‘thisContact’ should be set to item i of o’s contacts. I’ve corrected it above.

Thanks for your quick response Nigel, I really appreciate it as I am new to scripts.

I still get error “The variable contacts is not defined.” number -2753 from “contacts” on the Results when running it

Duh! I’m getting old. Two further corrections made.

Thanks Nigel , that was very helpful, I had been looking for a way to easily do it and this did the trick!!

Hi, thanks for help sofar. I ran the scripts but got this:

“missing value”

Can you help?

Hi @adilif. Welcome to MacScripter.

missing value is the normal result of the save command and is perfectly OK. Are you getting it from anywhere else in the script? The corrected, working script is the one in post #2 above.

Thanks @Nigel_Garvey for the prompt answer

I get missing value only there.

But if I use the other script I get something else:

" error “The variable contacts is not defined.” number -2753 from “contacts”"

Yes. Post #3 quotes the original version of the script in post #2, in which I made a few errors. :expressionless: The post #2 script has since been corrected. Post #3 immortalises the original goofs.

Ok thanks using the correct one now.
It’s fair to say that I realise the URL are under label “other” and “home page” in my case (I have some 5k contacts, that needs to be adjusted, so getting it right is important).
How does the script change? Excuse my ignorance

Hi @adilif. Sorry for the slow reply. I’m preparing my house for some work next week.

If I’ve understood correctly, you want a version of the script which does exactly the same as above, but using Contacts’s predefined URL labels “other” and “home page” instead of sankaman’s custom label “Outlook”.

Contacts’s predefined labels are a bit strange in that their text in AppleScript is wrapped in special codes. So in a script, “other” and “home page” are represented by “_$!<Other>!$_” and “_$!<HomePage>!$_” respectively.

tell application "Contacts"
	-- These will be 3,000+ item lists.
	script o
		property contacts : {}
		property urlLabels : {}
	end script
	
	set {o's contacts, o's urlLabels} to {it, label of urls} of people
	repeat with i from 1 to (count o's contacts)
		set thisContact to item i of o's contacts
		set theseLabels to item i of o's urlLabels
		if (theseLabels contains {"_$!<Other>!$_"}) then delete (thisContact's urls whose label is "_$!<Other>!$_")
		if (theseLabels contains {"_$!<HomePage>!$_"}) then delete (thisContact's urls whose label is "_$!<HomePage>!$_")
	end repeat
	save -- Save and make visible the changes.
end tell
1 Like

WOW worked like a charm and saved me hours.
Thhanks @Nigel_Garvey - well done!!!