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
------------------------------------------------------------
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.
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.
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.
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:
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:
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).
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
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.
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
In that case there is something screwed up with your Google Chrome dictionary, or with your AppleScript install (perhaps a bogus AppleScript extension.
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.
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 )