CoreLocation

Is there any way to use Core Location from a normal stand-alone AppleScript file (.scp)? I am writing a plugin for a third-party application, and it doesn’t want an XCode AppleScript application, it just wants a stand-alone script.

I think no, because it needs the CoreLocation framework. If you just use a file to execute the script, it’s not going to work.

OK thanks. Well, I wrote a separate application that I called from my script, and that works fine…

However, the data I am getting back seems to be bogus, and it is different from the results that I get from Google Maps or other HTML5 geolocation pages (such as http://benwerd.com/lab/geo.php). They give me correct results (Latitude: 3.142325995, Longitude: 101.71022504), whereas the scripts presented on this forum give me completely false results (Latitude: 52.351989746094, Longitude: 4.859991073608).

Is there anything that could account for this?

Strange. Perhaps something happened to the code? :confused:

I don’t think so, here’s the code. It returns the same false results no matter what wifi base station I am near.

script identicaStatzPluginAppDelegate
	property CLLocationManager : class "CLLocationManager"
	property locationManager : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		set locationManager to CLLocationManager's alloc()'s init()
		locationManager's setDelegate_(me)
		locationManager's startUpdatingLocation()
		locationManager's newLocation
		locationManager's stopUpdatingLocation()
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		return my NSTerminateNow
	end applicationShouldTerminate_
	
	on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
		set latitude to newLocation's coordinate's pointValue()'s x
		set longitude to newLocation's coordinate's pointValue()'s y
		do shell script "defaults write Statz-identi.ca location " & latitude & "," & longitude
		quit
	end locationManager_didUpdateToLocation_fromLocation_
	
	on locationManager_didFailWithError_(locationManager, myError)
		log myError
	end locationManager_didFailWithError_
	
end script

try this:

script identicaStatzPluginAppDelegate
   property CLLocationManager : class "CLLocationManager"
   property locationManager : missing value
   
   on applicationWillFinishLaunching_(aNotification)
       set locationManager to CLLocationManager's alloc()'s init()
       locationManager's setDelegate_(me)
       locationManager's startUpdatingLocation()
   end applicationWillFinishLaunching_
   
   on applicationShouldTerminate_(sender)
       return my NSTerminateNow
   end applicationShouldTerminate_
   
   on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
       set latitude to newLocation's coordinate's pointValue()'s x
       set longitude to newLocation's coordinate's pointValue()'s y
       do shell script "defaults write Statz-identi.ca location " & latitude & "," & longitude
       locationManager's stopUpdatingLocation()
       quit
   end locationManager_didUpdateToLocation_fromLocation_
   
   on locationManager_didFailWithError_(locationManager, myError)
       log myError
   end locationManager_didFailWithError_
   
end script

Thanks but no dice. When I log timestamp, it gives me 2010-01-11 13:16:13 +0800. Why does it think it has not logged any better location data since then, when it gives a better more recent location with Javascript?

I found in the Core Location Foundation Reference the desiredAccuracy property, the default value of which is kCLLocationAccuracyBest, which demands the highest level of accuracy. I guess I need to change it to something like kCLLocationAccuracyThreeKilometers which would allow any value within three kilometres.

Not sure how to do this; I tried the following but it didn’t work (“The variable kCLLocationAccuracyThreeKilometers is not defined.”):

set locationManager's desiredAccuracy to kCLLocationAccuracyThreeKilometers

This didn’t work either; it still returned the same invalid location data:

set locationManager's desiredAccuracy to 3000

Try:

locationManager's setDesiredAccuracy_(current application's kCLLocationAccuracyHundredMeters)

[CLLocationManager setDesiredAccuracy:]: unable to set argument 2 because the AppleScript value <NSAppleEventDescriptor: 'obj '{ ‘form’:‘usrp’, ‘want’:‘prop’, ‘seld’:‘utxt’(“kCLLocationAccuracyHundredMeters”), ‘from’:‘null’() }> could not be coerced to type d.

That error suggests you left out "current application’s’ or put kCLLocationAccuracyHundredMeters in quotes. Try it exactly as I posted.

No, I just copied and pasted it, honest! Here is the full code again with the new inclusion:

script identicaStatzPluginAppDelegate
	property CLLocationManager : class "CLLocationManager"
	property locationManager : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		set locationManager to CLLocationManager's alloc()'s init()
		locationManager's setDesiredAccuracy_(current application's kCLLocationAccuracyHundredMeters)
		locationManager's setDelegate_(me)
		locationManager's startUpdatingLocation()
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		return my NSTerminateNow
	end applicationShouldTerminate_
	
	on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
		set latitude to newLocation's coordinate's pointValue()'s x
		set longitude to newLocation's coordinate's pointValue()'s y
		do shell script "defaults write Statz-identi.ca location " & latitude & "," & longitude
		locationManager's stopUpdatingLocation()
		quit
	end locationManager_didUpdateToLocation_fromLocation_
	
	on locationManager_didFailWithError_(locationManager, myError)
		log myError
	end locationManager_didFailWithError_
	
end script

OK, it looks like these enums defined as extern aren’t included as properties of the application.

According to the header file, it’s “used to represent a location accuracy level in meters”. So try this:

locationManager's setDesiredAccuracy_(100)

Well I no longer get that error, but it still returns the same invalid location data from January. I’ve tried also setting the desired accuracy to 3000 or even higher, with no better result. Also, there is one other error that I get in the console, with or without the new code, but I don’t know if this is relevant:

[<CLLocationManager 0x20005dd40> valueForUndefinedKey:]: this class is not key value coding-compliant for the key newLocation.

I don’t get that error with your code. what I get, after 45 seconds of trying, is a failure error:

Error Domain=kCLErrorDomain Code=0 “The operation couldn’t be completed. (kCLErrorDomain error 0.)”

Sorry, you’re right about not getting that error from the code I posted… the error came from some other test code I didn’t post. Mea culpa.

However, I have determined that the invalid location data that I am getting is not due to any bug in the code, since at least two other apps that use Core Location also tell me that I am in the Netherlands (I’m in Malaysia, for the record): Mac WPS and Lucubrator… though that latter at least warns “This location is stale and may be invalid”. No leads yet on how to discard stale locations, though. Will post back if I work it out.