50

Some GUI apps use OS X location services, but I want to retrieve a Mac's physical location from the command line. This could be useful for running scripts, switching settings etc. The mechanism should be CoreLocation, not a Geo IP service (data too low-res, needs online connection).

Minimum requirement is output of longitude and latitude. Nice would be a number indicating the precision. Super nice: a geocoder that provides location names.

1
  • Please do not include answers directly in the question, it tends to confuse people and makes it rather difficult to understand what the question actually is. If you want to add sample output to the different solutions offered you can do so directly by editing the answers.
    – nohillside
    Commented Dec 17, 2014 at 13:34

6 Answers 6

33

vilmoskörte's answer appears to be a good one, but I took it upon myself to make a tool that does this for my own education. I put it on GitHub.

WhereAmI

A simple command line tool. No options, just runs and outputs Lat/Long, accuracy in meters, and a timestamp. It will need OS X Location Services to be turned on (System Preferences > Security & Privacy), and permission to gather your location (OS X will prompt you on first run).

Direct Download Link (.zip)

0
13

This already has been programmed, have a look at

http://iharder.sourceforge.net/current/macosx/locateme/

3
  • very nice. cool option: -g … Generate a Google Map URL
    – myhd
    Commented Aug 14, 2012 at 14:54
  • The author of the software has indicated that they are aware of the Mountain Lion problems and will look over the code to see if an easy fix is available.
    – bmike
    Commented Dec 29, 2012 at 20:32
  • This is the best answer. You can install with brew install locateme and it works on macOS Mojave.
    – mk12
    Commented Apr 8, 2019 at 19:27
8

I'm the author of LocateMe, and I just discovered that I have to copy LocateMe into the /Applications folder for it to work in Mountain Lion. I'm not sure what I'd have to do to avoid that -- probably something about getting a cert, which I'm not likely to pursue anytime soon.

In the meantime, for LocateMe and probably the others, try copying them to /Applications and see if you have better luck.

-Rob

1
  • I can confirm that works.
    – markhunte
    Commented Jan 2, 2013 at 10:09
6

I'm the author of CoreLocationCLI, mentioned in the comments above. Just wanted to note that we have moved the project to this location:

https://github.com/fulldecent/corelocationcli

Also, it now supports --once if you are only looking for a single output, rather than continuous output.

Lastly, there is a full binary on the project page as well if you would rather not compile it yourself. You will still need to authorize ("Would you like this app to be able to access your location") on the first app run.

2
5
+100

Those apps need to be signed with a developer certificate before they can authorise with the Core Location service. If you look in Console.app you can see the failed attempts to authorise:

31/12/2012 13:08:26.441 locationd[484]: Couldn't get information from PID 40084
31/12/2012 13:08:27.515 CoreLocationAgent[40086]: CodeSigningInforequest for pid=40084 

I’ve used my developer account to build and sign a new whereami binary. After you run it Mountain Lion will ask your permission to enable it to access location data.

1
  • This worked great for me in Mac 10.8.2
    – TJ Luoma
    Commented Jan 1, 2013 at 3:52
2

I know this is an old question, but it’s still the top result for getting location in terminal on Mac.

None of the other options here work any more (they are not able to request permissions for accessing location). After some digging, seems the most straightforward/native way is to use the shortcuts app to do the location query for you.

Here’s a sample shortcut that can get your current location (feel free to adapt it to your needs, as it itself is adapted), which you can run from the command line and print accordingly:

https://www.icloud.com/shortcuts/a8ce742fb5fe43caacb91bdca72a877f

You can get and print your current location in your terminal with something like this:

echo "{LAT}N {LON}E" | shortcuts run "Get Location" | tee

That sample shortcut supports lat, lon, altitude, address, street, city, state, zip, and country placeholders.

1
  • I sometimes get the error Make sure your device has Location Services enabled and try again; turning Wi-Fi on may help. Have you found a solution for that?
    – HappyFace
    Commented Feb 15 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .