Created
April 25, 2023 21:49
-
-
Save SKaplanOfficial/4ea2a707824fc376de2913a125f262c6 to your computer and use it in GitHub Desktop.
JXA script to reverse geocode the user's current location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
ObjC.import('CoreLocation') | |
app = Application.currentApplication() | |
app.includeStandardAdditions = true | |
if (!$['CLLocationManagerDelegate']) { | |
ObjC.registerSubclass({ | |
name: 'CLLocationManagerDelegate', | |
superclass: 'NSObject', | |
protocols: ['CLLocationManagerDelegate'], | |
properties: { | |
locations: 'id' | |
}, | |
methods: { | |
'locationManager:didUpdateLocations:': { | |
types: ['void', ['id', 'id']], | |
implementation: function (lm, locs) { | |
this.locations = locs | |
return; | |
}, | |
}, | |
'locationManager:didFailWithError:': { | |
types: ['void', ['id', 'id']], | |
implementation: function (lm, error) { | |
locations = [] | |
app.displayAlert(error.localizedDescription.js) | |
return; | |
} | |
} | |
} | |
}) | |
} | |
lm = $.CLLocationManager.alloc.init | |
lm.requestAlwaysAuthorization | |
delegate = $.CLLocationManagerDelegate.alloc.init; | |
lm.delegate = delegate | |
lm.requestLocation | |
while (delegate.locations.js == undefined) { | |
runLoop = $.NSRunLoop.currentRunLoop | |
today = $.NSDate.dateWithTimeIntervalSinceNow(0.1) | |
runLoop.runUntilDate(today) | |
} | |
geocoder = $.CLGeocoder.alloc.init | |
placemarks = null | |
geocoder.reverseGeocodeLocationCompletionHandler(delegate.locations.objectAtIndex(0), (p, e) => { | |
placemarks = p | |
}) | |
while (placemarks == null) { | |
runLoop = $.NSRunLoop.currentRunLoop | |
today = $.NSDate.dateWithTimeIntervalSinceNow(0.1) | |
runLoop.runUntilDate(today) | |
} | |
placemark = placemarks.objectAtIndex(0) | |
placeName = placemark.name.js | |
placeStreetNum = placemark.subThoroughfare.js | |
placeStreet = placemark.thoroughfare.js | |
placeCity = placemark.locality.js | |
placeState = placemark.administrativeArea.js | |
placePostal = placemark.postalCode.js | |
placeCountry = placemark.country.js | |
return `${placeName}: ${placeStreetNum} ${placeStreet}, ${placeCity}, ${placeState} ${placePostal}, ${placeCountry}` | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment