Comparing dates in trigger script

Posted on
Thu Jan 14, 2016 4:23 am
Topolino offline
Posts: 13
Joined: Nov 30, 2014

Comparing dates in trigger script

Please forgive me if this has been asked and solved before, but I just can't find anything that works.

I need use an applescript in the condition script of a trigger to see if a timestamp in a variable is older than X hours, but I can't convert the timestamp to a date to compare with.

I keep getting "invalid date and time date XXX of "script""

Any help would be greatly appreciated.

Posted on
Thu Jan 14, 2016 10:53 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Comparing dates in trigger script

Your full AppleScript and the value of the variable will be needed to diagnose the error.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jan 14, 2016 11:11 am
Topolino offline
Posts: 13
Joined: Nov 30, 2014

Re: Comparing dates in trigger script

Thanks for replying so quickly!

I eventually found a script that works, but I find it hard to believe that it needs to be this long. Any suggestions for something more efficient would be great.

Code: Select all
set alarm to value of variable "Alarm"
set alarmoff to value of variable "ALARM_OFF"
   
   set myDate to current date
   set now to current date
      
   dateObject from value of variable "TS_LAST_DETECTION" into myDate
   
   if myDate < (now - 2 * hours) then
      set value of variable "log" to "true"
      return true
   else
      set value of variable "log" to "false"
      return false
   end if

to dateObject from theDateString into myDate
   
   set {oti, text item delimiters} to {text item delimiters, " "}
   set {dateString, timeString} to text items of theDateString
   set text item delimiters to ":"
   set {hrs, mins} to text items of timeString
   
   set hrsMins to hrs * hours + mins * minutes
   
   set {yr, Mnth, dy} to words of dateString
   
   set the time of myDate to hrsMins
   set the year of myDate to yr
   set the month of myDate to Mnth
   set the day of myDate to dy
   
   set text item delimiters to oti
end dateObject

Posted on
Thu Jan 14, 2016 2:51 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Comparing dates in trigger script

Yeah, date handling in AppleScript is pretty awful. If it works, I wouldn't mess with it...

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests