AppleScript conditional statement rules
I need a script to run between 9pm and 6am and I'm struggling to understand why this isn't working. I've tried with and without parentheses.
From the suggestions here, I removed "as string" but then got an error when I tried to run the app.
I figured out the error came from another line in the code that I didn't include in the example.
set myTimeB to (round ((time of date ((current date) as string)) / 60) rounding down) mod 5 = 0 --every 5 minutes
Any idea why this needs "as string" to function?
set myTime to time of (current date) --as string
if (myTime > 21600) and (myTime < 75600) then
display notification "it's between 6am and 9pm"
else
display notification "it's between 9pm and 6am"
end if
Update to address totally changed scope of original question:
The issue as I see is that of date
and as string
in ((time of date ((current date) as string)) / 60)
do not belong there. It should just be:
set myTimeB to (round ((time of (current date)) / 60) rounding down) mod 5 = 0 --every 5 minutes`
-
(time of (current date)
returns an integer, so it's all set to be divided by60
. There is no need to be coercing it to a string. -
of date
makes no sense in the equation.
Original answer:
The following works for me:
set myTime to time of (current date)
if (myTime > 21600) and (myTime < 75600) then
display notification "it's between 6am and 9pm"
else
display notification "it's between 9pm and 6am"
end if
Note that originally, myTime
was a string and you were comparing it against an integer. Now it's being compared against the same class.
If you run the following in Script Editor:
return class of time of (current date) & class of 21600
It returns: {integer, integer}
While the result of:
set myTime to time of (current date) as string
return (class of myTime) & class of 21600
It returns: {text, integer}