How to copy text from a web page in Safari

I’m writing a macro to select quotes from the web and create a markdown link of the form
"quote" in (webpage title)[webpage url]
which i can then paste in my note taking application (tinderbox)

I have a handle on each element:

  • get the quote through ⌘C
  • get the url through ⌘L ⌘C
  • get the title through Set Variable ‘title’ to text '%WindowName%1%
  • then output to the clipboard with the formatting I want.

Unfortunately, my first ⌘C does NOT get the selected text in the clipboard. The clipboard stays populated with whatever was in it before I triggered the macro. So if I type in ⌘C on my own and then call the macro, things work.

I’ve tried “activating Safari” (even though I’m already in Safari) but it doesn’t work …
Any ideas?

⌘C should work as expected. The only reason I can think of that it wouldn’t is that maybe Safari (or the window you want to target) is not in front when you run the macro. Try to run the “Activate a Specific Application” before the copy to rule that out. Or try using the “Select Menu Item” action instead of the keystroke to do the copy. You’d be able to see if copying is disabled for some reason.

Safari’s very much in front, and I’ve used the ‘Activate Specific
Application’ as well.
I’ll try the select menu item piece …
What throws me off is that if I do ⌘C by hand just before activating
the macro, it works …

I just tried the keystroke (even though I was already sure it worked) and there’s no problem here. If you still can’t get it to work, maybe post your KM and OS versions.

Hey There,

Do not do this by brute-forcing the UI when there are better ways. Keyboard Maestro has a number of functions for working with Safari and Google Chrome.

Look under Safari Control in Actions. Also look in the Insert Token menu.

Ordinarily I'd do this sort of thing with AppleScript, because I've been doing it that way for many years - and it's easier for me.

tell application "Safari"
  tell front document
    set _title to do JavaScript "document.title"
    set _url to its URL
    set selectedText to do JavaScript "\"\"+window.getSelection();"
  end tell
end tell
set AppleScript's text item delimiters to {linefeed}
set _text to {_title, _url, return, selectedText} as text

From there I have all kinds of tools for parsing the text.

On the other hand this kind of job has become much easier to do in KM since the advent of version 6:

Best Regards,

[Now, let's see if this critter gets sent correctly via email.]

1 Like

Check the Keyboard Maestro Clipboard History to verify this. My guess is that it is being copied, but all the other clipboard actions are confusing the issue.

As @ccstone says, the much better solution to to use the Safari tokens to read the URL and page title, and the either read the selection via the Safari JavaScript token or use the clipboard to copy just that which will likely avoid any issues you are seeing.

Thanks to all for the tips on how to do this in a less “brute force way”. It’ll definitely be useful … once I get the copy selection to work.


  • the clipboard history switcher shows me that nothing is actually copied in the clipboard
  • worse, the javascript bit returns NOTHING, as shown in the following screenshot:

I’ve disabled ALL of the KM Macros I have and I’ve disabled all “keylogger” style apps I use (e.g. textexpander, BetterTouchTool, Karabiner …) . Javascript is NOT disabled in the “develop” menu of Safari …

I’m stumped. Cmd-C works perfectly “by hand” but it seems to be invisible to KM, even if it can find the Safari title and url …

Does it happen on a specific webpage only, or everywhere?

Perhaps you should show you actual macro, because all of these worked fine for me on the same web site you reference.

For some reason, the 3 versions posted by Peter work - and I can’t figure out what I did wrong with mine.

For now, I’m assuming it’s an error between the keyboard and the chair, so I won’t waste any more of people’s time on this. Thanks all for the suggestions - which are much less clunky than anything I could come up with anyway.

Here’s what I ended up doing:

This is an old post, but I was reproducing the script that Chris had shown and was having problems with the SafariJavaScript line. When I would type it, the straight quotes would get autocorrected to curly quotes. As curly quotes, the line wouldn’t work. When I copied and pasted straight quotes in, it worked.

Sounds like Smart Quotes is turned on.

Keyboard Maestro should be turning Smart Quotes off for the execution fields, but its possible to turn it back on, and some versions of OS X/Xcode/xib have not correctly honoured the "turn Smart Quotes off" field.