Search code examples
javascriptapplescript

AppleScript - do JavaScript returns missing value


The below script is used to click on a button to load a date on our website, then get the value of that date.

It should then return that date value, which will be used in some comparisons later on. However, when I log doJavaStuff and emptyDate, both come back as "(*missing value *)". The display dialog for emptyDate also returns "msng".

Why isn't the result of the do JavaScript being returned?

I've looked at multiple articles and sources and tried multiple suggestions, but nothing has worked. e.g. I tried setting the second line of the do JavaScript to a variable, and returning that variable, but that somehow made the AppleScript variable undefined.

I'm relatively new to AppleScript, so maybe I'm missing something obvious.

global emptyDue, serviceDate, doJavaStuff
set emptyDue to " "
set doJavaStuff to " "
set serviceDate to "2021-11-30"

on isEmptyDue()
    
    tell application "Safari" to tell document 1
        set doJavaStuff to do JavaScript
        "document.getElementById('trgUseNext').click(); #clicks button to load date.
            document.getElementById('inspDate').value;" --gets the value of date
        return doJavaStuff
    end tell
    
end isEmptyDue

set emptyDate to isEmptyDue()
display dialog emptyDate

log doJavaStuff
log emptyDate

Solution

  • I was able to duplicate your problem using an element that happens to be on this StackOverFlow page (post-id), using this simplified version of your script:

    on isEmptyDue()
        tell application "Safari" to tell document 1
            set doJavaStuff to do JavaScript
            "document.getElementById('post-id').value"
            return doJavaStuff
        end tell
    end isEmptyDue
    
    set emptyDate to isEmptyDue()
    display dialog emptyDate
    

    The return value is, as you noted, “msng”.

    The problem appears to be that AppleScript is perfectly happy to have nothing but quoted text on its own line, and is also perfectly happy to run do JavaScript with no script. That is, the code as you’ve presented it doesn’t run any JavaScript: it calls do JavaScript with nothing, and then the next line is a string of text that AppleScript ignores.

    If I combine the lines so that they are one line instead of two, display dialog emptyDate displays the expected 8-digit value of element post-id on this StackOverFlow page (found by viewing the source).

    on isEmptyDue()
        tell application "Safari" to tell document 1
            set doJavaStuff to do JavaScript "document.getElementById('post-id').value"
            return doJavaStuff
        end tell
    end isEmptyDue
    
    set emptyDate to isEmptyDue()
    display dialog emptyDate
    

    That was oddly difficult to track down because neither of the “mistakes” provided an obvious error: do JavaScript without any text is not an error—instead, it returns a cryptic string, “msng”, which I suspect but do not know stands for “missing”—and a string on its own on a line is not an error either. So it looks like it’s doing something and not working, instead of not doing anything and not working.