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