"Execute JavaScript in Safari" Execution Error: "The variable r is not defined"


I started receiving this error a few weeks ago, while running a macro that interacts with a web page via the Execute JavaScript in Safari action.

document.getElementsByClassName("editSaveButton medium")[0].click();

/var/folders/wc/ymvh1gv88xn895006s6vz9br1111aa/T/Keyboard-Maestro-Script-F7B85985-210F-477F-19D3-E5ABCAC615BC:10434:10385: execution error: The variable r is not defined. (-2753)

Stuff I’ve tried:

  • Restarting the Keyboard Maestro Engine.
  • Executing the macro on a different page.
  • Running the JS directly in Safari’s Console. RESULT: It works fine and selects the corresponding element.

As far as I know, there is no “variable r”. At least not one that I’ve defined.

I may or may not have restarted the computer since this started. Do you think that could do it?

KM: 7.3.1
OS: 10.11.6


Unfortunately, a change to Safari’s AppleScript support results in any error from the JavaScript giving that AppleScript error in Keyboard Maestro.

Try running the command in the Safari console to see what the actual error is.

Do you mean the JS:

document.getElementsByClassName("editSaveButton medium")[0].click();

When I run it in Safari’s Console it executes without error.

In that case, try changing the script in Keyboard Maestro to:

document.getElementsByClassName("editSaveButton medium")[0].click();

or possibly:

document.getElementsByClassName("editSaveButton medium")[0].click();
return "GOOD";

The issue may also apply if the script returns void.


It executes fine directly in Safari.

This is the error that’s generated, in Safari, when run from KM:

TypeError: undefined is not an object (evaluating 'document.getElementsByClassName("editSaveButton medium")[0].click')

Your other idea doesn’t run in either Safari or KM, because a return can only be run from within a function. Which makes sense.

It sounds like document.getElementsByClassName("editSaveButton medium") is not finding the item. What does it return if run from Keyboard Maestro?

Maybe. Except:

The button is clicked and the page does what it was going to do as a result of a clicked button.

The macro (and all of the other actions) seems to work fine. I just get the error (see: original post) from KM.

The error is caused by a bug in Safari, such that the do javascript command, when the javascript does not return anything, returns something that is so nothing that it removes the resulting variable from AppleScript’s mind.

Basically, the AppleScript that is executed looks like this:

set r to do javascript …
if r is missing value then
    set r to ""
end if

And the result from Safari is such that the second like complains “The variable r is not defined”. Which is madness, since it is clearly defined on the line before.

Such is AppleScript unfortunately.

I will look at working around the Safari bug (which has also been reported to Apple).

Quote of the day.

[quote=“peternlewis, post:8, topic:7700”]
And the result from Safari is such that the second like complains “The variable r is not defined”. Which is madness, since it is clearly defined on the line before.[/quote]

I think I understand

That’s great, thanks.

Knowing that’s it’s basically nothing, and the macro is likely otherwise working fine, is good enough. I can deal with the error window for now :slight_smile: