Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Google Latitude and Applescript

I am trying to get a peice of code working that someone gave out. It's basic point it to return your location via google's latitude service. It is wrote in the form of a handler and the handler seems like it would work. The problem is that in the top of the script file, there is a variable that seems to be set to load the user name and password of google latitude. When i ran the script as a whole it merely said it could not find the file that contained the username and password.



set library to (load script ((“/Library/Application Support/Jarvis/Library.scpt”) as POSIX file))


property myGoogleLatID : (do shell script "defaults read /Library/Preferences/Jarvis googleLatID")


on getGPSLocation()

try

set rawdata to do shell script "curl 'http://www.google.com/latitude/apps/badge/api?user=" & myGoogleLatID & "&type=atom'"

set mylocation to replaceText(space, ",", (first item of xml_parser(rawdata, "georss:point")))

set myLongitude to first word of mylocation

set myLatitude to last word of mylocation

set rawdata to do shell script "curl 'http://maps.google.com/maps/geo?output=xml&key=abcdefg&q=" & mylocation & "'"

set myZipcode to first item of xml_parser(rawdata, "PostalCodeNumber")

set myCountry to first item of xml_parser(rawdata, "CountryName")

set myState to first item of xml_parser(rawdata, "AdministrativeAreaName")

set myTown to first item of xml_parser(rawdata, "LocalityName")

set myStreet to first item of xml_parser(rawdata, "ThoroughfareName")

set mylocation to {street:myStreet, city:myTown, state:myState, zipcode:myZipcode, country:myCountry, longitude:myLongitude, latitude:myLatitude}

on error themessage

set mylocation to {street:"n/a", city:"n/a", state:"n/a", zipcode:"n/a", country:"n/a", longitude:"n/a", latitude:"n/a"}

return themessage

end try

return mylocation

end getGPSLocation



within my library file, i also have the handlers for replaceText() and the xml_parser(). I really just need to know how to create a file, the googleLatID, that this handler will be able to read the username and password to. There are many of these types of files thta would basically load my user name and password for various different accounts and website so that my handlers can work with those website.

Mac OS X (10.6.8)

Posted on Jun 10, 2012 6:54 AM

Reply
7 replies

Jun 10, 2012 9:48 AM in response to -Arch-

It's not clear quite what you're asking...


I think this is the main issue you're trying to address:


The problem is that in the top of the script file, there is a variable that seems to be set to load the user name and password of google latitude


which maps to this line, right:


property myGoogleLatID : (do shell script "defaults read /Library/Preferences/Jarvis googleLatID")


Since you're using 'defaults', that means the file in question has to be a valid .plist file. Is it a plist? or is it a simple text file?

In addition, the filename includes a space, so you'd need to escape that in order for it to work:


property myGoogleLatID : (do shell script "defaults read /Library/Preferences/Jarvis\\ googleLatID")


Additionally, you're storing the preferences as a property. This means it is evaluated at compile time, not run time. Therefore the script will always store the value that was present when the script was compiled. If you transfer the script to another user's system, or your user credentials change, the script will fail.


Finally, for now, you're not actually using your library script in any way.


So a little clean-up is needed, I think.

Jun 10, 2012 10:08 AM in response to Camelot

Since you're using 'defaults', that means the file in question has to be a valid .plist file. Is it a plist? or is it a simple text file?


There is no file. This is my first problem. I dont know how to create a file that this this script, or other scripts like this will be able to read.


Storing the perferences as a property shouldnt be an issue once I get this to work on my computer right? It just stops it from working right now since I do not have this script set up on my computer.


That was my mistake about the library script. That is the peice of script that I use to load my library script so I can access all the hanlders I have stored in it.

Jun 10, 2012 2:22 PM in response to -Arch-

There is no file. This is my first problem. I dont know how to create a file that this this script, or other scripts like this will be able to read.


If you want to use defaults to read the file then the easiest thing to do is to use default to write (or create) the file, too. For example, to create the preference file with a key 'googleLatID' and a value 'abc123', you could:


do shell script "defaults write /Library/Preferences/Jarvis.plist googleLatID abcd123 " with administrator privileges

