Help in Generating Link to Text Fragment of Web Page, Copying It to Clipboard, but Pasting in Rich Text

Hi All

I'm new to keyboard maestro and have usually used it for simple macros or custom shortcuts.

Recently, I found this chrome extension which allows you to link to a text fragment on a web page-- Link to Text Fragment + Long article about this here -- which I think would be ridiculously useful for my research needs if I can find a way to paste in the text fragment itself instead of the html code which is what it does at present

So to give more detail, in the github repo for the extension where I raised the same question, a developer sums up what the extension does right now as follows:

Say you select text fragment "worthwhile thing" on https://example.com, right-click and choose "copy link to text". Currently (prior to @tomayac's change), this would copy https://example.com#:~:text=worthwhile%20thing to the clipboard; when loaded, it highlights "worthwhile thing" on the page.

In order for it to do what I'd like, this is what they'll have to do to the extension

The change above means that now we'll also copy some additional data so clients that support HTML paste (e.g. Google Docs, Microsoft Word) will directly paste a link to https://example.com#:~:text=worthwhile%20thing but whose text is "worthwhile thing".

Rather than modify the extension, is there something that Keyboard maestro could do for the second part?

Thanks!

Hey @zendude,

I'm not quite clear on what you want. It sounds like:

⠀⠀A) You want to copy and paste RTF from the web.
⠀⠀B) You want to retain the link to said text.


  1. What web browser are you using?
  2. Where are you pasting your rich text note?
  3. What version of macOS are you using?

-Chris

Hey @ccstone sorry for that. It seems that the terms to express what I'm aiming for isn't easy to come by. Just in case I miss again, please also take a moment to look at the repository for this extension where you'll find me asking the same question (it took awhile, but i think they got it)

So to respond to your request for clarification

I want to be able copy the "underlying" link to a selected text fragment and paste it anywhere that supports rich text as a rich text link

So for instance... I used the "link to text fragment" extension right now to copy a link to the first line of your reply to me "Im not quite clear on what you want." The extension however generated a link which when pasted in (not keyboard maestro forum) looks like this:

https://forum.keyboardmaestro.com/t/help-in-generating-link-to-text-fragment-of-web-page-copying-it-to-clipboard-but-pasting-in-rich-text/21649#post_2:~:text=I'm%20not%20quite%20clear%20on%20what,%E2%A0%80%E2%A0%80A

If you click on this link... it will take you directly to the first line of your response

What I want to do is maintain everything BUT the text displayed.

So although the underlying link is the above, I want the pasted text to be identical to the "surface" text I copied... In other words, when copying "I'm not quite clear..." I want the pasted text to read "I'm not quite clear...

As per your other questions

Chrome and Vivaldi - the extension works on both

Web apps that support rich text links like Workflowy

Catalina 10.15.17

Thanks Chris!

Okay, it's doable.

You realize that your text reference blob will be entirely underlined as a link – yes?

It seems to me you'd be better off pasting the text and appending a link to the reference. Like so:

My pasted reference text. [ref]

-Chris

I had to Command-click on the [ref] here in the forum to get the url to work.

-Chris

Yes I'm aware of that. But since 7/10 times I normally paste the text blob into my note taking app and do my notetaking as a nested bullet underneath, I felt it's better to read through a blob of text as opposed to taking the risk of clickiing on the link and then finding myself sucked into this vortex called the internet

This was actually going to be my second request!--- which should help for the remaining 3/10 times when I just need some kind of ref pin

All in all -- accomplishing something like this in tandem with the existing deep linking capabilities I've recently discovered for ebooks (calibre) and PDFs (via Hook) means it's possible at last to link to something and have that link take me directly to the linked section in a page

So do you want rich text or would flat text - as e.g. innerHTML would give you - do?

Hey @zendude,

Okay, here's something to get you started. It will produce a text citation with a RTF reference link at the end.

  • I can't get it working in Vivaldi, because the keyboard shortcut won't work.
    ⠀⠀- Someone needs to report a bug to the Vivaldi people.

  • The keyboard shortcut is finicky in Chrome, but it works.
    ⠀⠀- The keyboard shortcut MUST be set to ShiftC.

I've gone as far with this as I'm willing to go, but I'll answer questions.

-Chris


Create Rich Text Deep Link to Web Text v1.00.kmmacros (12 KB)

1 Like

@MartinPacker -- Hey Ma

Hey @MartinPacker -- i don't know what flat text is, but the text pasted should be rich text for it to work for me

Hey @ccstone -- thanks so much for this! gonna take it for a spin and let you know

1 Like

Maybe I should've said "plain text". "Flat" is the same thing and the term refers to the lack of structure. Sorry for the confusion.

It seems you want to retain the structure of rich text.

Gary at MacMost.com just shared with his Patreon group how to create a web link that highlights selected text on a web page when followed.

Here’s the link if you have a Patreon acct with him:
https://www.patreon.com/posts/how-to-create-78512933?utm_medium=clipboard_copy&utm_source=copyLink&utm_campaign=postshare_fan&utm_content=join_link

He found (as @zendude has) that Chrome creates a link of selected text and that that link created in Chrome also works in Safari. The format of the link is the web URL followed by #:~:text= followed by the text you want highlighted with %20 replacing the spaces.

ex. https://macmost.com/
cliptools#:~:text=Option%20to%20add%20the%20item%20to%20the%20Saved%20Clips%20list

He goes on to create a macOS shortcut that creates the link from selected text on a web page.

I used the Execute Shortcut action to run the shortcut from Keyboard Maestro. Here's my issue.

The macro returns the result up to the = but nothing after. From the above example I get

https://macmost.com/ ](https://macmost.com/)
cliptools#:~:text=

Instead of

https://macmost.com/ ](https://macmost.com/)
cliptools#:~:text=Option%20to%20add%20the%20item%20to%20the%20Saved%20Clips%20list

Here the macro set up
image

and the Shortcut referenced

The shortcut works from the service menu as is set for. Clearly, I'm missing something.

The Shortcut receives input from Quick Actions -- in this case, the text selected in the Safari window. But rather than find a way to use KM to get that text and feed it to the Shortcut, you should be able to do the whole thing in KM. Something like:

Highlighted Text Weblink.kmmacros (2.5 KB)

Image

1 Like

This works; thank you! Much better w/o referencing the Shortcut. All in all a short, simple list of actions.

Looking up the 'Percent Encode for URL' lead to the list of possible filters and there are a lot :flushed:

In figuring why you got to 'randomly' declare a variable %SafariURL% I see it's actually the token 'Safari Document URL'.