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.com/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.com/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