using terms from application "Address Book" on action title for p with v -- if My Card has no address, don't show the menu item set myCard to my card if myCard is null then return end if set addcount to addressCount(myCard) if addcount is less than 1 then return else if addcount is equal to 1 then setDefaultStartingAddressID(id of item 1 of every address of myCard) 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 if id of v is equal to defaultStartingAddressID() then return "Show default address" else return "Set as default address" end if else return "Get Google driving directions" 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 set addressID to defaultStartingAddressID() set myAddress to address id addressID of myCard if myCard is equal to p then if id of v is equal to addressID then set mapUrl to "http://maps.google.com/maps?q=" & street of myAddress & ", " & city of myAddress else setDefaultStartingAddressID(id of v) -- use the clicked address as the default for future invocations end if 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 set startingAddress to "from: " if street of myAddress is not missing value then set startingAddress to startingAddress & street of myAddress & ", " if city of myAddress is not missing value then set startingAddress to startingAddress & city of myAddress & " " set endingAddress to "to: " if street of v is not missing value then set endingAddress to endingAddress & street of v & ", " if city of v is not missing value then set endingAddress to endingAddress & city of v set mapUrl to "http://maps.google.com/maps?q=" & startingAddress & endingAddress 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 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 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