Execute JavaScript on Chrome Fails No Matter What

Action: just a simple alert("hey"); with or without ; at the end, it doesn't matter the result is the same.

http://cl.ly/image/1D1J2D1S112c
http://cl.ly/image/1d0k1b3l3b3C


{
2023/01/22 15:33 CDT
Images are no longer available – please do not use externally hosted images.

–ccs : forum moderator
}


Hey Leonardo,

I had to use this syntax in Safari:

window.alert(“Hello World!”);

I was able to get this syntax to work:

alert(“Hello World!”);

By running it as pure AppleScript from FastScripts instead of Keyboard Maestro.

So I tried the direct approach from Keyboard Maestro using an Execute an AppleScript action.

------------------------------------------------------------
set jsStr to "alert(\"Hello World!\");"
doJavaScriptInSafari(jsStr)
------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
  try
    tell application "Safari" to do JavaScript javascriptStr in front document
  on error e
    error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
  end try
end doJavaScriptInSafari
------------------------------------------------------------

Okay – I tried it in Chrome, and it works.

------------------------------------------------------------
set jsStr to "alert(\"Hello World!\");"

tell application "Google Chrome"
  tell active tab of front window
    execute javascript jsStr
  end tell
end tell
------------------------------------------------------------

-Chris

Hi Chris!

Thanks as always for being so resourceful. The point I was trying to make is that the Execute Javascript action from Keyboard Maestro fails no matter what.

Cheers,

Leo

Hey Leonardo,

I think Peter will tell us that this is because of the way Keyboard Maestro runs AppleScript (via osascript) – and that it's not a bug – it’s a feature. :smile:

All of Keyboard Maestro's Safari and Chrome commands are AppleScripts.

Keyboard Maestro doesn't play nicely with scripts that create interface elements, however you can generally make them work with some syntax changes to the script.

I think that is at issue in this case, but we'll wait for the definitive answer from Peter.

-Chris

Occasionally the system seems to lose the connection to executing AppleScript via osascript in Google Chrome. My suspicion is that this is related to Chrome’s auto-update mechanism, but I don’t really know. That ends up resulting in all attempts to execute JavaScript (via AppleScript) to Chrome failing.

Restarting Chrome, the Keyboard Maestro Engine, or your Mac may resolve it.

1 Like

It doesn't matter what I put in there, the action just doesn't work at all.

CloudApp
CloudApp

Restarting Chrome, KM Engine or the Mac didn't resolve.

Hey Peter,

I ran this verbatim in Safari with an execute javascript action:

alert("Hey!");

Expecting it to work, but it did NOT. (I figured the Chrome failure was for the reason you mentioned.)

That’s why I went off on the wild goose chase.

The peculiar thing is that it DOES work NOW…

It did NOT work in a newly launched instance of the latest Google Chrome Beta.

But it does NOW…

I cannot explain this.

I’m pretty sure it wasn’t user-error, but I can’t be absolutely certain.

-Chris

Hey Leonardo,

What version of KM? What version of OSX?

How are you triggering your macros?

I used your same syntax and did not throw any errors (OSX 10.11.1 – KM 7.0.1).

-Chris

Chris, I realize you probably know this, but for any lurkers out there, your Chrome AppleScript will fail if you don't already have a Chrome window open.

There is another possibility which is that there is something JavaScript does not like about your variables.

When Keyboard Maestro executes the JavaScript command “// comment” in Chrome, it executes a script that looks like this:


tell application "Google Chrome"
	if it is running then
		tell window 1
			set r to execute active tab javascript "{
 document.kmvar = {};
document.kmvar.Add_Day = \"60\";
document.kmvar.Add_Month = \"0\";
document.kmvar.Add_Year = \"0\";
document.kmvar.Alert_Button = \"Stop\";
-- …
document.kmvar.Yesterday = \"2015-11-22\";
undefined;
}
// comment"
			if r is missing value then
				set r to ""
			end if
		end tell
	else
		set r to "Not Running"
	end if
end tell
r

It is possible AppleScript, JavaScript or Chrome is not happen with your variables or how the document.kmvar dictionary is filled out.

You can see the exact script in the Keyboard Maestro Engine log by entering this command in the Terminal:

defaults write com.stairways.keyboardmaestro.engine Debug JavaScript

and then restarting the Keyboard Maestro Engine and then executing the failing action, and then looking in the Engine.log file (Help ➤ Open Logs Folder). To turn the debugging back off, use the command:

 defaults delete com.stairways.keyboardmaestro.engine Debug 

and restart the engine.

With the AppleScript / JavaScript from the log file, try executing it in Script Editor and see if you get the same result. If so, try deleting all the document.kmvar section and see. If so, then you can narrow it down to which variable is the problem or email me your Keyboard Maestro Variables.plist file (Help ➤ Open preferences Folder) and I will see if I can determine what the problem is and how it can be avoided (and information in your variables will obviously be kept confidential).

Latest of everything, and I trigger my macros via alfred

That script will not work no matter what

CloudApp

My point is that it doesn't matter what you put in this field, it will throw an error. Here's an example with just a space

CloudApp

I've been using javascript actions for years without any problem, and I think I started to notice these errors about 2 months ago. Maybe when I upgraded to el cap, or to km7.

Are you running that from Alfred?

Have you tried running it with a Keyboard Maestro trigger.

I do not see the same error results you do when running the same code with a hotkey trigger.

Unless we can reproduce your problem there's not much likelihood of a fix.

This works fine from the Script Editor.app for me.

set jsStr to "alert(\"Hey!\");"

tell application "Google Chrome"
  if it is running then
    tell active tab of window 1
      set r to execute javascript jsStr
      if r is missing value then
        set r to ""
      end if
    end tell
  else
    set r to "Not Running"
  end if
end tell
r

-Chris

In that case there is something screwed up with your Google Chrome dictionary, or with your AppleScript install (perhaps a bogus AppleScript extension.

Any news on getting the Chrome actions to work again?

Not from me - the issue appears to be AppleScript / Chrome messed up on your Mac. If the script does not work, then until it works there is nothing I can do to make JavaScript actions in Chrome work on your Mac.

The only way to execute JavaScript actions in Chrome is via AppleScript. And if the AppleScript system is failing, none of the actions have any change of working.

Chrome actions are working fine.

Not a KM issue – sounds like there is some other issue with your system. Probably worth looking at Console logs, restarting with a fresh install of Chrome, and standing down all of any Chrome extensions you are using, and then bringing them back in one by one while testing the execution of JS.

( Assuming you do have JS execution enabled in the Chrome advanced preferences )

Chrome actions aren’t fine. Perhaps the code you used below is fine, but everything else isn’t.

I'm using a number of Chrome actions, of varying complexity.

Not having any problems with any of them ...

(and all KM variables can be found and read through Chrome JS Actions, for example)