Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Do reverse number lookup and age calcs in Address Book Apps
Realizing that plug-ins could be easily used to add functionality to Address Book, I set out to create two new AppleScripts, the ideas for which have been dangling in the nether regions of my mind for some time.

The first script is applied to the phone number fields and will automatically perform a reverse phone number look-up using Google. While I realize such look-ups are not always successful, sometimes they are the last resort and can turn up an address when I would otherwise have none.

The second script is applied to the birthday field and simply tells me how old somebody is.

To use the scripts, simply cut and paste the text below into a blank Script Editor document and save as a script in the ~/Library -> Address Book Plug-Ins directory. Then quit Address Book if it is open and relaunch it. Voila!

Reverse Look-Up script:

using terms from application "Address Book"
  on action property
    return "phone"
  end action property
   
  on action title for aPerson with aPhone
    return "Look Up Address"
  end action title
      
  on should enable action for aPerson with aPhone
    return true
  end should enable action
      
  on perform action for aPerson with aPhone
    set thephone to ((value of aPhone) as string)
    set thephone to (characters 1 thru 3 of thephone) & " " &
    (characters 5 thru 7 of thephone) & " " &
    (characters 9 thru 12 of thephone)
    tell application "Safari"
      activate
      open location "http://www.google.com/search?q=" & thephone
    end tell
  end perform action
end using terms from
Show Age Script:

using terms from application "Address Book"
  on action property
    return "birth date"
  end action property
   
  on action title for p with e
    set monthnow to (month of (current date) as number)
    set daynow to (day of (current date) as number)
    set monthcheck to ((month of e) as number)
    set daycheck to ((day of e) as number)
    if monthnow < monthcheck then
      return (first name of p) & " " &
      (((year of (current date)) - (year of e) - 1)
      as string) & " years old"
    else
      if monthnow = monthcheck then
        if daynow < daycheck then
          return (first name of p) & " " &
          (((year of (current date)) - (year of e) - 1)
          as string) & " years old"
        end if
      end if
      return (first name of p) & " " &
      (((year of (current date)) - (year of e))
      as string) & " years old"
    end if
  end action title
      
  on should enable action for p with e
    return true
  end should enable action
      
end using terms from
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[17,221 views]  

Do reverse number lookup and age calcs in Address Book | 12 comments | Create New Account
Click here to return to the 'Do reverse number lookup and age calcs in Address Book' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Do reverse number lookup and age calcs in Address Book
Authored by: CaptCosmic on Jan 20, '04 12:43:44PM

I have run into an issue with the Reverse Phone Lookup script where on some of my phone numbers, it pulls them apart incorrectly. Seems that Address Book is returning the phone number with extra spaces inserted into it.

---
Capt Cosmic



