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

Address Book Driving Directions Apps
Inspired by the Google Maps plug-in hints (1, 2), I created this AppleScript Address Book plug-in to get driving directions. Unlike the others, this one uses MapQuest and your default web browser (instead of Google Maps and Firefox), and it allows you to set a default starting address instead of just using the first one in your card.

First, launch Script Editor paste the code into a new script, and save it to ~/Library/Address Book Plug-Ins/Address Book Driving Directions.scpt. Then when you launch Address Book, there'll be a new menu item when you click on an address caption.

If your card has more than one address, you have to set one as the default starting point by selecting the 'Driving Directions From' menu item. This will open a browser window at MapQuest's driving directions form with the starting address pre-filled. It also sets that address as the default value for driving directions from other addresses (its ID is saved in Address Book's defaults).

Then when you click on an address in any other card, the menu option is called 'Driving Directions To,' and it uses the address you selected above as the starting point. Feel free to improve the script, but please post any bug fixes in follow-ups to this hint. Enjoy!

[robg adds: I tested this one, and it works as expected...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[7,960 views]  

Address Book Driving Directions | 9 comments | Create New Account
Click here to return to the 'Address Book Driving Directions' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Address Book Driving Directions
Authored by: notverypc on Feb 28, '05 09:02:14AM

Very cool script but can someone with a bit more Scripting knowledge convert it to use mapquest.co.uk instead of .com??



[ Reply to This | # ]
Address Book Driving Directions (UK version)
Authored by: slider on Feb 28, '05 04:00:27PM

Here is the modified version for use in UK:

using terms from application "Address Book"
on action title for p with v
-- if My Card is has no address, don't show the menu item
set myCard to my card
if myCard is null then
return
end if
if addressCount(myCard) is less than 1 then
return
end if

-- check if the clicked address' card is in My Card, then set the menu item accordingly
if myCard is equal to p then
return "Driving Directions From"
else
set defaultStartingAddressID to defaultStartingAddressID()
if addressCount(myCard) is equal to 1 or (defaultStartingAddressID is not null and getAddressFromCard(defaultStartingAddressID, myCard) is not null) then
return "Driving Directions To"
else
return "Choose a starting address in " & name of myCard & "'s card for Driving Directions"
end if
end if
end action title

on perform action for p with v
set mapUrl to ""
set myCard to my card
if myCard is not equal to null then
if myCard is equal to p then
-- clicked on an address in My Card, so prefill start adddress in MapQuest's form

set startingAddress to ""
if country of v is not missing value then set startingAddress to startingAddress & "1y=" & country of v
if street of v is not missing value then set startingAddress to startingAddress & "&1a=" & street of v
if city of v is not missing value then set startingAddress to startingAddress & "&1c=" & city of v
if state of v is not missing value then set startingAddress to startingAddress & "&1s=" & state of v
if zip of v is not missing value then set startingAddress to startingAddress & "&1z=" & zip of v
set mapUrl to "http://www.mapquest.co.uk/directions/main.adp?src=maps&1ex=1&1v=ADDRESS&" & startingAddress

setDefaultStartingAddressID(id of v) -- use the clicked address as the default for future invocations
else
-- clicked on another person's address, so use that as the destination, and the first or default address in My Card as the start
if addressCount(myCard) is equal to 1 then
set myAddress to myCardAddressByIndex(1)
else
set defaultStartingAddressID to defaultStartingAddressID()
set addressFromCard to getAddressFromCard(defaultStartingAddressID, myCard)
if addressFromCard is null then
display dialog "Choose a starting address in " & name of myCard & "'s card for Driving Directions" buttons {"OK"} default button "OK"
else
try
set addressID to defaultStartingAddressID()
tell myCard
set myAddress to address id addressID
end tell
end try
end if
end if

if myAddress is not equal to null then
set startingAddress to ""
if country of myAddress is not missing value then set startingAddress to startingAddress & "1y=" & country of myAddress
if street of myAddress is not missing value then set startingAddress to startingAddress & "&1a=" & street of myAddress
if city of myAddress is not missing value then set startingAddress to startingAddress & "&1c=" & city of myAddress
if state of myAddress is not missing value then set startingAddress to startingAddress & "&1s=" & state of myAddress
if zip of myAddress is not missing value then set startingAddress to startingAddress & "&1z=" & zip of myAddress

set endingAddress to ""
if country of v is not missing value then set endingAddress to endingAddress & "&2y=" & country of v
if street of v is not missing value then set endingAddress to endingAddress & "&2a=" & street of v
if city of v is not missing value then set endingAddress to endingAddress & "&2c=" & city of v
if state of v is not missing value then set endingAddress to endingAddress & "&2s=" & state of v
if zip of v is not missing value then set endingAddress to endingAddress & "&2z=" & zip of v

set mapUrl to "http://www.mapquest.co.uk/directions/main.adp?go=1&do=nw&un=m&cl=EN&ct=NA&rsres=1&" & startingAddress & endingAddress
end if
end if
end if

if mapUrl is not equal to "" then
tell application "System Events"
open location mapUrl
end tell
end if
end perform action

on addressCount(card)
try
tell card
set allAddresses to (every address)
return length of allAddresses
end tell
on error
return 0
end try
end addressCount

on myCardAddressByIndex(addressIndex)
try
set myCard to my card
tell myCard
set allAddresses to (every address)
set myCardAddressByIndex to item addressIndex of allAddresses
end tell
on error
set myCardAddressByIndex to null
end try
return myCardAddressByIndex
end myCardAddressByIndex

on defaultStartingAddressID()
try
set addressID to do shell script "defaults read com.apple.AddressBook drivingDirectionAddressID"
on error
set addressID to null
end try
return addressID
end defaultStartingAddressID

on setDefaultStartingAddressID(addressID)
do shell script "defaults write com.apple.AddressBook drivingDirectionAddressID " & (addressID as string)
end setDefaultStartingAddressID

on getAddressFromCard(addressID, card)
tell card
try
set addressFromCard to address id addressID
on error
set addressFromCard to null
end try
end tell
return addressFromCard
end getAddressFromCard

on action property
return "address"
end action property

on should enable action for p with v
return true
end should enable action
end using terms from

Use identical instructions to parent post to build and use.



[ Reply to This | # ]
Address Book Driving Directions (UK version)
Authored by: notverypc on Mar 01, '05 05:48:02AM

Thanks for the script but I'm getting an "404" error when the script trys to access the Mapquest Site. It also says "The requested URL /directions/main.adp was not found on this server."

I'll try to figure this out myself but as my Scripting knowledge is minimal I would apprecicate any help you can offer.



[ Reply to This | # ]
Address Book Driving Directions
Authored by: guvadafe on Feb 28, '05 01:12:20PM

It might be helpful if you indicate what version of Applescript is requried. I use OSX 10.2.6 and this script won't compile on this version. Yes, I'll be updating once I see more of the 'bugs' in 10.3 are fixed.



[ Reply to This | # ]
Re: Bugs in Panther
Authored by: ChrisAllison on Mar 01, '05 01:04:42PM
Exactly what bugs are you talking about? Are you joking, or being serious?

I'm not disputing that bugs may exist, but at this point, with Panther 10.3.9 builds already being seeded, and Tiger due out in the first half of this year (I do believe it will appear - contrary to what nay-sayers may have to... say), you're doing a little more than avoiding the bleeding edge, you're staying a few big 'ol steps back from the entire pack.

Why is that? I'm legitimately curious.

[ Reply to This | # ]
Address Book Driving Directions
Authored by: lenbudney on Mar 01, '05 09:41:03AM

I think I've found a bug: I can only get one set of driving directions. After that, the context menu does nothing until I restart address book.



[ Reply to This | # ]
Address Book Driving Directions
Authored by: oralibre on Mar 01, '05 10:50:52AM

Found out, when you change the line in the script from (set mapUrl to "http://www.mapquest.com/directions/main.adp?") to (set mapUrl to "http://www.mapquest.com/directions/europe.adp?") and when you use two letters for your Countrycode, like DE for Germany, the script works for europe too, but as mentioned before, only once, then you have to restart the addressbook.



[ Reply to This | # ]
Address Book Driving Directions
Authored by: ebernet on Jul 25, '05 07:09:37PM

Since Google Maps now works fine with Safari, you can change the "Firefox" designator to "URL Access Scripting" and "OpenURL" command to "open location". Like that you will get to view the directions in your default browser.



[ Reply to This | # ]
Address Book Driving Directions
Authored by: eduo on May 09, '06 12:52:08PM

Not that anyone probably cares, but I've gone ahead and modified the script to work for Google Spain and made it work with any browser out there set as default. If there is a need for a version in enligsh let me know:
http://www.eduo.info/2006/05/07/gmaps_en_addressbook/

No que a nadie le importe, pero he modificado estos scripts para funcionar en Google España y con cualquier navegador que este por defecto. Si existe una necesidad de una versión en ingles dejadme saber:
http://www.eduo.info/2006/05/07/gmaps_en_addressbook/

---
Eduo



[ Reply to This | # ]