Cleanup custom fields from AddessBook App

I have installed an app on my iphone and it added a custom homepage URL field to my contacts. Not the I have deleted the app, but it left the custom field in my contacts. I am using iCloud so the contacts synced to my address book app. I was trying to write a script that would let me delete all the contacts that halve this custom field. However, when I write the script I keep getting an error that says it can retrieve the URL property. When I dump the contact I can see the field. The output is item1.url item1.name. But I can’t seem to get this property by itself. Is the a listing of the contacts schema with all the available properties? Any ideas on how this could be accomplished?

Hi,

try this, replace my great label in the first line with the proper label name.
Caution: the script deletes all contacts which match the condition. There is no undo.


property customLabel : "my great label"

tell application "Contacts"
	set {identifiers, urlList} to {id, url} of people
	repeat with i from 1 to count identifiers
		repeat with anURL in (get item i of urlList)
			if label of anURL is customLabel then
				delete person id (item i of identifiers)
				exit repeat
			end if
		end repeat
	end repeat
	save
end tell

Thanks. This will help a lot. But I don’t want to delete the person, just the custom field. You noted that this will delete the contact. I am just getting clarification. Could I replace delete person with delete anURL and it would delete just the custom field?

In your first post

To delete only the URL, but not the contact use this


property customLabel : "my great label"

tell application "Contacts"
	set urlList to url of people
	repeat with i from 1 to count urlList
		repeat with anURL in (get item i of urlList)
			if label of anURL is customLabel then
				delete anURL
				exit repeat
			end if
		end repeat
	end repeat
	save
end tell


Sorry about that. This worked out great. Is there a reference to the people data structure? Or for other Mac OS Apps?

Hi arejay,

What you need to do is look at the dictionary for an application. For instance, open Script Editor and go to Open Dictionary. Choose Contacts. Looking down the list for people, there’s this:

I think this is what you mean by data structure.

gl,
kel

Hi arejay,

Just looking at the dictionary for a starter might be a little intimidating, but it’s actually not that bad after you get started.

Actually, using Contacts as a starter might be good because believe it or not it’s one of the easier apps to script. If you look at the dictionary entry that I posted, there are three parts to it: elements, properties, and responds to. Think of elements as objects, properties as things that every object of the same class have, and responds to are actions that can reformed on that certain object.

Now, every object may contain objects or they may be contained by other objects. Look at the contains and contained by definitions in the dictionary. Some objects may not contain the same objects as other objects.

Secondly, objects may have properties. properties are things that every object of the same class have. For instance, every person has the same properties. But note that they might not be filled, but have missing values.

Lastly, the actions that may be performed on an object is self explanatory. These are the things you can write and actually do something on the object.

The last big things that you need to think about is references to objects and semantics. Applications are written by different people, so many times they use slightly different syntax in their scripting things. Even within Apple, some people working on a project may not know what is the current syntax or even make up their own stuff. Although lately it has been pretty good to me. But especially in Mail it wasn’t very god in the past. Maybe they have a lot on their minds :).

Anyway, just a quicky tutorial to get started and hope it helps. I might have forgotten some things that I was thinking about, but after you get started it gets easier.

Edited: one thing I might mention is that when you se r/o, that means read only. In other words, it can’t be changed once it is initially set.

gl,
kel

tell application “Contacts”

set thePeople to selection

repeat with aPerson in thePeople
	
	set abID to id of aPerson
	
	set abUrl to {}
	set UrlCnt to count of every url of person id abID
	set homepage to {}
	--display dialog UrlCnt
	
	
	
	if UrlCnt ≥ 1 then
		repeat with i from 1 to UrlCnt
			set thisUrl to {}
			set thisUrl to thisUrl & {label of url i of person id abID}
			
			
			if label of url i of person id abID is "home page" then
				
				set homepage to value of url 1 of person id abID
				display dialog homepage
			end if
			
			set thisUrl to thisUrl & {value of url i of person id abID}
			set abUrl to abUrl & {thisUrl}
			
			
			
		end repeat
	end if

In Catalina macOS, the line label of url i or value of url 1 is giving error. Pls suggest the changes needed to make this work. I would like to open url link of selected contacts in safari.