I wrote an applescript studio app, and I want to make it expire after one year of use. Problem is, I need to get the current date every time it’s launched from a network time server (in case the user is trying to be tricky by changing their computer date). I’ve been Googling for about 2.5 hours with NO LUCK!!! Is there any way to do this?!?
That second url can be used to extract the time from the html code. Note: I’m not sure how reliable this will be but it works now and I’m posting this script to show you how you can use text item delimiters to extract information from html code. It’s up to you to decide how reliable this solution is. Personally I wouldn’t worry about people changing their clock. You can perform multiple checks of the time while your application is running to ensure the time. I don’t believe most users will keep their clock set to a wrong date for long periods of time… and I wouldn’t worry about the users that will.
set theUrl to "http://www.worldtimeserver.com/current_time_in_US-NY.aspx"
set theHtml to do shell script "curl " & quoted form of theUrl
set theTime to extractTheTime(theHtml)
set theDate to extractTheDate(theHtml)
set fullDate to date (theDate & space & theTime)
on extractTheTime(theHtml)
set text item delimiters to "<div id=\"analog-digital\">"
set a to text item 2 of theHtml
set text item delimiters to ":"
set b to text items of a
set thehour to text -2 thru -1 of (item 1 of b)
set theMinutes to text 1 thru 5 of (item 2 of b)
set text item delimiters to ""
return thehour & ":" & theMinutes
end extractTheTime
on extractTheDate(theHtml)
set text item delimiters to "onclick=\"switchToAnalog();\""
set a to text item 2 of theHtml
set text item delimiters to return
set theDate to item 3 of (text items of a)
set text item delimiters to ""
repeat while theDate begins with space -- remove leading spaces
set theDate to text 2 thru -1 of theDate
end repeat
return theDate
end extractTheDate
Yeah, getting the time from a webpage would seem the most straightforward solution. I’m looking for something a little more elegant though, and would probably only use that as a last resort.
I had a bit of luck with the following:
do shell script "ntpdate -q time.apple.com"
Which returns about 6 or 7 lines, including the difference between the time server and the computer time in seconds, but I couldn’t find a way to use this output in my script. Anyone know if there’s a way to format ntpdate it into something useful?
Well I figured it out. Sometimes you just need to ask somebody how to do it before the answer comes to you. Here you go:
Magic script:
set theDate to do shell script "ntpdate -q 69.25.96.13"
set theStart to (offset of "offset" in theDate) + 7
set theEnd to (offset of "delay" in theDate) - 3
set theOffset to characters theStart thru theEnd in theDate
set theOffset to (theOffset as string) / 60
set theDate to (current date) + (theOffset * minutes)
And then you have the actual time, regardless of what the computer’s clock is set to!
That’s sweet. I didn’t know the ntpdate shell command.
A comment about your script… this line returns a list of characters which then has to be coerced to a string in your next line of code.
set theOffset to characters theStart thru theEnd in theDate
set theOffset to (theOffset as string)
You can get that as “text” instead of as “characters” and then you can get the string directly.
set theOffset to text theStart thru theEnd of theDate
Also, you then divide that by 60 and multiply by minutes… when you can just add the offset instead. So I would change your script to the following, but of course it’s just a minor thing.
set theDate to do shell script "ntpdate -q 69.25.96.13"
set theStart to (offset of "offset" in theDate) + 7
set theEnd to (offset of "delay" in theDate) - 3
set theOffset to (text theStart thru theEnd of theDate) as number
set currentDate to (get current date) + theOffset