Show an address in Google Maps, Map24.com etc.

Has anybody ever tried a script to query online mapping services like Google Maps, Map24.com …?
This will involve some AJAX & Javascripting, which is the reason that it’s too far for me.

I would like a “do script” statement to display a given (street, city, country) → centralised onto a (zoomable) browser map…

Hi Eelco,

The following is a script I’m writing to obtain geographic coordinates from Google Earth for creating waypoints for my GPS device.
It’s still under construction but it may help you.


-- This Address Book Tell is a modification of a script written by StefanK, Sankt Gallen, Swizerland in response to a question from contactzero on MacScripter

tell application "Address Book"
	set tmp to {}
	set mySelection to selection
	repeat with aPerson in mySelection
		set theName to (first name of aPerson) & space & (last name of aPerson)
		set _addresses to addresses of aPerson
		log _addresses
		repeat with anAddress in _addresses
			tell anAddress
				set theLabel to the label of anAddress
				set theStreet to the street of anAddress
				set theCity to the city of anAddress
				set theState to the state of anAddress
				set theZip to the zip of anAddress
				set theCountry to the country of anAddress
			end tell
		end repeat
	end repeat
end tell


-- Get coordinates from Google Earth
tell application "Google Earth"
	activate
	set cmtLn to theStreet & ", " & theCity & ", " & theState & ", " & theZip & ", " & theCountry
	set the clipboard to cmtLn
	tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke "a" using command down -- highlight any text in the "Fly to" window
	tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke "v" using command down
	tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke return
	delay 1
	
	set {lat, long} to {latitude, longitude} of (GetViewInfo)
	
end tell

-- Write GPX file
set gpxFile to theName & ".GPX"
set gpxFileRef to (open for access file gpxFile with write permission)
set boundsLn to "<metadata>" & return & tab & "<bounds minlat=" & quote & lat & quote & " minlon=" & quote & long & quote & " maxlat=" & quote & lat & quote & " maxlon=" & quote & long & quote & "/>" & return & "</metadata>"
set wptLn to "<wpt " & "lat=" & quote & lat & quote & space & "lon=" & quote & long & quote & ">" -- create wpt line
set nameLn to tab & "<name>" & theName & "</name>"
set cmtLn to tab & "<cmt>" & cmtLn & "</cmt>"
set descLn to tab & "<desc>" & cmtLn & "</desc>"
if theLabel contains "home" then
	set symLn to tab & "<sym>" & quote & "Residence" & quote & "</sym>"
else
	set symLn to tab & "<sym>" & quote & "Waypoint" & quote & "</sym>"
end if

