A script targets "Contacts", runs without errors, but no result

Hello,

My script takes a contact, highlighted (“selected”) in “Contacts”, aka “Address Book”, and attempts to duplicate it to the similarly named group in the rest of my accounts. I’m able to retrieve every necessary group, the needed elements and their properties, however upon the completion the contact isn’t duplicated, and the duplicates aren’t visible in Contacts. By “duplicating” I don’t mean it in a plain sense: I use make as evident by reviewing my script. I’d think of using add as a more streamlined approach after creating an object but every time using add the error “A person can be created only in a group” is raised.
At this point I’m not even sure where I should look into: is it the grouping of the statements within the object model, putting together the commands and their options or what? I know that adding to a group requires looping but it’s more or less simple only if many contacts added to 1 group but not vice versa which is what I aim doing. Also, when making a contact should I direct the command at the top hierarchical object or lower ones will do too? Do I need to use the at option?



(*
Necessary prerequisites:
-- The application "Contacts" running and a target contact selected

Creation information:
Created, compiled and run using "Script Editor" in macOS Mojave (rev. 10.14.6) 

Checkpoints set to verify the statements loop through all of the filtered groups*)

set TheContact to get the first item of the (get selection)
log "TheContact↑" & (log TheContact)

set CCard_prime to {nick:TheContact's nickname, org:TheContact's organization, suf:TheContact's suffix, website:TheContact's home page, bdate:TheContact's birth date, ttl:TheContact's title, img:TheContact's image, tname:TheContact's name, tnote:TheContact's note, corp:TheContact's company, jbttl:TheContact's job title, lname:TheContact's last name, fname:TheContact's first name} -- for making a new contact card using the most practical writable properties of the selected contact.

set CCard_duo to {phone_label:TheContact's phone's label, phone_num:TheContact's phone's value, email_label:TheContact's email's label, email_value:TheContact's email's value, social_service_name:TheContact's social profile's service name, social_user_name:TheContact's social profile's user name, social_user_id:TheContact's social profile's user identifier, social_profile_url:TheContact's social profile's url, IM_service_type:TheContact's instant message's service type, IM_service_name:TheContact's instant message's service name, IM_user_name:TheContact's instant message's user name} -- for making a new contact card using the values of the selected contact's subclasses.

log "CCard_duo↑" & (log CCard_duo)

set CGroup to missing value
set TheGroups to (get every group)
set AllAccountsFriendsGroups to {}
repeat with i from 1 to number of items in TheGroups -- target groups to add the contact - which doesn't belong to any other group than the default smart group "Last imported"- to.
	set this_item to (item i's contents of TheGroups) -- the group	
	tell this_item
		if {TheContact} is in (get people) then set CGroup to this_item
	end tell
	if this_item's name is "friends" then set end of AllAccountsFriendsGroups to this_item
end repeat


log "AllAccountsFriendsGroups↑" & (log AllAccountsFriendsGroups)

set CheckPoint to 0
repeat with i from 1 to number of items in (get every item of AllAccountsFriendsGroups)
	try
		set this_item to item i's contents of AllAccountsFriendsGroups
		tell this_item
			if {TheContact} is not in (get people) then
				set CheckPoint to CheckPoint + 1
				set ContactDouble to make new person with properties {first name:CCard_prime's fname, last name:CCard_prime's lname} at the end of this_item's people
				tell ContactDouble
					set {nickname, organization, suffix, home page, birth date, title, image, note, company, job title, last name, first name} to {CCard_prime's nick, CCard_prime's org, CCard_prime's suf, CCard_prime's website, CCard_prime's bdate, CCard_prime's ttl, CCard_prime's img, CCard_prime's tnote, CCard_prime's corp, CCard_prime's jbttl, CCard_prime's lname, CCard_prime's fname}
					make new phone with properties {label:CCard_duo's phone_label, value:CCard_duo's phone_num} at the end of this_item's phones
					make new email with properties {label:CCard_duo's email_label, value:CCard_duo's email_value} at the end of this_item's emails
					make new social profile with properties {service name:CCard_duo's social_service_name, user name:CCard_duo's social_user_name, user identifier:CCard_duo's social_user_id, url:CCard_duo's social_profile_url} at the end of this_item's social profiles
					make new instant message with properties {service type:CCard_duo's IM_service_type, service name:CCard_duo's IM_user_name, user name:CCard_duo's IM_user_name} at the end of this_item's instant messages
					
				end tell
				-- add ContactDouble to this_item
				save
				set selected to true
			end if
		end tell
	end try
	-- set CheckPoint to i
end repeat
return CheckPoint

Model: MacBook Pro 9,1 (mid-2012 15") Core i7 2.3 GHz, 16 GB RAM, 1 TB SSD
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

Hi. The culprit is likely the try block with the save command in it; as soon as one line fails, so do all subsequent. I’m not really sure why you aren’t duplicating the items, which is simpler than making.


tell application "Contacts"
	if not (exists group "friendz") then
		set friends to make group with properties {name:"friendz"}
		repeat with aPerson in (get selection) --or (get people), for entire contact list
			tell aPerson to if groups's name is {} then duplicate it to friends
		end repeat
		save
	end if
end tell

I would add some log instructions which would show what works and what doesn’t.



(*
Necessary prerequisites:
-- The application "Contacts" running and a target contact selected

Creation information:
Created, compiled and run using "Script Editor" in macOS Mojave (rev. 10.14.6) 

Checkpoints set to verify the statements loop through all of the filtered groups
*)
tell application "Contacts" -- MISSED
	set TheContact to get the first item of the (get selection)
	log "TheContact" & (log TheContact)
	
	set CCard_prime to {nick:TheContact's nickname, org:TheContact's organization, suf:TheContact's suffix, website:TheContact's home page, bdate:TheContact's birth date, ttl:TheContact's title, img:TheContact's image, tname:TheContact's name, tnote:TheContact's note, corp:TheContact's company, jbttl:TheContact's job title, lname:TheContact's last name, fname:TheContact's first name} -- for making a new contact card using the most practical writable properties of the selected contact.
	
	set CCard_duo to {phone_label:TheContact's phone's label, phone_num:TheContact's phone's value, email_label:TheContact's email's label, email_value:TheContact's email's value, social_service_name:TheContact's social profile's service name, social_user_name:TheContact's social profile's user name, social_user_id:TheContact's social profile's user identifier, social_profile_url:TheContact's social profile's url, IM_service_type:TheContact's instant message's service type, IM_service_name:TheContact's instant message's service name, IM_user_name:TheContact's instant message's user name} -- for making a new contact card using the values of the selected contact's subclasses.
	
	log "CCard_duo↑" & (log CCard_duo)
	
	set CGroup to missing value
	set TheGroups to (get every group)
	set AllAccountsFriendsGroups to {}
	repeat with i from 1 to number of items in TheGroups -- target groups to add the contact - which doesn't belong to any other group than the default smart group "Last imported"- to.
		set this_item to (item i's contents of TheGroups) -- the group	
		tell this_item
			if {TheContact} is in (get people) then set CGroup to this_item
		end tell
		if this_item's name is "friends" then set end of AllAccountsFriendsGroups to this_item
	end repeat
	
	
	log "AllAccountsFriendsGroups↑" & (log AllAccountsFriendsGroups)
	
	set CheckPoint to 0
	repeat with i from 1 to number of items in (get every item of AllAccountsFriendsGroups)
		try
			log (i & ", point 01")
			set this_item to item i's contents of AllAccountsFriendsGroups
			log result
			log (i & ", point 02")
			tell this_item
				if {TheContact} is not in (get people) then
					log (i & ", point 03")
					set CheckPoint to CheckPoint + 1
					log result
					log (i & ", point 04")
					set ContactDouble to make new person with properties {first name:CCard_prime's fname, last name:CCard_prime's lname} at the end of this_item's people
					log result
					log (i & ", point 05")
					tell ContactDouble
						set {nickname, organization, suffix, home page, birth date, title, image, note, company, job title, last name, first name} to {CCard_prime's nick, CCard_prime's org, CCard_prime's suf, CCard_prime's website, CCard_prime's bdate, CCard_prime's ttl, CCard_prime's img, CCard_prime's tnote, CCard_prime's corp, CCard_prime's jbttl, CCard_prime's lname, CCard_prime's fname}
						log result
						log (i & ", point 06")
						make new phone with properties {label:CCard_duo's phone_label, value:CCard_duo's phone_num} at the end of this_item's phones
						log result
						log (i & ", point 07")
						make new email with properties {label:CCard_duo's email_label, value:CCard_duo's email_value} at the end of this_item's emails
						log result
						log (i & ", point 08")
						make new social profile with properties {service name:CCard_duo's social_service_name, user name:CCard_duo's social_user_name, user identifier:CCard_duo's social_user_id, url:CCard_duo's social_profile_url} at the end of this_item's social profiles
						log result
						log (i & ", point 09")
						make new instant message with properties {service type:CCard_duo's IM_service_type, service name:CCard_duo's IM_user_name, user name:CCard_duo's IM_user_name} at the end of this_item's instant messages
						log result
					end tell
					-- add ContactDouble to this_item
					log (i & ", point 10")
					save
					log (i & ", point 11")
					set selected to true
					log (i & ", point 12")
				end if
			end tell
		end try
		-- set CheckPoint to i
	end repeat
	return CheckPoint
end tell -- MISSED

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 24 juillet 2020 16:18:17

I want to go with the make approach as a way to explore the potential of the Contact’s dictionary, for future reuse.

I re-worked the original script because I noticed that the values of properties of the subclasses in question are lists and this is a game-changer. I also removed the error checks. However, despite all of that the script still failed with the error “Contacts got an error: AppleEvent handler failed.” Why in the world is it so hard to do a seemingly simple task of making a new contact? This kind of operations is very common and other apps have no trouble getting it done.


tell application "Contacts"
	set TheContact to get the first item of the (get selection)
	log "TheContact↑" & (log TheContact)
	set CCard_prime to {nick:TheContact's nickname, org:TheContact's organization, suf:TheContact's suffix, website:TheContact's home page, bdate:TheContact's birth date, ttl:TheContact's title, img:TheContact's image, tname:TheContact's name, tnote:TheContact's note, corp:TheContact's company, jbttl:TheContact's job title, lname:TheContact's last name, fname:TheContact's first name} -- for making a new contact card using the most practical writable properties.
	
set CCard_duo to {phone_label:TheContact's phone's label, phone_num:TheContact's phone's value, email_label:TheContact's email's label, email_value:TheContact's email's value, social_user_name:TheContact's social profile's user name, social_user_id:TheContact's social profile's user identifier, social_profile_url:TheContact's social profile's url, IM_service_type:TheContact's instant message's service type, IM_service_name:TheContact's instant message's service name, IM_user_name:TheContact's instant message's user name} -- for making a new contact card by creating the main subclasses of the person element.
	
	log "CCard_duo↑" & (log CCard_duo)
	
	set CGroup to missing value
	set TheGroups to (get every group)
	set AllAccountsFriendsGroups to {}
	repeat with i from 1 to number of items in TheGroups -- target groups to add the contact - which doesn't belong to any other group than the default smart group "Last imported"- to.
		set this_item to (item i's contents of TheGroups) -- the group	
		tell this_item
			if {TheContact} is in (get every person) then set CGroup to this_item
		end tell
		if this_item's name is "friends" then set end of AllAccountsFriendsGroups to this_item
	end repeat
	
	
	log "AllAccountsFriendsGroups↑" & (log AllAccountsFriendsGroups)
	
	repeat with i from 1 to number of items in (get every item of AllAccountsFriendsGroups)
		set this_item to item i's contents of AllAccountsFriendsGroups
		tell this_item
			if {TheContact} is not in (get every person) then
				set ContactDouble to make new person with properties {first name:CCard_prime's fname, last name:CCard_prime's lname} at the end of this_item's every person
				tell ContactDouble
					set {nickname, organization, suffix, home page, birth date, title, image, note, company, job title, last name, first name} to {CCard_prime's nick, CCard_prime's org, CCard_prime's suf, CCard_prime's website, CCard_prime's bdate, CCard_prime's ttl, CCard_prime's img, CCard_prime's tnote, CCard_prime's corp, CCard_prime's jbttl, CCard_prime's lname, CCard_prime's fname}
					tell (make new phone at end of every phone)
						repeat with aLabel in (get items of CCard_duo's phone_label)
							if aLabel's contents is not in {"", missing value} then set end of label to aLabel's contents
						end repeat
						
						repeat with aValue in (get items of CCard_duo's phone_num)
							if aValue's contents is not in {"", missing value} then set end of value to aValue's contents
						end repeat
					end tell
					
					tell (make new email at end of every email)
						repeat with aLabel in (get items of CCard_duo's email_label)
							if aLabel's contents is not in {"", missing value} then set end of label to aLabel's contents
						end repeat
						repeat with aValue in (get items of CCard_duo's email_value)
							if aValue's contents is not in {"", missing value} then set end of value to aValue's contents
						end repeat
					end tell
					
					tell (make new social profile at end of every social profile)
						repeat with aUserName in (get every item of CCard_duo's social_user_name)
							if aUserName's contents is not in {"", missing value} then set end of user name to aUserName's contents
						end repeat
						repeat with aUID in (get every item of CCard_duo's social_user_id)
							if aUID's contents is not in {"", missing value} then set end of user identifier to aUID's contents
						end repeat
						repeat with aURL in (get every item of CCard_duo's social_profile_url)
							if aURL's contents is not in {"", missing value} then set end of url to aURL's contents
						end repeat
					end tell
					
					tell (make new instant message at end of every instant message)
						repeat with sType in (get every item of CCard_duo's IM_service_type)
							if sType's contents is not in {"", missing value} then set end of service type to sType's contents
						end repeat
						repeat with sName in (get every item of CCard_duo's IM_service_name)
							if sName's contents is not in {"", missing value} then set end of service name to sName's contents
						end repeat
						
						repeat with uName in (get every item of CCard_duo's IM_user_name)
							if uName's contents is not in {"", missing value} then set end of user name to uName's contents
						end repeat
						
					end tell
					save
					set selected to true
					
				end tell
				-- add ContactDouble to this_item
			end if
		end tell
	end repeat
end tell

Model: MacBook Pro 9,1 (mid-2012 15") Core i7 2.3 GHz, 16 GB RAM, 1 TB SSD
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14