Faster Code for Entourage Contacts

I want to select using AppleScript a group of contacts in Entourage 2004 that have the category “test” already assigned to each contact.

Each of the 3 AppleScripts listed below works, but only the last AppleScript (which requires some smart mouse clicks by the user beforehand) completes in a reasonable amount of time. My Entourage contact database contains 2,687 records, and 3 of those contacts have the category “test” assigned to them. The first AppleScript takes 43 seconds to complete. The second AppleScript takes 28 seconds to complete. The third AppleScript takes less than 1 second to complete (excluding smart user mouse clicks).

Does anyone have better/faster AppleScript code that selects the contacts in a specific category (but does not require the user manually highlight the contacts beforehand)?

Script 1:


tell application "Microsoft Entourage"
	set theCategory to category "test"
	set theseContacts to {}
	set allContacts to every contact
	repeat with thisContact in allContacts
		set assignedCategories to category of thisContact
		repeat with i from 1 to (count of assignedCategories)
			if item i of assignedCategories is theCategory then
				set end of theseContacts to thisContact
			end if
		end repeat
	end repeat
	--do stuff to theseContacts
end tell
beep

Script 2:


tell application "Microsoft Entourage"
	set theCategory to category "test"
	set theseContacts to contacts where theCategory is in its category
	--do stuff to theseContacts
end tell
beep

Script 3 (user clicks to Entourage Address Book window, filters contacts to “Categoy is” → “test”, highlights/selects all resultant records, then runs AppleScript):


tell application "Microsoft Entourage"
	set theseContacts to selection
	--do stuff to theseContacts
end tell
beep

I don’t know if there’s a good way. Maybe someone else knows how to filter on that property. The categories are clearly indexed though – if you do a find for category is “xxx”, the results window comes up very fast. I’ve never tried UI scripting Entourage, but it might be possible to automate a find.

How much does your data change? Could you write a script to build a group for each category value and use that to get a list of contacts?

Thanks for your reply.

The point you make in your post is spot on: doing a “find” on the category (with user mouse clicks, etc.) produces results in less than a second, so the category field must be indexed (or programitically similar). Note to others: In Entourage, this can be seen by activating Entourage’s “Address Book” pane or window, choosing “Advanced Find” from the “Edit” menu, and setting the search criteria as “Category” → “Is” → “test”. The results of that querry are instataneous.

The challenge is to reproduce that very fast querry in AppleScript.

I might try UI scripting or the approach using groups that you suggest, but I’m hoping someone has a better solution. Surely this must be a typical task: select all contacts/e-mail messages whose category is “____” (fill in the blank), and do it fast (because that’s just the beginning of the script).