Hi all!
Delurking once again to see if any of the gurus can help me optimize my code. The project (in a nutshell) is to put my contacts’ group affiliation(s) into their Notes field in Address book–one per line. Naturally, any existing (non-group-related) notes need to stay intact. The groups also should be listed in the notes field in alphabetical order. The script also needs to take into account changes in a contact’s groups.
I’ve cobbled together suggestions from here and there, and with some spit & baling wire, managed to make it work, but it seems slow (with about 1500 contacts). Right now, I believe the contact in the greatest number of groups is about 25, but that maximum is likely to be exceeded soon.
Here’s the code:
property delim : linefeed -- this is the delimiter used to separate notes
tell application "Address Book"
set thePeople to the selection
repeat with thePerson in thePeople
set theCategories to the name of the groups of thePerson
tell me
BubbleSort(theCategories)
end tell
set noteValue to note of thePerson
if noteValue is in {linefeed, missing value, space} then set noteValue to ""
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delim}
set noteList to text items of noteValue
set noteListLength to length of noteList
set categoryList to text items of theCategories
--because the categoryList comes from AddressBook, it will never have 'cat:' in it for comparison, whereas the noteList SHOULD have 'cat:' in each item once everything is set up. So this is the point to first massage the category list into having 'cat:' prefixes with linebreaks
set AppleScript's text item delimiters to (linefeed & "cat: ")
set categoryList to "cat: " & (categoryList as text)
set AppleScript's text item delimiters to delim
--find out which item in the list is the first 'cat:' item and truncate the list, then build it up with the new categories
if noteListLength is not 0 then
repeat with i from 1 to noteListLength
if text item i of noteList begins with "cat: " then
set i to i - 1
if i = 0 then exit repeat
if text item i of noteList is "" then set i to i - 1
exit repeat
end if
end repeat
if i = 0 then
set noteList to {}
else
set noteList to items 1 thru i of noteList
end if
end if
--now build the new note list, inserting a blank line if there are existing notes
if noteList is not {} then set end of noteList to ""
set categoryList to categoryList as text
set end of noteList to categoryList
set newNoteValue to noteList as text
set note of thePerson to newNoteValue
set AppleScript's text item delimiters to tid
end repeat
save
end tell
on BubbleSort(theList)
if class of theList is list then
set theSize to length of theList
repeat with i from 1 to theSize
repeat with j from 2 to (theSize - i + 1)
if ((item (j - 1) of theList) > (item j of theList)) then
set temp to (item (j - 1) of theList)
set (item (j - 1) of theList) to (item j of theList)
set (item j of theList) to temp
end if
end repeat
end repeat
return theList
else
return false
end if
end BubbleSort
I’m sure the sorting code for the groups is slow, and I wish I could figure out a better way to insert, append, and delete changes to the list of groups once they are already in the Notes field.
All suggestions welcome!
Thanks,
Brad