How to Automate Finding My Location with Keyboard Maestro and 'Which City Am I In' Tool?

I'm trying to streamline a process on my Mac where I can automatically determine my current location and have that information easily accessible for various tasks. I came across the 'Which city am I in' tool, which accurately provides the city I'm located in based on my IP address. I'm wondering how I can use Keyboard Maestro to automate this process so that I can trigger it with a simple keystroke or when I connect to a new Wi-Fi network.

Specifically, I'm looking for advice on how to set up a macro in Keyboard Maestro that can:

Trigger the 'Which City Am I In' tool: Is it possible to have Keyboard Maestro open a specific webpage or tool, and then extract the city name from the result? If so, what would be the best approach to capture that information automatically?

Store the location data: Once the city name is retrieved, I'd like to save this information in a text file or display it in a notification. What are some efficient ways to handle and store this data within the macro?

Use conditions to trigger the macro: Can I set the macro to trigger automatically under certain conditions, like when my IP address changes or when I connect to a different Wi-Fi network? If so, how would I go about setting these conditions in Keyboard Maestro?

I've done some basic automation with Keyboard Maestro before, but integrating it with an external tool like 'Which City Am I In' is new to me. Iā€™m curious if anyone has done something similar or has ideas on the best way to approach this. Also, are there potential pitfalls I should be aware of when trying to extract data from an external tool and use it within Keyboard Maestro?

I'm eager to hear from others who have automated location-based tasks or used Keyboard Maestro for similar purposes. Any detailed guidance, especially with step-by-step examples or screenshots, would be greatly appreciated!

KM already has an action called Get Location which returns your latitude and longitude. All you would have to do is convert that to a city name. The method that you suggested will not work if you have a VPN, so I think you should adjust your path forward here.

Why precisely do you want a city name? How does a city name help you? Coordinates are much more accurate. The city of New York is 11,000 sq miles. Don't you want more precision than that?

I don't think it would be too hard to create a macro that uses trigonometry to calculate the nearest city for a given latitude/longitude coordinate, and you wouldn't have to rely on a third party website to do that. I think I can see a way to do that in roughly a single KM statement. So it would be very fast.

I have done similar things in the past like calculating time zones and times for sunsets and sunrises.

Okay, I got a single line shell command to solve this problem. You need to get a source file containing all the cities and their latitude, longitude. Then you use the following command to find the closest one: (where 37 and -118 are your latitude and longitude, in this example, in fields 7 and 8. You can substitute your own values and field numbers.)

awk -F "," '{print $0 "," ((37-$7)^2+((-118)-$8)^2)^0.5}' | sort -t "," -k 10 -n -r | head -n 1 | sed 's/,.*//'

This command will calculate the distance of each city's coordinates from your specified coordinates, sort the result by shortest distance to longest distance, take the shortest one which is the first line of the result, then select the first field of the first line.

In my case, my source file started like this, but your source file may differ:

New York,New York,TRUE,NY,New York,Queens,40.6943,-73.9249,18908608
Los Angeles,Los Angeles,TRUE,CA,California,Los Angeles,34.1141,-118.4068,11922389
Chicago,Chicago,TRUE,IL,Illinois,Cook,41.8375,-87.6866,8497759
Miami,Miami,TRUE,FL,Florida,Miami-Dade,25.7840,-80.2101,6080145
Houston,Houston,TRUE,TX,Texas,Harris,29.7860,-95.3885,5970127
Dallas,Dallas,TRUE,TX,Texas,Dallas,32.7935,-96.7667,5830932
Philadelphia,Philadelphia,TRUE,PA,Pennsylvania,Philadelphia,40.0077,-75.1339,5683533
Atlanta,Atlanta,TRUE,GA,Georgia,Fulton,33.7628,-84.4220,5180179

My shell command, above, adds the "distance" to each city as an additional value on each line, then sorts the lines by distance in degrees (technically, the square of the distance), then tells you the one that's closest.

My source data was for US cities, but if you want world cities, you can probably find that online too.

1 Like

Give Shortcuts a go -- it is likely to be more accurate then IP-based location, and certainly more private!

It's as simple as making a Shortcut with the following actions:

...and running it once to allow the necessary permissions, then calling it from KM:

image

That will display the nearest town/city in a dialog, but you can instead save it to a variable and do whatever you want with the data.

1 Like

What a beautiful solution. But there are still some good ideas in my solution, too. I think I need to create a macro that automatically presses the like button on any message you upload here.

Yes, I like the idea. I just blanche at the extra work :wink:

Anyone who wants to split difference and use KM's %GetLocation% without rolling their own city list could look at the Google Maps "Reverse geocoding" API. It's a paid service but you get $200/month credit -- so you could make up to 40k geolocation requests a month for free (if I'm reading it right -- check carefully!).