The way I do it is I look at Address Book's "My Card" and give the user a list of all of the street addresses on it. When he chooses one of those addresses, that becomes the starting address. I save that in a property. The next time the user uses the plug-in the starting address is remembered, thanks to the property being set the first time, and that is exactly what I want. The problem is, if I quit Address Book, the property's value is forgotten, and the user has to set the starting address all over again.
So, the property works the way I want it to until Address Book is quit and re-launched. That's what it boils down to. Is there a way to make the property survive across relaunches? I'd appreciate any help on this. This is my first time with Properties and maybe I'm doing something wrong. Thanks a lot.
Here's the script. Put it into your /Library/Address Book Plug-Ins folder (saved as a script, scpt). You will have to restart Address Book for it to be an option (click on the label next to any street address to see the "* Map Directions To *" item, then choose it.)
--May 2012
--
property the_starting_point : ""
using terms from application "Address Book"
on action property
return "address"
end action property
--
on should enable action for theperson with theEntry
if theEntry ≠ missing value then
return true
else
return false
end if
end should enable action
on action title for theperson with theEntry
-- the string in the next line will appear when you click on an address' label in the Apple Address Book.
return "* Map Directions To *"
end action title
on perform action for theperson with theEntry
set the_addresses to {}
try
if the_starting_point is "" then
set myaddresses to every address of my card
repeat with an_address in myaddresses
set address_string to street of an_address & ", " & city of an_address & ", " & state of an_address & " " & zip of an_address as string
set clean_address_string to my stripReturns(address_string)
set end of the_addresses to clean_address_string
end repeat
if (count of the_addresses) > 1 then
set the_starting_point to choose from list the_addresses with prompt "Choose your starting point."
set the_starting_point to the_starting_point as string
display dialog "I'll remember that." buttons "OK" default button "OK"
else
set the_starting_point to item 1 of the_addresses as string
display dialog "Since you only have one address on your Address Book card, I'll use it as the starting point." buttons "OK" default button "OK"
end if
if the_starting_point is not "false" then
set the_starting_point to clean_it_up(the_starting_point)
else
set the_starting_point to ""
display dialog "You canceled." buttons "OK" default button "OK"
return
end if
end if
on error
display dialog "There's a problem. Be sure you have at least one address for yourself and that Address Book knows which card is yours."
end try
--
set the_street to street of theEntry
set cleanedStreet to clean_it_up(the_street)
--
set the_city to city of theEntry as string
set cleanedCity to clean_it_up(the_city)
--display dialog cleanedCity
--
set the_state to state of theEntry as string
set cleanedState to clean_it_up(the_state)
--
set the_zip to zip of theEntry as string
set cleanedZip to clean_it_up(the_zip)
--
set destinationAddress to cleanedStreet & ",+" & cleanedCity & ",+" & cleanedState & "+" & cleanedZip
--make the URL
my OpenTheURL(the_url)
end perform action
on clean_it_up(some_string)
set new_string to {}
set some_string to every character of some_string
set theCount to (count of some_string)
repeat with i from 1 to theCount
set the_character to item i of some_string as string
if the_character is " " then
set end of new_string to "+"
else
set end of new_string to the_character
end if
end repeat
return new_string as string
end clean_it_up
--
on OpenTheURL(some_url)
tell application "Safari"
activate
set the_window to make new document
set URL of the_window to some_url
end tell
end OpenTheURL
--
on stripReturns(some_string)
set new_string to paragraph 1 of some_string
set the_paragraphs to paragraphs of some_string
if (count of the_paragraphs) is greater than 1 then
repeat with i from 2 to count of the_paragraphs
set new_string to new_string & " " & item i of the_paragraphs
end repeat
end if
return new_string as string
end stripReturns
--
end using terms from
--
Christian Boyce
Christian Boyce and Associates
Mac, iPhone, and iPad Consultants
New Main Phone Number: 424-354-3548