1

Is there a more-efficient way to determine if a contact doesn't exist than this:

set theAddress to "foo@bar.com"

set found to false

repeat with aContact in contacts

  if email addresses of aContact contains theAddress then
    set found to true
    exit repeat
  end if

end repeat

if not found then
  ...
end if

This will create a new contact if it isn't found (and return true):

set found to open contact email address "foo@bar.com"

** edit **

Search Outlook contacts by category suggests that I should be able to do this using a Spotlight query:

-- the address to be found
set theEmailAddress to "foobar@acme.com"

-- search identity folder
set currentIdentityFolder to quoted form of POSIX path of (current identity folder as string)

--perform Spotlight query
set theContacts to words of (do shell script "mdfind -onlyin " & currentIdentityFolder & "  'kMDItemContentType == com.microsoft.outlook14.contact && [metadata element that represents an email address] == " & theEmailAddress & "' | xargs -I % mdls -name com_microsoft_outlook_recordID '%' | cut -d'=' -f2 | sort -u | paste -s -")

-- process results
...

What is the metadata element that represents a contact's email address?

Community
  • 1
  • 1
craig
  • 25,664
  • 27
  • 119
  • 205

1 Answers1

0

The metadata element you're looking for is kMDItemEmailAddresses -- you can find that and more at Apple's developer library though I'll admit it took a lot of searching to find it.

Shell command that worked for me (wrapping it in AppleScript is an exercise for the reader):

mdfind 'kMDItemContentType == com.microsoft.outlook14.contact && kMDItemEmailAddresses == "user@example.com"'