I would like to do one thing if some text is selected in Chrome and a slightly different thing, using some default text perhaps, if there is no text selected.
I'm making notes about what Chrome tabs are open and I want to save the link with my selected text or default to the tab title.
Is there any way to determine if any text is selected? I would really like to be able to use the same KBM hotkey whether or not text is selected and let the automation determine the difference.
In a thread from about a year ago, How do I express "If something is selected in Finder"?, @Idorigo asked a similar question about something selected in Finder. There the solution was to see if the Cmd+C hotkey was enabled. But that key appears to always be enabled in Chrome.
Since I'd have to have the proper window at the front to select some text, I don't think that particular situation is an issue, but it's good to know about.
I set up the following macro as a test:
When I select some text and press they hotkey (F1 is my testing key) it pops up large. When there is nothing selected, nothing happens.
I don't know yet whether I want to do the testing for content in JavaScript or in KBM, but this is a great start. Thanks. It's also good to know that I don't have set up all kinds of JavaScript environment context, that KBM does all that for me.
While not foolproof¹ what I generally do when I want to know if text is selected is to see if the menu item Edit » Copy is enabled.
Generally there is nothing to copy unless something is selected.
¹ of course you could have, for example, selected the URL in the address bar, and that would enable Edit » Copy so it doesn’t necessarily tell you that it’s something on a web page which is selected.
Thanks for the idea, but I think this is a variation of the issue that I raised in the OP, where many various apps are good about disabling the Command+C key or the Edit > Copy menu item, Google Chrome is apparently not so well behaved.
As I write this, my Chrome Edit menu shows the Copy command fully present, not greyed out, just like every other command in the Edit menu.
I've used Chris' suggestion of using the JavaScript .getSelection() method of the default window object and it's working fine for me. The actual JavaScript code in my working macro is:
I'm not enough of a JavaScript user to have figured out on my own that idiom of concatenating an empty string onto the end of window.getSelection(). My guess is that it forces the result to be a string rather than a NULL or something like that which the if wouldn't like.
I'm guessing, I didn't research or test, I took Chris' sample code and ran with it. On looking at it here, I could probably eliminate the second concatenation and have the true branch of the if just return window.getSelection() without appending the '' to it.
I was fiddling with this not long ago and discovered the native coercion method:
window.getSelection().toString()
Here's how to do it with AppleScript:
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/03/29 22:46
# dMod: 2021/03/29 22:46
# Appl: Google Chrome
# Task: Extract Selected Text from the Front Web Page.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @Selected-Text, @Text
--------------------------------------------------------
set jsCmdStr to "window.getSelection().toString()"
set selectedText to doJavaScriptInChrome(jsCmdStr)
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on doJavaScriptInChrome(jsCmdStr)
try
tell application "Google Chrome" to ¬
tell front window's active tab to execute javascript jsCmdStr
on error errMsg
error "Error in handler doJavaScriptInChrome() of library NLb!" & ¬
return & ¬
return & ¬
errMsg
end try
end doJavaScriptInChrome
--------------------------------------------------------