Printing contacts from Address Book

I am trying to print a list of contacts from Address Book. What I am doing is generating a list of matching people, like so:

theList {“Abraham”, “Allan”, “Bathurst”}

repeat with i in theList
set thePeople to thePeople & (every person whose last name = (i as string))
end repeat

Now I would like to print the people in the list thePeople. I know I can use System Events to drive Address Book to print, but how do i get it to print and iterate through the list of thePeople? Probably something obvious, but I am blind to it right now.


I struggled a bit with System Events: despite using Element Inspector, I couldn’t figure out a way to ‘set focused’ of any element in Group 2 (the name group). This is an awkward way around it.

-- I apologise - this is a very messy way to ensure that the focus is on the 'Name' column of Address Book
-- There must be a better way, but I couldn't figure it out
tell application "Address Book" to activate
tell application "System Events" to tell process "Address Book" to tell window 1
	tell list 1 to click button 2
	tell list 1 to click button 1
	delay 0.3
	repeat 10 times
		keystroke tab
		set f to focused of text field 1
		if f = true then exit repeat
		delay 0.1
	end repeat
	repeat 3 times
		keystroke tab
	end repeat
	keystroke "p" using command
end tell

-- Create or empty folder for printing
tell application "Address Book"
	set AllGroups to name of every group
	if AllGroups does not contain "Print List" then make new group with properties {name:"Print List"}
	set OldList to every person in group "Print List"
	if OldList is not equal to {} then
		set ToDelete to button returned of (display dialog "The print folder is not empty: do you wish to remove all people from this list?" buttons {"Delete", "Cancel"})
		if ToDelete = "Delete" then
			repeat with i in OldList
				remove i from group "Print List"
			end repeat
		end if
	end if
	-- Add all people in NameList to print folder
	set NameList to {"Abraham", "Allan", "Bell"}
	set PersonList to {}
	repeat with i in NameList
		set PersonList to PersonList & (every person whose last name = i)
	end repeat
	repeat with i in PersonList
		add i to group "Print List"
	end repeat
	set selection to every person in group "Print List"
end tell

-- Start up the print dialog
-- You could add further System Events commands to set the print exactly as you want
tell application "System Events" to tell process "Address Book" to tell window 1
	keystroke "p" using command down
end tell