I need a script that will perform a task if the time is later than 8pm. My date coercion is not working. Can someone help with the following script:
set currentTime to the time string of (current date)
if currentTime is greater than “8:00:00 PM” then
try
do something here
end try
end if
I found my answer here: http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.2d.html#34922
set currentTime to the time of (current date)
if currentTime is greater than “72000” then – 8:00:00 PM in seconds since midnight of the date value
try
do something here
end try
end if
Hi, Rob.
More properly, you should be comparing the time with the number 72000 (without quotes), not the string “72000”. The string works in your case because it’s automatically coerced to number when the script runs ” and that in turn is because the variable to the left of ‘is greater than’ contains a number. But there’s no point in making the script do a coercion if it doesn’t have to.
If the comparison had been the other way round, with the string to the left of the operator and a number to the right, the number would have been coerced to string instead and the comparison would have been lexical:
"72000" is less than 7300 -- 7300 is coerced to "7300".
--> true