I have declared global variables at the top of the script, and I have initialised them.
I can set the "do JavaScript" to the variable "emptyDue" fine. But then as soon as I try to compare the result of the JavaScript to the serviceDate variable, I get: "error "The variable emptyDue is not defined." number -2753 from "emptyDue"".
Why can I assign emptyDue on one line, and then on the very next line, it suddenly forgets that it should exist?
global emptyDue, serviceDate
set emptyDue to ""
set serviceDate to "2021-11-30"
on isEmptyDue()
tell application "Safari" to tell document 1
set emptyDue to do JavaScript "document.getElementById('trgUseNext').click();
document.getElementById('trgUseNext').value; --gets the date value from a webpage."
if emptyDue < serviceDate then
display dialog "TRUE"
else
display dialog "FALSE"
end if
end tell
end isEmptyDue
isEmptyDue()
log emptyDue
There is a known flaw in AppleScript’s design: it is possible for a command to return no value. If you then bind the result of that command to a variable, it effectively makes that variable undefined.
You can avoid that -2753 error by making sure your command always returns a value, even if that value is just an empty string.