Macro for pasting the current, physical geographical location

I’d like to have the possibility to build macros for my journaling workflow in which KM is entering not only the date, but also the current geolocation. I made some research but it seems that KM is not able to do it by itself. So I will probably have to look into building a script, which is fine. But before doing that I wanted to ask if anyone has done something similar and how did they do it. Thanks in advance!

Hey Martin,

There’s a handy little freeware scriptable app on the App-Store called Location Helper.

tell application "Location Helper"
   set geoLocString to get location string
end tell

It gives you output like this:

“<+36.11432963,-97.0551724> +/- 65.00m (speed -1.00 mps / course -1.00) @ 09/01/2016, 19:15:08 Central Daylight Time”

You can also get this information as an AppleScript record, which makes the data easier to work with.

And there’s more (see the app’s AppleScript dictionary by dropping it on the Script Editor.app).

-Chris

1 Like

Examples:

http://www.mousedown.net/mouseware/Location_Examples.html

1 Like

Very nice tip @ccstone! The following macro now inserts a reverse geolocated city name in the appropriate place.

This is the script…

# This needs the Location Helper App to work (available in the Mac App Store)
tell application "Location Helper"
   set geoLocString to reverse geocode location
   set geoLocCity to short_name of item 5 of address_components of item 1 of results of geoLocString
end tell

… and for completeness sake here's the little macro:

Thanks for sharing. Can you please post the macro file.

1 Like

Sure. Here you go! Journal Header.kmmacros (2.3 KB)

Seems to be possible, post Yosemite, with unassisted AppleScript too.

(As long as the launching app, e.g. Script Editor, Atom.app etc, has been given permission to access Location)

( Not sure how to set the delegate from JS )

use framework "CoreLocation"
use scripting additions

on run
    set locnManager to current application's CLLocationManager's alloc()'s init()
    locnManager's setDelegate:me
    locnManager's startUpdatingLocation()
    
    set locn to locnManager's location
    set coords to unwrap(locn's coordinate)
    
    set strCoord to intercalate(linefeed, ¬
        {"Latitude: " & latitude of coords, "Longitude: " & longitude of coords})
    
    set strFull to unwrap(locn's |description|)
    
    
    set strBoth to intercalate(linefeed & linefeed, {strCoord, strFull})
    set the clipboard to strBoth
    strBoth
end run


-- GENERIC FUNCTIONS

-- unwrap :: NSObject -> AS value
on unwrap(objCValue)
    if objCValue is missing value then
        return missing value
    else
        set ca to current application
        item 1 of ((ca's NSArray's arrayWithObject:objCValue) as list)
    end if
end unwrap

-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
    set {dlm, my text item delimiters} to {my text item delimiters, strText}
    set strJoined to lstText as text
    set my text item delimiters to dlm
    return strJoined
end intercalate
2 Likes

Looks like this kind of thing may suffice in JS for Automation (again, assuming that permission has been granted).

(function () {
    'use strict';


    ObjC.import("CoreLocation");

   
    return $.CLLocationManager.alloc.init.location.coordinate;

})();
2 Likes

Your AppleScript is failing for me, and I do have Script Editor with access to Locations:
Script Editor 2.8.1 (183.1) on macOS 10.11.4

Your JXA script also fails for me:

Let us know what you discover - seems to be working here on Sierra.

(Note that in your version of the original JS snippet you are attempting to concatenate a string with an object)

It is not clear what your script is supposed to return. There were no comments nor any description.

Please feel free to correct my code to return the location name.

Hey Guys,

I had to run several times in Script Debugger to get the system to ask me to approve it for location services.

So far I cannot get the Script Editor to do so… Ah, okay – I had to use the AppleScript to get the SE enabled.

Once I got the SE location-enabled the JavaScript started working.

OSX 10.11.6

A couple of questions:

How do you get the JavaScript to return the whole record?

Is there a way to make the location-manager disconnect from the calling app?

-Chris

For the full description string

(function () {
    'use strict';


    ObjC.import("CoreLocation");
   
    var locn = $.CLLocationManager.alloc.init.location;
    
    
    return ObjC.unwrap(locn.description);
    
    // or, more briefly, using equivalent syntactic sugar:
    // return locn.description.js;

})();

For other methods and properties, see:

disconnect from the calling app

Tell me more ? I'm being slow - will read it again this evening after a bit more coffee :slight_smile:

1 Like

Are you perhaps thinking about whether access to location data can be freed from the OS constraints on which apps have access to protected information ?

I think that might be like the attempt of the early Church Fathers to extricate the soul from the body – for the broader OS the running script is inescapably a material process embedded in a particular app, and subject to all the constraints which that app is heir to ...

But maybe that's not what you're after ?

Hey Rob,

Ah, thanks!

In my earlier tests the Location menu came up and did not go away. (I've rebooted since then.)

In running your latest JavaScript from the Script Editor I've found the Location menu goes away after a few seconds.

I'll test more when I have time.

-Chris

This is exactly what I am looking for and trying to duplicate what Day One app does. I paid for a full year subscription back in June but really think that keeping this in Scrivener is the best approach.

I would like to return the current address or at least city I am in and the current weather. For some reason I am getting the error.

error "Location Helper got an error: The application "Location Helper" parameter is missing for text." number -1701 from application "Location Helper" to text

I have the location app enabled for location services in system preferences and running. All the other commands seem to be working. I am not really interested in coordinates as much as I am the current address or again at least the city.

Not sure if there is another app that I can get the current temperature. I haven't found anything yet.

What macOS are you running? Ventura cracked down on locations permissions, and because of the new (god-awful) Preferences GUI, you can no longer manually authorize location services for specific applications. This means that the command line tools I used to use for this kind of thing no longer work.

However, there is an action in the Apple Shortcuts app that can get geolocation data, and I have switched to using it to get my current location. If you’re interested, let me know and I will share more info (I don’t want to immediately hijack this thread haha).

Yes that would be great I would love to see what you have. I am slowly going backwards on my computers do to different issues. I was on Ventura and then went back a couple OSs for 2013 Mac Pro and now on Catalina for my 2012 MacBook Pro. I have a new Mac mini coming so I will be back on Ventura again soon.

Basically the Shortcut returns the best location it can get (which is almost always a physical address). You can call the Shortcut via the command line, and since you can run shell scripts from KM, you can then parse the data any which way you want. Do the following steps, and let me know if it doesn’t work or you have questions.

  1. Download the Shortcuts shortcut via the link below (I’ve included a screenshot so you can see it’s contents; it’s incredibly simple).
  2. Download the Keyboard Maestro macro and execute it (I’ve included a screenshot of the macro as well, also very simple).
Link to Download KM Macro (click to expand/collapse)

Download Macro(s): Get geolocation using macOS Shortcut.kmmacros (2.2 KB)

KM Macro Screenshot

Link to Download Shortcut (click to expand/collapse)

Shortcuts

Shortcut Screenshot (click to expand/collapse)