API Docs

Determine the Mac's location via macOS Location Services.

Location data is obtained through WiFi network scanning and, where available, GPS hardware. User permission is required — call hs.permissions.requestLocation() before using any tracking features.

The module exposes a geocoder sub-object for forward/reverse geocoding without requiring Location Services.

Locations are HSLocation objects.

Types

This module provides the following types:

Object Types

Plain JavaScript objects that this module's methods and properties take or return:

HSPlacemark

A place found by geocoding (a "placemarkTable" in Hammerspoon 1). Fields other than location are absent if they're not relevant

Field Type Description
name optional string Place name
fullAddress optional string Full formatted address (e.g. "1 Apple Park Way, Cupertino, CA 95014, United States")
shortAddress optional string Abbreviated address (e.g. "Cupertino, CA")
locality optional string City
country optional string Country name
countryCode optional string ISO country code
location HSLocation Where the place is

HSLocation

A location, as Location Services reported it (a "locationTable" in Hammerspoon 1)

Field Type Description
latitude number Degrees north (positive) or south (negative)
longitude number Degrees east (positive) or west (negative)
altitude number Metres above sea level (0 if unknown)
horizontalAccuracy number Uncertainty radius in metres (-1 if invalid)
verticalAccuracy number Altitude accuracy in metres (-1 if invalid)
course number Direction of travel in degrees (-1 if invalid)
speed number Metres per second (-1 if invalid)
timestamp number When the location was determined, in seconds since the Unix epoch

Properties

This module has no properties.

Methods

hs.location.lookupAddress(address) -> Promise<HSPlacemark[]>

Geocodes an address string into an array of places.

Returns a Promise that resolves with an array of HSPlacemark objects (sorted by relevance) or rejects with an error message.

hs.location.lookupAddress(address) -> Promise<HSPlacemark[]>
Name Type Description
address string a free-form address string in any locale
Promise<HSPlacemark[]>
a Promise resolving to the places found
hs.location.geocoder.lookupAddress("Apple Park, Cupertino")
    .then(p => console.log(p[0].locality, p[0].countryCode))

hs.location.lookupLocation(locationTable) -> Promise<HSPlacemark[]>

Reverse-geocodes a location into an array of places.

Returns a Promise that resolves with matching placemarks or rejects with an error.

hs.location.lookupLocation(locationTable) -> Promise<HSPlacemark[]>
Name Type Description
locationTable object The location to look up, such as an HSLocation
latitude number
longitude number
Promise<HSPlacemark[]>
a Promise resolving to the places found
hs.location.geocoder.lookupLocation({ latitude: 37.3349, longitude: -122.0090 })
    .then(p => console.log(p[0].name))

hs.location.servicesEnabled() -> boolean

Returns true if Location Services are enabled system-wide.

hs.location.servicesEnabled() -> boolean
boolean
true if enabled, false otherwise
hs.location.servicesEnabled() // → true or false

hs.location.authorizationStatus() -> string

Returns the app's current Location Services authorization status as a string.

hs.location.authorizationStatus() -> string
string
"authorized", "denied", "restricted", or "notDetermined"
const status = hs.location.authorizationStatus()
if (status === 'notDetermined') hs.permissions.requestLocation()

hs.location.get() -> HSLocation | null

Returns the most recently cached location, or null.

Activates Location Services if not already running. The cache is updated periodically while any watcher is running.

hs.location.get() -> HSLocation | null
HSLocation | null
The location, or null if no cached location is available
const loc = hs.location.get()
if (loc) console.log(loc.latitude, loc.longitude)

hs.location.distance(from, to) -> number

Calculates the straight-line distance in metres between two locations.

Does not require Location Services.

hs.location.distance(from, to) -> number
Name Type Description
from object The first location, such as an HSLocation
latitude number
longitude number
to object The second location, such as an HSLocation
latitude number
longitude number
number
distance in metres, or -1 if either table is invalid
const d = hs.location.distance(
    { latitude: 51.5074, longitude: -0.1278 },
    { latitude: 48.8566, longitude:  2.3522 }
) // → ~341,000 metres

hs.location.sunrise(latitude, longitude, date) -> Date

Returns the time of sunrise for the given coordinates and date, or null if the sun does not rise on that date (polar night).

hs.location.sunrise(latitude, longitude, date) -> Date
Name Type Description
latitude number degrees north (positive) or south (negative)
longitude number degrees east (positive) or west (negative)
date optional Date the date to calculate for; pass null or omit to use today
Date
A Date object representing the time of sunrise, or null
const rise = hs.location.sunrise(51.5, -0.1)
console.log(rise.toTimeString())

hs.location.sunset(latitude, longitude, date) -> Date

Returns the time of sunset for the given coordinates and date, or null if the sun does not set on that date (midnight sun).

hs.location.sunset(latitude, longitude, date) -> Date
Name Type Description
latitude number degrees north (positive) or south (negative)
longitude number degrees east (positive) or west (negative)
date optional Date the date to calculate for; pass null or omit to use today
Date
A Date object representing the time of sunset, or null
const set = hs.location.sunset(51.5, -0.1)
console.log(set.toTimeString())

hs.location.addWatcher() -> HSLocationWatcher

Creates a new location watcher object. Call .start() on it to begin receiving updates. The watcher is automatically stopped when the module shuts down.

hs.location.addWatcher() -> HSLocationWatcher
an HSLocationWatcher
const w = hs.location.addWatcher()
w.setCallback((event, data) => console.log(event, data))
w.start()

hs.location.removeWatcher(watcher) -> None

Removes a previously created watcher and stops it if running.

hs.location.removeWatcher(watcher) -> None
Name Type Description
watcher HSLocationWatcher the watcher returned by addWatcher()
None
const w = hs.location.addWatcher()
w.start()
hs.location.removeWatcher(w)