I have a script that ran perfectly with the Address Book application. Moving it to run with the Contacts application now throws an error about how it must be a ‘person’ to add to a cohort. The contact info is sucked in from a spreadsheet satisfactorily. Here is the complete code:
global Cohort
global Roster
global NA
set WasFound to false
set Roster to {}
set NA to "#N/A"
launch application "Contacts"
tell application "Microsoft Excel" to activate
tell application "Microsoft Excel" to set Cohort to string value of cell "Edit!A37"
tell application "Contacts" to activate
tell application "Contacts"
set myGroups to (get name of every group)
if myGroups contains Cohort then set WasFound to true
if not WasFound then set theGroup to make new group with properties {name:Cohort}
end tell
tell application "Microsoft Excel" to activate
tell application "Microsoft Excel"
repeat with i from 2 to 33
if string value of cell ("Edit!B" & i) is NA then
set j to i - 1
exit repeat
end if
end repeat
end tell
repeat with i from 2 to j
set TEMP to {key:i - 1, LastName:"", FirstName:"", MI:"", Rank:"", |Hash|:"", |Nickname|:"", Srvc:"", Corps:"", Addressline1:"", |City|:"", |State|:"", |Zip|:"", HomePhone:"", |Email|:"", ParentMilitaryCommand:"", MobilePhone:"", WorkPhone:"", EmailDomain:""}
tell application "Microsoft Excel"
set LastName of TEMP to string value of cell ("Edit!B" & i)
set FirstName of TEMP to string value of cell ("Edit!C" & i)
set MI of TEMP to string value of cell ("Edit!D" & i)
set Rank of TEMP to string value of cell ("Edit!F" & i)
set |Hash| of TEMP to string value of cell ("Edit!G" & i)
set |Nickname| of TEMP to string value of cell ("Edit!H" & i)
set Srvc of TEMP to string value of cell ("Edit!J" & i)
set Corps of TEMP to string value of cell ("Edit!M" & i)
if Corps of TEMP is NA then set Corps of TEMP to ""
set Addressline1 of TEMP to string value of cell ("Edit!N" & i)
try
if (not cell ("Edit!O" & i) is missing value) and (string value of cell ("Edit!O" & i) ≠ "") then
set Addressline1 of TEMP to Addressline1 of TEMP & ", " & string value of cell ("Edit!O" & i)
--AddressLine2
end if
end try
set |City| of TEMP to string value of cell ("Edit!P" & i)
set |State| of TEMP to string value of cell ("Edit!Q" & i)
set |Zip| of TEMP to string value of cell ("Edit!R" & i)
set HomePhone of TEMP to string value of cell ("Edit!S" & i)
set |Email| of TEMP to string value of cell ("Edit!T" & i)
set ParentMilitaryCommand of TEMP to string value of cell ("Edit!U" & i)
set MobilePhone of TEMP to string value of cell ("Edit!V" & i)
set WorkPhone of TEMP to string value of cell ("Edit!W" & i)
set EmailDomain of TEMP to my rightstr(|Email| of TEMP, 4)
end tell
tell application "Contacts"
set theStudent to make new person
set last name of theStudent to LastName of TEMP
set first name of theStudent to FirstName of TEMP
set middle name of theStudent to MI of TEMP
set title of theStudent to Rank of TEMP
set note of theStudent to "Cohort: " & Cohort & linefeed & "Hash: " & |Hash| of TEMP
set nickname of theStudent to |Nickname| of TEMP
set suffix of theStudent to Srvc of TEMP
set job title of theStudent to Corps of TEMP
set organization of theStudent to ParentMilitaryCommand of TEMP
if EmailDomain of TEMP is ".mil" then
make new email at end of every email of theStudent with properties ¬
{label:"Work", value:|Email| of TEMP}
else if EmailDomain of TEMP is ".gov" then
make new email at end of every email of theStudent with properties ¬
{label:"Work", value:|Email| of TEMP}
else
make new email at end of every email of theStudent with properties ¬
{label:"Home", value:|Email| of TEMP}
end if
make new phone at end of every phone of theStudent with properties ¬
{label:"Work", value:WorkPhone of TEMP}
make new phone at end of every phone of theStudent with properties ¬
{label:"Home", value:HomePhone of TEMP}
make new phone at end of every phone of theStudent with properties ¬
{label:"Mobile", value:MobilePhone of TEMP}
make new address at end of every address of theStudent with properties ¬
{label:"Home", street:Addressline1 of TEMP, city:|City| of TEMP, state:|State| of TEMP, zip:|Zip| of TEMP}
set cls to class of theStudent
add theStudent to group Cohort
save
end tell
set the end of Roster to theStudent
end repeat
beep
on rightstr(txt, N)
set C to length of txt
if N > C then set N to C
set rtntxt to text (C - N + 1) thru C of txt
return rtntxt
end rightstr
You’ll note that I verify the class of thestudent just before the add step. It is a person, yet the error is, “Contacts got an error: You can only add a person to a group.” Yeah, OK, but?
I’m clueless about the Contacts error. All help appreciated. Thanks