(note that I've added .plist to the file name to retain some correlation with the guidelines)

You can repeat this line as much as you like to write additional key/value pairs to the preference file, any of which can be read back using 'defaults read...'


Storing the perferences as a property shouldnt be an issue once I get this to work on my computer right? It just stops it from working right now since I do not have this script set up on my computer.


It shouldn't be an issue, but if you are expecting to distribute this file it would be better to load the values at runtime rather than at compile time. One reason being that if anyone gets a hold of this script they will automatically have access to your Google Latitude account credentials.


That was my mistake about the library script. That is the peice of script that I use to load my library script so I can access all the hanlders I have stored in it


OK. As long as you realize it doesn't do anything right now, I guess you're OK. 🙂 I just wanted to make sure you realized that.

Jun 10, 2012 3:26 PM in response to Camelot

If you want to use defaults to read the file then the easiest thing to do is to use default to write (or create) the file, too. For example, to create the preference file with a key 'googleLatID' and a value 'abc123', you could:


do shell script "defaults write /Library/Preferences/Jarvis.plist googleLatID abcd123 " withadministrator privileges

(note that I've added .plist to the file name to retain some correlation with the guidelines)

You can repeat this line as much as you like to write additional key/value pairs to the preference file, any of which can be read back using 'defaults read...'

So, would google latittude know that the abcd123 is my username? This is the part that really confused me. I believe the way the code is designed is to set googLatID to my username and password and then when I call the GetGPSLocation(), the first thing it does it loads the website and my username and password, right?


Also, there are sections of code that call the xml_parser(). Will this new line work with that handler? Here it is for reference.



on xml_processor(rawdata, theItem, tags)

set rawtext to rawdata

set myoutput to {}

set myItems to xml_parser(rawtext, theItem)

repeat with x from 1 to count of myItems

set temp to {}

repeat with z from 1 to count of tags

set temp to temp & xml_parser(itemx of myItems, itemz of tags)

end repeat

set myoutput to myoutput & {temp}

end repeat

return myoutput

end xml_processor

on xml_parser(rawtext, theID)

set myoutput to {}

repeat

try

if rawtext contains ("<" & theID & ">") then

set myoutput to myoutput & {text ((offset of ("<" & theID & ">") in rawtext) + 2 + (count of characters of theID)) thru ((offset of ("</" & theID & ">") in rawtext) - 1) of rawtext}

set rawtext to text ((offset of ("</" & first word of theID & ">") in rawtext) + 2 + (count of characters in theID)) thru -1 of rawtext

else

exit repeat

end if

on error

exit repeat

end try

end repeat

return myoutput

end xml_parser

Jun 10, 2012 8:50 PM in response to -Arch-

So, would google latittude know that the abcd123 is my username?


At the highest level, Google Latitude has no idea who you are, what your username is or what your password is. It's up to your script to determine that (e.g. by reading the preference file) and provide it to Google (e.g. by incorporating that data into your script.


Ultimately, the .plist accessed by defaults is a structured file of key/value pairs - you can write any key (e.g. 'username') and its corresponding value (e.g. 'joeuser') to the file using 'defaults write', and retrieve the values via 'defaults read'.


For example, to write the username 'joeuser' and password 'abc123' values to the file you might use something like:


do shell script "defaults write /Library/Preferences/Jarvis username joeuser " with administrator privileges

do shell script "defaults write /Library/Preferences/Jarvis pass abc123 " with administrator privileges

Then, later you can use defaults read to obtain the current values and store them in AppleScript variables, like:


set myUser to do shell script "defaults read /Library/Preferences/Jarvis username"

set myPW to do shell script "defaults read /Library/Preferences/Jarvis pass"


Now you can see how to write data to a preference file, and read it back in later.


Since you now have the data in the AppleScript variables, you can incorporate that into the rest of the script.


Note that this contrasts with your original script where you read the entire preference file data into a variable, but that's not going to work (at least not directly) since Google have no knowledge (or interest) in all the .plist data, just the username and password elements.

Unfortunately I don't know the Google Latitude APIs to know how to present this data to Google. Your original code won't work as written, but I can't precisely tell you what needs to change without seeing a working example API call.

Jun 11, 2012 12:14 AM in response to -Arch-

-Arch- wrote:


So, would google latittude know that the abcd123 is my username? This is the part that really confused me. I believe the way the code is designed is to set googLatID to my username and password and then when I call the GetGPSLocation(), the first thing it does it loads the website and my username and password, right?


Have you signed into Google Latitude? I'm curious whether signing in creates a plist file with the proper name and form.


t would be useful to know where you got this script, too, in case there's some documentation or examples.

Jun 11, 2012 10:49 AM in response to twtwtw

Have you signed into Google Latitude? I'm curious whether signing in creates a plist file with the proper name and form.


Google Latitude, like most Google services, is just a web app, accessed via a URL - note the 'do shell script "curl..."' in the original post, so there's no chance that signing in is going to create a corresponding .plist - curl doesn't know to do that.

Even if the OP signs in via a typical browser, his authentication would likely be validated via cookies, and there's no trivial way to transfer a session cookie from a browser to curl.


So I think the OP has the right approach - store the username and password somewhere (although I might like it in some kind of encrypted form), and load it at runtime to make the call to Google.

Google Latitude and Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.