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.

locationTable

A locationTable is a plain JS object with the following keys:

Key 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 Seconds since the Unix epoch

Types

This module provides the following types:

Properties

This module has no properties.

Methods

hs.location.lookupAddress(address) -> Promise<[[String:Any]]>

Geocodes an address string into an array of placemarkTables. Returns a Promise that resolves with an array of placemarkTable objects (sorted by relevance) or rejects with an error message.
hs.location.lookupAddress(address) -> Promise<[[String:Any]]>
Name Type Description
address string a free-form address string in any locale
Promise<[[String:Any]]>
a Promise resolving to an array of placemarkTables
hs.location.geocoder.lookupAddress("Apple Park, Cupertino")
    .then(p => console.log(p[0].locality, p[0].countryCode))

hs.location.lookupLocation(locationTable) -> Promise<[[String:Any]]>

Reverse-geocodes a locationTable into an array of placemarkTables. Returns a Promise that resolves with matching placemarks or rejects with an error.
hs.location.lookupLocation(locationTable) -> Promise<[[String:Any]]>
Name Type Description
locationTable {[key: string]: number} an object with at least `latitude` and `longitude`
Promise<[[String:Any]]>
a Promise resolving to an array of placemarkTables
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() -> [String: Any]

Returns the most recently cached location as a locationTable, or null. Activates Location Services if not already running. The cache is updated periodically while any watcher is running.
hs.location.get() -> [String: Any]
[String: Any]
a locationTable, 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 locationTables. Does not require Location Services.
hs.location.distance(from, to) -> number
Name Type Description
from {[key: string]: number} locationTable with at least `latitude` and `longitude`
to {[key: string]: number} locationTable with at least `latitude` and `longitude`
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 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 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
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)