2

Is there a way to search for Contacts in Outlook 2011 for the Mac by Categories?

tell application "Microsoft Outlook"

  -- get the category by name
  set theCategory to item 1 of (every category whose name = "Recruiter")

  -- correctly displays 'Recruiter [25]'
  display dialog (name of theCategory) & " [" & (id of theCategory) & "]"

  -- perform the search (incorrectly, it seems)
  set theContacts to (every contact whose (every category contains theCategory))

  -- should display ~100; actually displays 0
  display dialog (count of theContacts)

end tell
craig
  • 25,664
  • 27
  • 119
  • 205

1 Answers1

1

I think there may be some bugs/features in the OL dictionary implementation with regard to categories - I think your search statement should work, but I agree it doesn't.

One workaround to this is to do a spotlight search instead. This may even be preferable, because it is probably faster than using the OL dictionary. In short, replace your set theContacts to ... line with the following:

    set currentIdentityFolder to quoted form of POSIX path of (current identity folder as string)
    set theContactIDs to words of (do shell script "mdfind -onlyin " & currentIdentityFolder & "  'kMDItemContentType == com.microsoft.outlook14.contact && com_microsoft_outlook_categories == " & id of theCategory & "' | xargs -I % mdls -name com_microsoft_outlook_recordID '%' | cut -d'=' -f2 | sort -u | paste -s -")

    set theContacts to {}
    repeat with thisContactID in theContactIDs
        set end of theContacts to contact id thisContactID
    end repeat

    -- For example display the first name of the first contact
    display dialog first name of (item 1 of theContacts) as string

This will do a spotlight search (mdfind command) for the contacts you require:

  • It will only look in your current identity folder
  • It will only look for contacts
  • It will only return contacts which are marked with the id of the "Recruiter" category

The output of the mdfind command is a list of files which match this query. So this output is piped to mdls, which will list all spotlight-searchable fields, including category. A simple list of contact IDs should be returned to applescript.

The list of contact IDs can then be converted to a list of contacts with the simple repeat loop.

Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
  • +1 - To get your script to work, I had to use `set theCategory to item 1 of (every category whose name = "Recruiter")` to get the category object; is there a shortcut? A related question, the query returns a `List` of `ctxt` objects. Is there a way to cast them to a `contact`? – craig Sep 18 '13 at 14:25
  • 1
    To answer the first question, I think this is unavoidable as whose clauses will always return a list. In this case we are guaranteed there is only one category whose name = "Recruiter", so we just have to look at item 1 of that list. Also it is possible for this list to be empty (if there is no category whose name = "Recruiter"), so robust code should also code defensively around that possibility. – Digital Trauma Sep 18 '13 at 15:40
  • To answer the second question, I have edited my answer. The list of contact IDs can simply be iterated over to get a list of contacts. – Digital Trauma Sep 18 '13 at 17:07