Go Back   The macosxhints Forums > Working with OS X > OS X Developer



 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
Old 09-14-2004, 12:54 AM   #1
ephramz
Triple-A Player
 
Join Date: May 2004
Posts: 52
How to use nested Where Commands to filter in Applescript

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:

Code:
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:
Code:
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.

Code:
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

Last edited by ephramz; 09-14-2004 at 12:57 AM.
ephramz is offline   Reply With Quote
 

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:59 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Site design © Mac Publishing LLC; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of Mac Publishing LLC.