Distance from Location

I am trying to highlight addresses in Safari and then search the distance from a predefined location. Can anyone think of a better way to do this than what I am doing by waiting for a google map to load and then pressing return to copy and paste the URL? Seems like something with forms action or Javascript might be better but maybe I am just over thinking it.

Safari Control.kmactions (697 B)

If you get longitude and latitude (as floating point numbers) from Google Maps, you can use the Haversine formula in an Execute JXA action to derive a distance:

https://rosettacode.org/wiki/Haversine_formula#ES6

(One could write an AppleScript version, but it’s probably not quite the right language, as it would have to call out anyway for the trigonometric functions. )

Thanks, I’ll have to check this out more, looks like some good stuff here.

Here's a very rough and indicative sketch of one approach (using miles (if you prefer km, or another planet, then you need to replace the value used for the planetary radius, and the final unit label).

If you get an NaN value, then the argument of the sleep command (waiting for the Google server to respond) will need to be increased.

JavaScript for Automation source

(() => {
    'use strict';

    // GENERIC ------------------------------------------------------------

    // standardAdditions :: () -> Application
    const standardAdditions = () =>
        Object.assign(Application.currentApplication(), {
            includeStandardAdditions: true
        });

    // HAVERSINE ---------------------------------------------------------

    // haversine :: (lat :: Float, lng :: Float) ->
    // (lat :: Float, lng :: Float) -> Float
    const haversine = (dctFrom, dctTo) => {
        // Math lib function names
        const [
            pi, asin, sin, cos, sqrt, pow, round
        ] = [
            'PI', 'asin', 'sin', 'cos', 'sqrt', 'pow', 'round'
        ]
        .map(k => Math[k]),

            // degrees as radians
            [rlat1, rlat2, rlon1, rlon2] = [
                dctFrom.lat, dctTo.lat, dctFrom.lng, dctTo.lng
            ]
            .map(x => x / 180 * pi),

            dLat = rlat2 - rlat1,
            dLon = rlon2 - rlon1,
            radius = 3962.17; // radius of Earth in miles
        // Adjust for other units or planets

        // km
        return round(
            radius * 2 * asin(
                sqrt(
                    pow(sin(dLat / 2), 2) +
                    pow(sin(dLon / 2), 2) *
                    cos(rlat1) * cos(rlat2)
                )
            ) * 100
        ) / 100;
    };

    // MAIN --------------------------------------------------------------
    const
        kme = Application('Keyboard Maestro Engine'),
        sa = standardAdditions(),
        [dctFrom, dctTo] = ['fromAddress', 'toAddress']
        .map(k => {
            const
                strURL = '"https://maps.googleapis.com/maps/api/' +
                'geocode/json?address=' + kme.getvariable(k)
                .split(/\s+/)
                .join('+') + '"',
                strJSON = sa.doShellScript('curl ' + strURL);

            sa.doShellScript('sleep 1');

            try {
                return JSON.parse(strJSON)
                    .results[0].geometry.location
            } catch (e) {
                return {}
            }
        });

    return '' + haversine(dctFrom, dctTo) + ' miles';
})();

Thank you.