try
	set eof gpxFileRef to 0
	write "<gpx version=\"1.1\" creator=\"AB2GPX\"
  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  
  xmlns=\"http://www.topografix.com/GPX/1/1\"
  xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 [url=http://www.topografix.com/GPX/1/1/gpx.xsd\>]http://www.topografix.com/GPX/1/1/gpx.xsd\">[/url]
" & return & boundsLn & return & return & wptLn & return & nameLn & return & cmtLn & return & descLn & return & symLn & return & "</wpt>" & return & return & "</gpx>" to gpxFileRef
on error error_message number error_number
	display dialog error_message & return & error_number & return & "<gpx"
end try

close access gpxFileRef

Cheers and Good Luck,
Tony

Model: MacBook
AppleScript: 2.0.1
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

Thanks apsharman ,

That’s fairly good (using Google maps would be faster on a light machine, though…)
I have slightly midified the script (to accomodate that my local addresses without country are in Holland, plus that I found that Google Earth better processes a space than a “missing value”)
What’s the idea of writing a gpx file…?


-- This Address Book Tell is a modification of a script written by StefanK, Sankt Gallen, Swizerland in response to a question from contactzero on MacScripter

tell application "Address Book"
	set tmp to {}
	set mySelection to selection
	repeat with aPerson in mySelection
		set theName to (first name of aPerson) & space & (last name of aPerson)
		set _addresses to addresses of aPerson
		log _addresses
		repeat with anAddress in _addresses
			tell anAddress
				set theLabel to the label of anAddress
				set theStreet to the street of anAddress
				set theCity to the city of anAddress
				set theState to the state of anAddress
				set theZip to the zip of anAddress
				set theCountry to the country of anAddress
			end tell
		end repeat
	end repeat
end tell
if theCountry = missing value then set theCountry to "Netherlands"
if theState = missing value then set theState to ""
if theZip = missing value then set theZip to ""
set cmtLn to theStreet & ", " & theCity & ", " & theState & ", " & theZip & ", " & theCountry
set cmtLn to my substitute(cmtLn, "missing value", "")
--return cmtLn

-- Get coordinates from Google Earth
tell application "Google Earth"
	activate
	set the clipboard to cmtLn
	tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke "a" using command down -- highlight any text in the "Fly to" window
	tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke "v" using command down
	tell application "System Events" to tell (name of application processes whose frontmost is true) to keystroke return
	delay 1
	set {lat, long} to {latitude, longitude} of (GetViewInfo)
end tell

-- Write GPX file
set gpxFile to theName & ".GPX"
set gpxFileRef to (open for access file gpxFile with write permission)
set boundsLn to "<metadata>" & return & tab & "<bounds minlat=" & quote & lat & quote & " minlon=" & quote & long & quote & " maxlat=" & quote & lat & quote & " maxlon=" & quote & long & quote & "/>" & return & "</metadata>"
set wptLn to "<wpt " & "lat=" & quote & lat & quote & space & "lon=" & quote & long & quote & ">" -- create wpt line
set nameLn to tab & "<name>" & theName & "</name>"
set cmtLn to tab & "<cmt>" & cmtLn & "</cmt>"
set descLn to tab & "<desc>" & cmtLn & "</desc>"
if theLabel contains "home" then
	set symLn to tab & "<sym>" & quote & "Residence" & quote & "</sym>"
else
	set symLn to tab & "<sym>" & quote & "Waypoint" & quote & "</sym>"
end if

try
	set eof gpxFileRef to 0
	write "<gpx version=\"1.1\" creator=\"AB2GPX\"
  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  
  xmlns=\"http://www.topografix.com/GPX/1/1\"
  xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 [url=http://www.topografix.com/GPX/1/1/gpx.xsd\>]http://www.topografix.com/GPX/1/1/gpx.xsd\">[/url]
" & return & boundsLn & return & return & wptLn & return & nameLn & return & cmtLn & return & descLn & return & symLn & return & "</wpt>" & return & return & "</gpx>" to gpxFileRef
on error error_message number error_number
	display dialog error_message & return & error_number & return & "<gpx"
end try

close access gpxFileRef

on substitute(theText, toReplace, newText)
	set AppleScript's text item delimiters to the toReplace
	set the allTheText to every text item of theText
	set AppleScript's text item delimiters to the newText
	set theText to the allTheText as string
	set AppleScript's text item delimiters to ""
	return theText
end substitute

Now I can open Google maps with the following script (still a little buggy)

tell application "Address Book"
	set tmp to {}
	set mySelection to selection
	repeat with aPerson in mySelection
		set theName to (first name of aPerson) & space & (last name of aPerson)
		set _addresses to addresses of aPerson
		log _addresses
		repeat with anAddress in _addresses
			tell anAddress
				set theLabel to the label of anAddress
				set theStreet to the street of anAddress
				set theCity to the city of anAddress
				set theState to the state of anAddress
				set theZip to the zip of anAddress
				set theCountry to the country of anAddress
			end tell
		end repeat
	end repeat
end tell
if theCountry = missing value then set theCountry to "Netherlands" -- or your country default
if theState = missing value then set theState to ""
if theZip = missing value then set theZip to ""
set cmtLn to theStreet & ", " & theCity & ", " & theState & ", " & theZip & ", " & theCountry
set cmtLn to my substitute(cmtLn, "missing value", "")
--return cmtLn
tell application "Safari"
	open location "http://maps.google.com/maps?q=" & cmtLn
	activate
end tell

on substitute(theText, toReplace, newText)
	set AppleScript's text item delimiters to the toReplace
	set the allTheText to every text item of theText
	set AppleScript's text item delimiters to the newText
	set theText to the allTheText as string
	set AppleScript's text item delimiters to ""
	return theText
end substitute


Hi Eelco,

The GPX file is used to create a waypoint in my GPS device. I’m a motorcyclist who does a lot of traveling throughout Europe.

Since my script can only handle a single address, I’m still working on it to handle multiple addresses. Additionally, I need to add error handling (sometimes, the addresses aren’t found). I’ll keep you informed.

Cheers,
Tony

Hi there,

thanks for the scripts!!!
Is there a possibility to get the coodrinates from the given adress and put them in e.g. the notice field?

Bye… WürfelMac