Maybe you’re not using el cap or km7 then, because it definitely doesn’t work and it doesn’t work even when you try to use with applescript.
Try executing this AppleScript
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
No problem at all here. Works fine.
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
Let me update my el cap to 10.1.2. What chrome version are you on?
Good luck !
The point is that this is not a Keyboard Maestro issue.
It is a Chrome / AppleScript issue.
And it is not an issue for all users, it is an issue for some users that Chrome's AppleScript support breaks.
This is not something Keyboard Maestro can "work around" - Keyboard Maestro can't do any more than you can in Script Editor. If you can't get the scripts working in Script Editor, there is no hope of Keyboard Maestro being able to do anything.
This is an issue that has plagued Chrome for years - multiple OS X versions and many Chrome versions.
It is likely that it is some sort of corruption in a cache related to AppleScript or Launch Services or a similar system. My guess remains that the corruption happens during an automatic update of Chrome.
Whether it is possible to get anyone from Google Chrome to look in to the problem, I have no idea. But until it is resolved on your Mac, Keyboard Maestro's Chrome actions can't possibly work.
The usual things can be tried:
- Restart.
- Option ➤ Delete Immediately Chrome, and reinstall it.
- Reset your Launch Services cache.
- Create a fresh user account and see if it has the problem as well.
- Clean re-install.
There is no guarantee any of it will solve the problem, since no one has any idea what causes the problem in the first place or even what the problem really is that causes the AppleScripts to fail.
No worries Peter, thanks for looking at it anyway. Hopefully either apple or google fix it within the next weeks
I wouldn’t advise waiting on Google or Apple to fix it - this problem has been around for years. Without something done on your part, it is likely not going to resolve itself.
Either that or start using Safari for these sorts of tasks since the Safari actions work. Or alternatively, solve them with straight UI actions like clicking and tabbing and typing.
@thankful_dude: I think you are consistently missing the point.
The issue is NOT with KM, or El Capitan, or Chrome but with your environment.
A number of other users are working fine with KM, El Capitan, and Chrome.
If you want to resolve your issue, then you need to look internally.
As has been said for many years: "Doctor, heal thyself"
Good luck.
So I did some more research and it’s definitely an issue with Chrome, however they don’t intent to fix it
https://code.google.com/p/chromium/issues/detail?id=543437
There’s even a user with exactly the same problem I’m having and he can no longer run his Chrome actions. As it turns out it’s nothing related to Keyboard Maestro.
Unfortunately I stumbled upon the same behaviour. We have some Alfred workflows that interact with Chrome by running an Applescript that executes Javascript in a Chrome tab. The executed JS now seems to be sandboxed and can’t access any of the non-default globals.
This worked before, but I have no idea when it changed exactly. It’s kind of a pain, as many of our workflows really speed up our work process, some of which recently just stopped working.
I would have moved to Safari a long time ago if it wasn’t for the fact that it doesn’t have favicons…
Thanks for the help anyway, everyone.
This is not the same issue.
Or more accurately, there are two different issues. This one is why you cannot access jQuery or other JavaScript added facilities in Chrome any more. This is some sort of "security" feature, which is really not any kind of security since as the thread describes you can just use the javascript: URL instead, except more painfully. This issue affects everyone with a recent version of Chrome when trying to execute JavaScript via AppleScript that includes access to page-added scripting facilities like jQuery. It only affects the Execute JavaScript in Google Chrome action, and only then if you are expecting scripting stuff provided by the page to be available to your script.
This is not the same as the issue some folks experience on their individual Macs which results in a complete inability to execute JavaScript via AppleScript in Chrome, regardless of what the JavaScript does. This afflicts only a small number of people, and renders the Keyboard Maestro Chrome actions inoperative.
Good point. Issue #2
which results in a complete inability to execute JavaScript via AppleScript in Chrome, regardless of what the JavaScript does
is resolved by doing what you suggested and completely uninstalling Chrome, including user profiles, caches etc, then installing everything back again. Thanks for that tip!
Using Safari until issue #1 is resolved, that is if it's ever going to be resolve as it seems it was deliberate.
By default, Google Chrome executing JavaScript through AppleScript is turned off. To turn it on, from the menu bar, go to View > Developer > Allow JavaScript from Apple Events. For more information: https://support.google.com/chrome/?p=applescript
Yes! Thank you! The only real answer here!
4 years later and and you can count one more unfortunate wretch in. I started using Chromium and ran into the same incapacity to execute JavaScript whereas it's buttery smooth in this browser's console as well as in that of every Mac-compatible browser. Other Chromium's AppleScript commands work more or less coherently.
However, Safari is no better and has been so over the last 6 years, starting with Safari 9, if not earlier. Running JS code as basic as document.getElementsByClass(\"_class_\")
via AppleScript would return missing value. As of Safari 12, 13 and 14 it's nothing, 0 symbols. In Chromium it's always {}
. Go figure.
Not feasible, alas, until you post a sample macro:
- describing what problem you aimed to solve with it,
- and also describing what you saw and why that surprised you.
Tell never works – if you want any help, you will only get it by moving straight past tell to show.
If you want help with this please post a couple of test macros – one for Safari and one for Chromium – that demonstrate your problems.
Please also provide specific web pages to test them on.
You are aware that JavaScript from AppleEvents must be manually turned ON for both browsers, before JS via AppleScript can operate – yes?
Replying to both last posters: I didn't ask for help, nor did I expect outsiders to lecture me about the best ways of framing my remarks. Also, assuming other individuals to be less informed is poor practice and doesn't conform to the mindset of good faith, the more so because the users of such elaborate automation tools as Keyboard Maestro likely have passed the threshold of the basic knowledge thereof.
I see these actions falling with the message
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/QXPScriptingAdditions.osax" cannot be used with the current OS because it has no OSAXHandlers entry in its Info.plist.
for Safari, and
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/QXPScriptingAdditions.osax" cannot be used with the current OS because it has no OSAXHandlers entry in its Info.plist. /var/folders/pr/34n7_cwx0_1bz_k7xcy1hjyc0000gn/T/Keyboard-Maestro-Script-7B78C662-58BB-4E88-871D-CCE41B5573D3:94:100: script error: Expected end of line but found identifier. (-2741)
for Chromium.
Additionally running the macro on Chromium won't show the result in a window despite being explicitly told to, unless clicked on "Try" in the body of the JS action which is the output pasted.
This misbehaviour reflects the inability of executing Javascript via AppleScript in each of the browsers.
Run Javascript in Safari.kmmacros (2.5 KB)
The icing on the cake is that none of KM Chrome's actions talks to Chromium.
Because you still have an old QuarkXpress scripting addition installed. Find it, bin it, and that particular error will go away.
It probably won't make a difference to the actual problem, but it will make troubleshooting easier if it isn't there cluttering your output with error messages.
You expected to see a result displayed in the window ?
An element isn't a string value, so KM Display Results
is not expected to show anything there. There's no string to show.
If I expand the expression to something which does actually return a string:
document.getElementById("ac-gn-segmentbar").localName
then it works absolutely fine here.