How to Determine if Any Text Is Selected in a Chrome Window

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.

Any suggestions?

Thanks.

1 Like

A little JavaScript?

window.getSelection()+'';

-Chris

Thanks Chris,

I don't know where to begin to get KBM to talk to JavaScript to talk to my browser window. Pointers please?

I presume that then I could test to see if the value was empty or not, right?

Thanks.

See the Execute a JavaScript in Front Browser action.

Or the Execute a JavaScript in Google Chrome action.

The former is more versatile and works in Safari, Chrome, Brave, and a couple of others.

Yes.

If you need to perform the JavaScript in anything but the front browser window, you'll need to use AppleScript instead of the Keyboard Maestro action.

-Chris

Thanks Chris,

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:

image

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.

That's sweet.

1 Like

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.

1 Like

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:

if ( window.getSelection()+'' ) {
     window.getSelection()+''
}
else {
      '#'
}

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.

2 Likes

Hey August,

Yes, that's just coercing to string.

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
--------------------------------------------------------

-Chris

Thanks Chris. That looks like it does the trick.

=A=