XML and time help.

I am making a Applescript bridge to connect a web service to applescript.
While sending and receiving data, via I get something like this:

I want to do two things:

  1. Get the value of the XML tag “remaining” and “resetdate” via System Event’s XML parser
    and
  2. Convert the value of XML tag “resetdate” to the actual readable date.

Can anyone help me with that? I tried the XML parser for this, but it just didn’t agree with my knowledge.
I don’t know where to go with “resetdate.”

The rest of my script is doing fine, except the cURL parts, and it’ll be posted on the Code Exchange soon.

Hi Dylan,

You can use a small snippet of Perl code to easily convert unix time to a human readable format. As Perl is pre-installed on all modern Macs, it’s safe to use:


on run
	-- unixtime
	set uts to "1293940068"
	set datestr to my getdatestr(uts)
	display dialog datestr
end run

on getdatestr(uts)
	set snippet to "perl -e 'print scalar(gmtime(" & uts & ")), \"\\n\"'"
	set datestr to do shell script snippet
	return datestr
end getdatestr

Best regards,

Martin

Hi Dylan,

I convinced the System Events app to parse your XML using the following code (I saved your XML to a file):


set xmlfilepath to "/Users/martin/Desktop/test.xml"

tell application "System Events"
	tell contents of XML file xmlfilepath
		tell XML element 1
			tell XML element "success"
				set resetdate to value of XML attribute "resetdate"
				set remaining to value of XML attribute "remaining"
			end tell
		end tell
	end tell
end tell

Thanks Martin, but I have one problem.
I need to make the script save the XML data to a XML file, but my problem is that it isn’t saving and isn’t reading the file when it’s saved manually. I looked up on it, but all the websites said is that Applescript doesn’t work well with saving XML data to .xml files. Do you have any solutions?

I found my problem by having cURL output it’s data to a file, and having Applescript reading it. Thank you, everything now works. I’m going to work on everything else based on the new data extracted from the XML file. :slight_smile: