So I’m extremely new to Mac/AppleScript, but have been a Windows developer for a long time. I have a script to help out printing envelopes. The general idea of script is to:
a) Loop all contacts of a group looking for related name with label ‘mailing label’
b) Backup first, middle, last names and title to a new related name called “backup.XXX” (where xxx is field name)
c) Set contact first name to value of ‘mailing label’ related name, clear out middle, last and title.
d) Let the Print occur
e) Loop all contacts and restore “backup.xxx” to appropriate contact field.
The problem is during the initial loop, when I’m created related names of ‘backup.xxx’ after creating a related name, for some reason my ‘current contact’ moves to the next one in the list.
Below is some of the more important code. I’m not sure if my refactoring the script into separate functions (thus requiring a new tell application statement) has caused this problem or not. I can refactor script back out into one long script if you experts think that could be a problem.
At the very bottom I’ll post out the logging I’ve done using the ‘log’ method. In my group, I have two contacts, ‘Tom Aney’ and ‘Tom Spears’.
set mylist to group groupName
repeat with contact in people in mylist
if ((first related name whose label is mailingLabelName) of contact) exists then
set mailingLabelInfo to (first related name whose label is mailingLabelName) of contact
my backupContactInfo(mailingLabelInfo, contact)
end if
end repeat
Then the backupContactInfo function is:
on backupContactInfo(mailingLabelInfo, contact)
tell application "Address Book"
set mailingLabel to (value of mailingLabelInfo)
set t to (title of contact)
set f to (first name of contact)
set l to (last name of contact)
set m to (middle name of contact)
set logPerson to (id of contact) & ": " & f & " " & l
log logPerson & ": Processing back..."
my backupContactName(contact, "backup.title", t)
my backupContactName(contact, "backup.first", f)
my backupContactName(contact, "backup.middle", m)
my backupContactName(contact, "backup.last", l)
end tell
end backupContactInfo
And the final helper function is backupContactName:
on backupContactName(contact, backupLabel, backupValue)
tell application "Address Book"
set personID to (id of contact)
set logPerson to personID & ": " & (first name of contact) & " " & (last name of contact)
set backupID to personID & ":RN:" & backupLabel
if backupValue is not missing value then
if ((first related name whose label is backupLabel) of contact) exists then
set backupName to (first related name whose label is backupLabel) of contact
log logPerson & ": Updating value of '" & backupLabel & "' to '" & backupValue & "'"
set value of backupName to backupValue
else
log logPerson & ": Creating value of '" & backupLabel & "' to '" & backupValue & "'"
set backupName to make new related name with properties {label:backupLabel, value:backupValue, id:backupID} at end of (related names of contact)
end if
return backupLabel & ": " & backupValue & return
else
if ((first related name whose label is backupLabel) of contact) exists then
set backupName to (first related name whose label is backupLabel) of contact
log logPerson & ": Deleting value of '" & backupLabel & "'"
delete backupName
end if
return ""
end if
end tell
end backupContactName
Here is the log output:
(*6AE796FD-6013-41BD-9698-2226CB438912:ABPerson: Tom Aney: Processing back...*)
(*6AE796FD-6013-41BD-9698-2226CB438912:ABPerson: Tom Aney: Creating value of 'backup.first' to 'Tom'*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Creating value of 'backup.last' to 'Aney'*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Set first name to The Aney Family*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Delete middle name*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Delete last name*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Delete title*)
(*Processing restore for '6AE796FD-6013-41BD-9698-2226CB438912:ABPerson: Tom Aney'...*)
(*6AE796FD-6013-41BD-9698-2226CB438912:ABPerson: Tom Aney: Set first name to 'Tom.*)
(*6AE796FD-6013-41BD-9698-2226CB438912:ABPerson: Tom Aney: Delete 'backup.first.*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Set last name to 'Aney.*)
(*B010B1E8-F982-4EAB-90BE-D3ED7396BAA8:ABPerson: Tom Spears: Delete 'backup.last.*)