As I cannot find any method to applescript the Maps application, how can I find driving distances between two addresses?
Based upon a prior wonderful discussion of scripting MapKit [topic]45915[/topic], I attempted to set a MapKit direction request source location to a SourceMKMapItem.
use framework "Foundation"
use framework "MapKit" --required for MKPlacemark
use scripting additions
set SourceAddressString to "Rockefeller Plaza, NY, NY"
set {theLat, theLong, fullAddress} to (my coordinatesForAddress:(contents of SourceAddressString))
set SourceMKMapItem to my MapItemWithPlacemarkFromAddress:fullAddress latitude:theLat longitude:theLong
my MKDirectionsRequestFrom:SourceMKMapItem
on MapItemWithPlacemarkFromAddress:fullAddress latitude:theLat longitude:theLong
# Get address components
set fullAddress to current application's NSString's stringWithString:fullAddress
set DataDetector to current application's NSDataDetector
set DataDetectorOfAddress to DataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeAddress) |error|:(missing value)
set FirstMatchOfDataDetectorAddress to DataDetectorOfAddress's firstMatchInString:fullAddress options:0 range:{0, fullAddress's |length|()}
if FirstMatchOfDataDetectorAddress = missing value then error "Can't interpret address"
# Create a dictionary of returned key values relative to this data detector object
set addressDictionay to FirstMatchOfDataDetectorAddress's components()
# Create a placemark using the coordinate and address dictionary
set aCoordinate to {latitude:theLat, longitude:theLong}
set aPlacemark to (current application's MKPlacemark's alloc()'s initWithCoordinate:aCoordinate addressDictionary:addressDictionay)
# Create a map item initiating with the placemark
set MapItemWithPlacemark to (current application's MKMapItem's alloc()'s initWithPlacemark:aPlacemark)
end MapItemWithPlacemarkFromAddress:latitude:longitude:
on coordinatesForAddress:AddressString
set AddressString to current application's NSString's stringWithString:AddressString
set AddressString to AddressString's stringByReplacingOccurrencesOfString:space withString:"+"
set urlString to "htt" & "ps://maps.googleapis.com/maps/api/geocode/json?address=" & AddressString
set theURL to current application's |NSURL|'s URLWithString:urlString
set theData to current application's NSData's dataWithContentsOfURL:theURL
set {theJson, theError} to (current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference))
if theJson is missing value then error theError's localizedDescription() as text
set theStatus to theJson's objectForKey:"status" -- check it went OK
if not (theStatus's isEqualToString:"OK") then error theStatus as text
set theResults to theJson's objectForKey:"results"
set thePred to current application's NSPredicate's predicateWithFormat:"types CONTAINS 'street_address'"
set bestResult to (theResults's filteredArrayUsingPredicate:thePred)'s firstObject()
if bestResult = missing value then set bestResult to theResults's firstObject()
set theLat to (bestResult's valueForKeyPath:"geometry.location.lat") as real
set theLong to (bestResult's valueForKeyPath:"geometry.location.lng") as real
set fullAddress to (bestResult's objectForKey:"formatted_address") as text
return {theLat, theLong, fullAddress}
end coordinatesForAddress:
on MKDirectionsRequestFrom:SourceMKMapItem
set directionsRequest to current application's MKDirectionsRequest
set directionsRequest to directionsRequest's setSource:SourceMKMapItem
end MKDirectionsRequestFrom:
My applescript errs on the following command:
set directionsRequest to directionsRequest's setSource:SourceMKMapItem
with the following error:
- 1. What is the correct method to set the source property for MapKit’s DirectionRequest using a MapItem?[/*]
- 2. As my overall goal is to applescript driving distances between two geographical locations, what might be another method?[/*]