Sorting Address Book selection for printing

It should have been a simple script.

Select the names beginning with each letter and then print them. Successive prints would paginate the letters.
But Address Book resorts the selection based on person id. I tried to force X-ABUID by exporting vcards and re-writing their ids before re-importing. But Address Book re-creates the id anew on every import. So I needed to sort the selection myself.

The following script hangs in 2 places.

(1) I fudged the sort order by filling the (unused) nicknames with the sort order I wanted. But while I can sort on “name of person id” I cannot sort on “nickname of person id.”

(2) I send the Bubblesort my own list. When that list is returned I cannot set the selection to it. It looks good in the event log. But I am missing something critical.

set a2zList to {“a”, “b”, “c”}
–use unused nickname to handle sort order
–this is crude but effective when nickname is not otherwise used
(*
tell application “Address Book”
repeat with x from 1 to (count every person)
if company of person x is true then
set nickname of person x to name of person x
else
set nickname of person x to last name of person x
end if
end repeat
end tell
*)

repeat with x in a2zList
tell application “Address Book” to set selection to (every person whose nickname begins with x)
tell application “Address Book” to set aList to selection
log "Count = " & ((count of aList) as string)
if (count of aList) > 0 then — skip over any missing letters
my BubbleSort(aList)
log “End bubblesort”
tell application “Address Book” to set selection to every item in aList --ERROR AppleEvent handler failed
log "Count = " & ((count of selection) as string)
– Address Book MUST be top window for keystrokes
if frontmost of application “Address Book” is false then
tell application “Address Book” to activate
end if
tell application “System Events” to tell process “Address Book”
keystroke “p” using command down
repeat until exists window “Print”
delay 0.5
end repeat
tell window “Print”
tell table 1 of scroll area 1 of group 1
click checkbox “Address” of row 3
click checkbox “Photo” of row 5
click checkbox “Job Title” of row 6
click checkbox “Note” of row 15
end tell
click button “Print”
delay 1
end tell
end tell

end if
delay 5

end repeat

tell application “Address Book” to quit – close Address Book

http://www.macosxhints.com/article.php?story=20040513173003941

on BubbleSort(theList)
–if class of theList is list then
set theSize to count of theList
repeat with i from 1 to theSize
repeat with j from 2 to (theSize - i + 1)
if ((name of item (j - 1) of theList) > (name of item j of theList)) then --ERROR should be nickname (not name)
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

Model: MacBook Pro
AppleScript: 2.0
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

The second problem can be easily resolved.

The bubblesort routine just needs ‘tell application “Address Book”…end tell’ around the reference to nickname.

I think the first problem is intractable.

Consider for a moment that there is some solution. But then Address Book would re-sort the selection (based on person ids) That re-sorting was why the bubblesort was required in the first place.

The only solution I can think of is for Apple to include a “rebuild” menu option, which would re-establish all the person ids in the alphabetical order selected in the preferences. Then any selection of person ids would always re-sort into alphabetical order.

This script was just the 2nd script I have every written.