I experimented with the code in the file you linked to and came up with the following as a start:
script LocatorAppDelegate
property parent : class "NSObject"
property CLLocationManager : class "CLLocationManager"
on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
log newLocation
end locationManager_didUpdateToLocation_fromLocation_
on applicationWillFinishLaunching_(aNotification)
set locationManager to CLLocationManager's alloc()'s init()
locationManager's setDelegate_(me)
locationManager's startUpdatingLocation()
log locationManager's |location|()
locationManager's stopUpdatingLocation()
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
You have to add the CoreLocation framework to your project to get this working (Click on the Frameworks triangle in the groups and Files pane of Xcode, then right click on the Other Frameworks folder, choose Add and go down to Existing Frameworks. Choose CoreLocation.framework and click Add).
I can’t really tell whether this is working properly since I’m not near any WiFi networks that would have any location info associated with them. The startUpdatingLocation() method seems to be working, because it opens up a panel asking if you want your program to use your current location. The log of locationManager’s |location| gives me “null” which I would expect given that my computer is not connected to any public WiFi network. I’m not sure what I should be passing in to “locationManager_didUpdateToLocation_fromLocation_”. It isn’t being called in this program, but I’m not sure that it would be if I’m not getting any location info. I’d be interested to know if the log gives you any info if you try this.
I don’t know about that - the iPhone and the computer may be accessing different data to get location. It isn’t clear to me how the device is getting this information. I downloaded the app from the link you posted and added “NSLog(@”%@“,[locationManager location])” to the end of his applicationDidFinishLaunching method, and it also logged “null”(and I didn’t get any results in the program window).
I do think that location is the right thing to log as far as I can tell from the Objective-C. It, and pieces derived from it like location.coordinates.latitude seem to be where the data is coming from. I don’t know if there can be a timing issue – whether it takes some time to get the data. I tried putting a delay in my apple script, but that didn’t help. I’m not sure how to put a delay in Objective-C.
Where in the program did you change locationManager’s |location|() to locationManager’s newLocation? I don’t understand why that would work since newLocation isn’t a Cocoa property, it’s just used as a parameter name in the “locationManager:didUpdateToLocation:fromLocation:” method.
script LocatorAppDelegate
property parent : class "NSObject"
property CLLocationManager : class "CLLocationManager"
on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
log newLocation
end locationManager_didUpdateToLocation_fromLocation_
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)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
It logs the location in the locationManager_didUpdateToLocation_fromLocation_ method. However, I can’t seem to manipulate the data it gets. How do I get it as a string?
I don’t know if you can get the whole thing as a string, but you should be able to get the various parts of the data with things like:
on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
log newLocation's coordinate()'s latitude()
log newLocation's coordinate()'s longitude()
log newLocation's horizontalAccuracy()
end locationManager_didUpdateToLocation_fromLocation_
I’m not sure what the line, “locationManager’s newLocation” in the applicationWillFinishLaunching_ method is doing. If you comment out that line out does your program still work?
I don’t know where to go from here. I think that what I gave you is a good translation from the Objective-C code, but I’m not really sure what kind of objects “coordinate” and “latitude” are. You could try getting rid of the parentheses in coordinate() and/or latitude() and see if that helps. When you log newLocation, what do you get?