[ Reply to This | # ]
Amended script
Authored by: hughescr on Jan 20, '04 03:18:34PM
This modified script uses infospace.com which has better results generally than Google, will automatically switch searches for "work" vs "home" phone lookups, and also does a better job figuring out phone number format:

using terms from application "Address Book"
	on action property
		return "phone"
	end action property
	
	on action title for aPerson with aPhone
		return "Look Up Address"
	end action title
	
	on should enable action for aPerson with aPhone
		return true
	end should enable action
	
	on perform action for aPerson with aPhone
		set thephone to ((value of aPhone) as string)
		set wpq to 1
		if (label of aPhone) = "work" then
			set wpq to 0
		end if
		set AppleScript's text item delimiters to {"(", ")", " ", "-"}
		set qpa to (first word of thephone)
		set qpx to (second word of thephone)
		set qpp to (third word of thephone)
		set AppleScript's text item delimiters to ""
		tell application "Safari"
			activate
			open location ("http://ypng.infospace.com/home/yellow-pages/redir.htm?" & 
				"qfm=p&searchtype=all&fromform=psearch&QK=10&top=1&" & 
				"qcat=reverse&qsubcat=revphone&wqp=" & wpq & "&qpa=" & 
				qpa & "&qpx=" & qpx & "&qpp=" & qpp & "&x=0&y=0")
		end tell
	end perform action
end using terms from


[ Reply to This | # ]
Cant Compile ...
Authored by: alexmathew on Jan 20, '04 06:21:42PM

Script Editor returns an error:
Expected "given" "into" "with" but found "property"
Saving as Text does not help.

Also, if Address Book Plug-Ins directory does not exist - we are to create them? Can it be placed in /Library/ instead of ~/Library/

Thanks
AM



[ Reply to This | # ]
Cant Compile ...
Authored by: jordanburke on Jan 21, '04 05:35:43PM

I also get the complie problem. I am new to apple script, so it might be my fault. I copied the code and pasted it in Script Editor. I went to save it and it said it wouldn't compile it. I saved it anyway in the library/address book plugins/ location but nothing worked in the Address Book once I restarted it. Should they appear in the apple script menu or should they be activated by a right mouse click? Thanks.



[ Reply to This | # ]
Do reverse number lookup and age calcs in Address Book
Authored by: ahbe on Jan 21, '04 10:42:14AM

Ok, I copied and pasted the scripts, and saved them as applications under ~/Library/Scripts/Address Book Scripts/ and now they show up under the script menu in Address Book. However, when I try and run them nothing happens. What am I doing wrong? Thanx for the help.



[ Reply to This | # ]
Do reverse number lookup and age calcs in Address Book
Authored by: rwintheiser on Jan 21, '04 02:50:25PM

maybe I am just being dense but I have this problem too. Additionally, if I put these where the author recommends they do not appear in the scripts menu.



[ Reply to This | # ]
Do reverse number lookup and age calcs in Address Book
Authored by: dmmorse on Jan 21, '04 04:26:28PM

you are putting the scripts in the wrong location. They need to go in ~/Library/Address Book Plug-Ins/. In this location, they will not show up in your scripts menu, but rather will provide information when you click on the "phone" or "birthday" fields, respectively.



[ Reply to This | # ]
Do reverse number lookup and age calcs in Address Book
Authored by: dmmorse on Jan 21, '04 04:28:33PM

...And one more thing, don't forget to restart Address Book



[ Reply to This | # ]
Do reverse number lookup and age calcs in Address Book
Authored by: ahbe on Jan 22, '04 12:54:39AM

Whoohoo! Thanx for the help. A simple right click and now it works just fine. I noticed there are lots of new right click stuff I didn't know about in address book. One that will really save me a bunch of time is right clicking on the address you can automatically get a map. Very nice...



[ Reply to This | # ]
Multi-country/international ammended script
Authored by: jolinwarren on Jan 23, '04 07:02:14AM
I took the "Infospace" version of the reverse look-up script by "hughescr" and modified it to provide the following:

  • Displays the country name where the address will be searched for.
  • Disables the menu item if the country is unsupported.
  • Allows for adding different search methods for different countries.
  • Uses the System's default web browser instead of always using Safari.
  • Allows for the use of '.' as a number separator (as in '212.345.3456').

Note that currently, this script only performs reverse look-ups for US phone numbers. To add support for other countries, simply add to the end of the script where it says "handle reverse lookup for Another Country here, etc...". Right now, if no country is specified on an address card, the script defaults to the USA. This can also be changed in the script. Enjoy!


using terms from application "Address Book"
	on action property
		return "phone"
	end action property
	
	on action title for aPerson with aPhone
		if (country of address 1 of aPerson) = missing value then
			return "Look up USA address"
		else
			return "Look up " & country of address 1 of aPerson & " address"
		end if
	end action title
	
	on should enable action for aPerson with aPhone
		if ((country of address 1 of aPerson) = "USA") or 
			((country of address 1 of aPerson) = missing value) then
			return true
		else
			return false
		end if
	end should enable action
	
	on perform action for aPerson with aPhone
		
		set thephone to ((value of aPhone) as string)
		set wpq to 1
		if (label of aPhone) = "work" then
			set wpq to 0
		end if
		set origDelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"(", ")", " ", "-"}
		try
			set qpa to (first word of thephone)
			set qpx to (second word of thephone)
			set qpp to (third word of thephone)
			
		on error
			set AppleScript's text item delimiters to {"."}
			set theWords to text items of thephone
			set qpa to (first item of theWords)
			set qpx to (second item of theWords)
			set qpp to (third item of theWords)
		end try
		
		set AppleScript's text item delimiters to origDelim
		
		--handle look-ups for specific countries
		if ((country of address 1 of aPerson) = "USA") or 
			((country of address 1 of aPerson) = missing value) then
			
			set theURL to "http://ypng.infospace.com/home/yellow-pages/redir.htm?" & 
				"qfm=p&searchtype=all&fromform=psearch&QK=10&top=1&" & 
				"qcat=reverse&qsubcat=revphone&wqp=" & wpq & "&qpa=" & 
				qpa & "&qpx=" & qpx & "&qpp=" & qpp & "&x=0&y=0"
			tell application "System Events" to open location theURL
			
		else if (country of address 1 of aPerson) = "Another Country" then
			--handle reverse lookup for "Another Country"  here, etc...	
			
		end if
		
	end perform action
end using terms from


[ Reply to This | # ]
FYI: action property "date" is really "custom date"
Authored by: BobVB on Feb 09, '04 12:56:06AM

I was trying to make a generic 'time since' for the date field but couldn't get it to highlight with roll over. After scanning the Address Book code I found that the returned value is "custom date" NOT "date" as it says in the Applescript Library description.

Once I used "custom date" the altered script worked fine.



[ Reply to This | # ]
Do reverse number lookup and age calcs in Address Book
Authored by: bservo on Feb 12, '04 05:05:43PM
FYI. An age calculation script was included as an example in Apple's Address Book developer release notes.

I came across this via Google several months ago when researching how to launch IM applications for IM fields in Address Book, covered in an earlier Mac OS X hint.

[ Reply to This | # ]