Apple Maps Integration

My fight with Apple Maps continues and I’ve found a way to extract the URL (easy) and name (hard) of the selected item.

Maps is pretty unfriendly to this kind of thing these days.

The Get Address script is pretty straight forward:

tell application "System Events"
	tell process "Maps"
		click menu item "Copy" of menu "Edit" of menu bar 1
		delay 0.4    
    	return (the clipboard as text)
	end tell
end tell

However the Get Name script is a PITA because the short URL has to be expanded (curl), then the “name” parameter must be extracted (python):

tell application "System Events"
	keystroke "c" using command down
	delay 0.4
	set shortUrl to the clipboard
	set curl to "curl -Ls -o /dev/null -w '%{url_effective}' " & quoted form of shortUrl
	set longURL to do shell script curl
	set python to "python3 -c 'import sys;from urllib.parse import urlparse, parse_qs;parsed = urlparse(sys.argv[1]);params = parse_qs(parsed.query);print(params.get(\"name\")[0])' " & quoted form of longURL
	set decodedName to do shell script python
	return decodedName
end tell

Calling out to curl and python feels clumsy but I’m not an AppleScript expert - is there a better way?

I suspect you will need to have the Apple command line tools installed for this to work.

I thought maybe re-writing the Get Name script in JavaScript would be an improvement as I could avoid the call outs to curl/python. As it turns out there are restrictions on HTTP access and it doesn’t have the standard URL type for parsing. I got to the point where something 4x the size still needed curl and wasn’t actually working so bailed.

Thank you for your script, @psidnell .

We will have a look and see if we can improve it.

Thank you

1 Like

We have updated our script server with the script you provided, @psidnell . Thank you.

The script version is 408.

Python is taken out of the script because newer macOS versions do not come with Python.

It seems there is no way to expand the short URL locally. And curl is available on all Macs. So the script still uses curl to get the long URL.

Please let us know if there are any issues.

Thank you

Thanks for the update. I wonder if the “name” parameter (if available) would be better than using the first line of the address, otherwise the hook gets a rather generic title:

1 Like

Just changing “address” to “name” in the script gives the result below. Maybe using address as a fallback would be ideal?

Thank you for pointing this out, @psidnell .

I will fix it.

1 Like

The script server has been updated with the fix. Script version 410.

Thank you

1 Like

That’s working great - thank you!