Execute JS in Safari - Copy Text Selection

Hi ,
I am playing with Execute JS in safari action and wanted to copy the current selection in safari to clipboard , assuming I am copying Text only. This code below by chatGPT fails to give any result

  1. Developer Mode On
  2. KM can control safari
  3. Edited after @griffman reply - Cmd+C - will work , just trying to figure JS here
(function() {
    document.execCommand('copy');
    return window.getSelection().toString();
})();

This may be a stupid question, but if you want to copy the current selection, wouldn't sending Command-C to the browser do just that?

-rob.

You are right , like I said , I am just trying to understand, why the above javascript didn't work. CMD+C will work absolutely fine too.

Sorry; misunderstood that you were just working with the Javascript action. I am far from a JS expert, but I think the problem here is that the function isn't in Modern Syntax form. Uncheck this box, and it works:

image

-rob.

1 Like

Whereas with modern syntax all you need is:

return window.getSelection().toString();


(Not sure what the intention of the .execCommand invocation was)


Safari selected text.kmmacros (1.9 KB)

1 Like