Applescript to estimate driving times

I'm trying to automate making accurate alarms to help me estimate when I need to leave to be on time for meetings.

To do this accurately, I want to access Apple Maps (or Google Maps services, or ...) to get an estimate of driving times given current driving conditions. So I'd like to query the maps service reasonably close to departure time. (If I get an estimate on Sunday evening at for an appoint on Monday at rush hour leaving from a different location, it's obviously going to be inaccurate.)

Ideally, I'd like to directly access my Apple Calendar to get the appointments and their locations. But if that's too complicated, I'll just create a formatted text file or sqlite file or whatever with the information.

I've considered doing this with Shortcuts, Automator, AppleScript, JXA, Hammerspoon, Python, etc. It seems that each technology has part of what I need, but not all. But I don't have deep knowledge of any of them. Perhaps someone can advise me on the most appropriate technology.

Here are my impressions: Shortcuts: Has built in access to driving time estimates and Calendar events, but awkward and possibly too limited in terms of program logic. Automator: Is more capable than Shortcuts in some ways, less in others. AppleScript: I don't know. JXA: I prefer Javascript to AppleScript, but it seems hard to find good documentation. Python: I'm quite proficient in Python, but I don't know if I can access things like Calendar events. If not, I'm willing to keep a separate file manually. (I could perhaps create the Calendar events from this, except that some appointments are set up by my work.) Hammerspoon: I don't have any experience with it and don't know it's capabilities.

Any advice appreciated.

Replies

A combination can be used. Shortcuts and Automator both have AppleScript, JavaScript/JXA, and shell script actions, so built-in actions can be used to get results that are then passed to your code. AppleScriptObjC, JXA, and PyObjC can also use (most) Cocoa frameworks.

I wrote this a couple years ago using Python, using Google Maps, hoping to find a good time to miss traffic. (Spoiler: There isn't one at a reasonable hour.) I hope it is ok to post a Google-centric solution. Enjoy!

#!/usr/bin/python import urllib.request, json, datetime, pytz

url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=Denver+CO&destinations=Boulder+C O&key=putYourKeyHere" response = urllib.request.urlopen(url) data = json.loads(response.read())

t = datetime.datetime.now() now_utc = datetime.datetime.now(pytz.timezone('US/Mountain')) t = now_utc.astimezone(pytz.timezone('US/Mountain'))

f = open('travelTime.csv','a') distanceT=data['rows'][0]['elements'][0]['distance']['text'] distanceV=data['rows'][0]['elements'][0]['distance']['value'] durationT=data['rows'][0]['elements'][0]['duration_in_traffic']['text'] durationV=data['rows'][0]['elements'][0]['duration_in_traffic']['value']

f.write("%s,%s,%s,%s,%s\n" % (t.strftime("%Y-%m-%d %H:%M:%S"), distanceT, distanceV, durationT, durationV)) f.close()

  • Sorry..that wrapped funny. "response = .." should be on a new line.

Add a Comment