ephramz
09-13-2004, 11:54 PM
The "where" or "whose" command is great for doing quick and easy searches in Applescript, for instance, the following script to search for the email of someone with a certain name:
tell application "Address Book"
set thisContact to (get every person whose name is "Joe Schmo")
if length of thisContact > 0 then
return value of first item of email of thisContact
else
return "Not found."
end if
end tell
is about ten times faster (on my 500 MHz Pismo) than doing the search manually, as below. But is there anyway to do 'where' filter searches on more complicated nested data structures, for instance to look for the an Address Book entry who has a certain email? Since the emails are a list for each contact, and it's value needs to be evaluated to see what's in it, it seems convoluted, but a complicated lines like this actually works:
set thisContact to the first item of (get every person where ¬
the value of the email of it contains "joe@schmoe.com")
and saves lengthy code and time! Let the apps do the searching for you.
set fromPerson to "Joe Schmo"
tell application "Address Book"
set allContacts to people
set resultsOfSearch to {}
set isMatch to false
repeat with thisContact in allContacts
if name of thisContact is fromPerson then
set end of resultsOfSearch to thisContact
set isMatch to true
exit repeat
end if
end repeat
if isMatch then
set fromPerson to the name of thisContact
return value of first item of email of thisContact
end if
end tell
tell application "Address Book"
set thisContact to (get every person whose name is "Joe Schmo")
if length of thisContact > 0 then
return value of first item of email of thisContact
else
return "Not found."
end if
end tell
is about ten times faster (on my 500 MHz Pismo) than doing the search manually, as below. But is there anyway to do 'where' filter searches on more complicated nested data structures, for instance to look for the an Address Book entry who has a certain email? Since the emails are a list for each contact, and it's value needs to be evaluated to see what's in it, it seems convoluted, but a complicated lines like this actually works:
set thisContact to the first item of (get every person where ¬
the value of the email of it contains "joe@schmoe.com")
and saves lengthy code and time! Let the apps do the searching for you.
set fromPerson to "Joe Schmo"
tell application "Address Book"
set allContacts to people
set resultsOfSearch to {}
set isMatch to false
repeat with thisContact in allContacts
if name of thisContact is fromPerson then
set end of resultsOfSearch to thisContact
set isMatch to true
exit repeat
end if
end repeat
if isMatch then
set fromPerson to the name of thisContact
return value of first item of email of thisContact
end if
end tell