This shortcut calculates travel times and distances for address pairs contained in a text file. Please note:
Each address pair consists of a start address, a linefeed, and a destination address. The format of an address must be STREET, CITY, STATE and the address must be within the United States. The user can set commonly-used addresses in the Dictionary action. There can be blank lines between but not within an address pair.
The travel times and distances are obtained from an Apple server, and, with a large number of address pairs, the shortcut can take a minute or more to run. Also, on occasion, the server is unable to find a valid address, and I don’t know the reason for that.
The Distance action has various options, and the user should set these to the desired values.
The shortcut included below uses the Get Addresses action to get the addresses from the text file input. I don’t know if this is better or worse than the approach used in the above shortcut.
I did resolve an issue which arises when the shortcut appears unable to find a known valid address. The address is in fact found, but an error is thrown because the driving distance or time cannot be calculated. I’m not aware of any way to handle this error within the shortcut.
A very good job done there, as you say it does work with US addresses and it does work in the UK with UK addresses if the address is formatted carefully!
dbrewood. Thanks for looking at my shortcuts, which got me to thinking about what exactly the Get Addresses action really does. As can be seen from the screencapture included below, this action returns addresses that it identifies as possible addresses but does not determine whether those addresses actually exist.
For either of the above shortcuts to be successful, it has to find valid addresses, plus the driving distance to those addresses, plus the driving time to those addresses. Otherwise, an error is thrown. This makes the shortcuts insufficiently reliable for use.
Anyways, I’ve been working to better understand the ins-and-outs of writing shortcuts, and I learned a lot about the location actions, which makes the effort worthwhile. Also, perhaps this will help other forum members in the future.
This shortcut prompts the user for a destination and then displays the driving time and distance from their current location to the destination. This shortcut works with addresses in the United States only. The following screencapture shows the first portion of the shortcut.
dbrewood. I mentioned above that these shortcuts are not very reliable with addresses in the US and don’t work at all with addresses in the UK. So, I don’t know how to comply with your request.
FWIW, the following shortcut works as you want but only with addresses in the US. You obviously have a better understanding of UK addresses than I do, and perhaps you can edit the shortcut to work there.
I tested the shortcut with the following addresses–the first address is the start address:
201 South Cortez Street, Prescott, AZ
200 West Washington Street, Phoenix, AZ
200 North Spring Street, Los Angeles, CA
1313 Disneyland Drive, Anaheim, CA
100 South Entrance Road, Grand Canyon Village, AZ
The Get Travel Time action reports an error and stops the shortcut if the travel time cannot be calculated. However, the Get Distance action simply returns “0 miles” if a travel distance cannot be calculated. So, if travel distance is all that’s needed, the following shortcut might be considered.
This shortcut is called by way of an AppleScript. The input is a list that contains a start and a destination address, and the output is a driving distance. All error handling is done within the AppleScript. The following screencapture only shows the first portion of the shortcut.
set theAddresses to {"201 South Cortez Street, Prescott, AZ", "200 West Washington Street, Phoenix, AZ"}
tell application "Shortcuts Events" to run shortcut named "Travel Distance" with input {item 1 of theAddresses, item 2 of theAddresses}
set drivingDistance to item 1 of result --> "99.7 miles"
I thought I would further explain the purpose of my shortcut in post 12.
It is commonplace in AppleScripts to call a shell command to perform some function–using the sips command to make a JPG file from a PNG file is an example. In a similar fashion, a user may want to call a shortcut to perform some function that cannot be done directly with an AppleScript. Getting the driving distance between two points is an example.
In an AppleScript, a shell command is called by way of the do shell script command, and a shortcut is called by way of a run command in a tell Shortcuts Events statement. You can also call a shortcut from within an AppleScript using what the documentation calls a “URL scheme”, but this is seldom useful.
The time it takes to run the do shell script command and to set a variable is 4 milliseconds. In contrast, the time it takes to run a shortcut and to set a variable is 95 milliseconds. So, there’s a price to pay when running a shortcut from an AppleScript.
This is just my personal opinion, but the circumstances where one would call a shortcut from an AppleScript are somewhat limited, and the reason is that the entire task is normally best done with a shortcut.
I’m planning a road trip and decided to see if one of these shortcuts would be helpful. I learned a few things:
For my purposes, it’s just as useful (and a lot simpler) to get driving distances from city to city as it is from address to address.
Driving time is easily added, but I didn’t need this, and it significantly slowed the shortcut.
The shortcut gives more accurate results if state names are not abbreviated. For example, the shortcut gave me the driving distance to Las Vegas, NV when I specified Las Vegas, NM. Specifying Las Vegas, New Mexico fixed this.