This branches off the “cleaner date formatting” thread…
It’s a bit complicated. A script needs permission, location services needs to be turned on, the script needs to be run on the main thread, and it’s asynchronous, so you need to send the message and get a reply in another handler.
But this should give you something to play with:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "CoreLocation" -- for location
if not current application's CLLocationManager's locationServicesEnabled() as boolean then
error "Location Services not enabled"
end if
if current application's NSThread's isMainThread() as boolean then
my startSearching()
else
my performSelectorOnMainThread:"startSearching" withObject:(missing value) waitUntilDone:true
end if
on startSearching()
set locationManager to current application's CLLocationManager's alloc()'s init()
locationManager's setDelegate:me
locationManager's setDesiredAccuracy:(current application's kCLLocationAccuracyThreeKilometers)
locationManager's startUpdatingLocation()
end startSearching
on locationManager:locationManager didUpdateLocations:newLocations
locationManager's stopUpdatingLocation()
set newLocation to newLocations's lastObject()
set theCoordinates to newLocation's coordinate()
try
-- struct is passed as record in 10.11+
set coords to ((latitude of theCoordinates) as text) & ", " & longitude of theCoordinates
on error -- pre-10.10 we have to parse the description
set theDescription to current application's NSString's stringWithString:(newLocation's |description|())
set anNSScanner to current application's NSScanner's scannerWithString:theDescription
anNSScanner's setCharactersToBeSkipped:(current application's NSCharacterSet's characterSetWithCharactersInString:"<,")
set {theResult, aLat} to anNSScanner's scanDouble:(reference)
set {theResult, aLng} to anNSScanner's scanDouble:(reference)
set coords to (aLat as text) & ", " & aLng
end try
display dialog coords
end locationManager:didUpdateLocations:
on locationManager:anCLLocationManager didFailWithError:anNSError
error (anNSError's localizedDescription() as text)
end locationManager:didFailWithError: