Set Clipboard to RTF Hyperlink & Plain Text MD Link

Use Case

  • Set the System Clipboard to BOTH a Rich Text Hyperlink, and a Plain Text Markdown link, of the FrontMost Browser.
  • When you Paste, it will automatically use the format of the receiving document.

MACRO:   Set Clipboard to Rich-Text (RTF) Hyperlink & Plain Text MD Link VER 3 [Example]

--- VER: 3.0    2020-03-05 ---
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

UPDATED: 2020-03-05 17:00 GMT-6

  • This is a MAJOR update, which takes advantage of the KM8+ FrontBrowser tokens.
  • Works with Safari, Google Chrome, All Chrome-based Browsers (like Brave Browser), and Firefox.

DOWNLOAD Macro File:

Set Clipboard to Rich-Text (RTF) Hyperlink & Plain Text MD Link VER 3 [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Set Clipboard to Rich-Text (RTF) Hyperlink & Plain Text MD Link
    • Based on the FrontMost Browser, create and put on Clipboard:
      * RTF formatted Hyperlink
      * One or All Plain Text Links
      (based on KM Var "CBPlainTextType")
      * Markdown code
      * BBCode
      * Wiki
    • When You Paste, it will use the format of the receiving document.

REQUIRES:

  1. KM 8.0.2+
  2. macOS 10.12.6+ (Sierra+)

HOW TO USE:

  1. Have Safari, Chrome, or FireFox as the ACTIVE, frontmost app
  2. Trigger this Macro
  3. After the Link Text and URL are extracted, a Prompt will be shown to allow you to confirm or change this info.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
  1. Assign a Trigger for this macro
    • I prefer ⌘⌥C.
  2. Move this macro to a Macro Group that is only Active when you need this Macro, most likely a Group for Browsers
  3. ENABLE this Macro.

TAGS: @WebScrape @Hyperlink @Link @URL @RTF @MD @Markdown

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

6 Likes

I just discovered a cool, unexpected, benefit of this macro/script.
If you select a hyperlink from a Google Search, it will create a very clean hyperlink with ONLY the intended destination. IOW, it cuts out all that Google noise. :sunglasses:

3 Likes

I think I've found a better RegEx pattern for this. I'm going to test for awhile and if all goes well, I'll update the script here.

See my post here.

If you want to try the new RegEx pattern, you can replace this line in the function extractURLfromHyperlink(pstrHTML) in the AppleScript file:

set strRegEx to "<a.+?\\s*href\\s*=\\s*[\"\\']?([^\"\\'\\s>]+)[\"\\']?"

It is working very well for me so far.

###Note that I have made a major revision to my original post above to provide the complete, finished, Ver 1.0 release.

I have just uploaded Ver 1.1.

1 Like

Updated original post/download just now with a major revision.

Hi and good day,
Today I’m exploring this neat script.

And it works, the clipboard provides the earlier selected text as a hyperlink of the relevant page.

1.)
HOWever I get an error message:

*** SCRIPT ERROR ***
Action Result: OK
Script Msg: «class bhit»:OK, «class gavu»:false
Macro Aborted.

POSSIBLE CAUSE:
Invalid path or name of Script file.


As far as I can see the path is correct and the name of the script, too.

2.)
Also the context menu, for choosing MD, Plain Text, etc. only appeared the first time/ run, not later.

(I rebooted KM & KM engine)

What could be the reason?

/
with best regards,
Omar KN
Stockholm, Sweden

Hey Omar,

You installed everything where JM specifies?

You DO have the Satimage.osax installed?

-Chris

All 4 items are installed, also Satimage.

All 4 files are in ~/Documents/My KM Data

… or should the .kmmacros files be in a special folder somewhere else?

Here is a screendump: https://d.pr/free/i/Dwf6Xu

So the macro does work, except for the error message, and I rebooted, too.

And also Satimage: https://d.pr/free/i/nuRGyI

/
Any assistance would be fine!

With best regards,
Omar KN

I have not idea why you are getting that error msg -- something has changed with later macOS versions. When I wrote this script I was using Yosemite.

IAC, for now you can either ignore the error msg, or change the Macro Confirmation options from "Full" to "Brief". I'll post an update later to fix.

Added ( return “” --> NEW CODE 2018-03-09 ) to line 306 as acc. to Chris S.

/
okn

1 Like

This is great. Is it possible to set the font and point size as well?

Yes, but you have to change the script:

image

Near the top of the script you should find this line:

property fontStyleStr : "color:blue; font-family:verdana,geneva,sans-serif; font-size:14px;"

This is standard web page CSS styling, so change as you like.

This is an excellent script! Is it possible to copy the Title and URL automatically so it doesn't have a pop up? Then I can just paste into my desired destination.

Hi @James_Hawkins,

This action is there for a reason. It allows you to check the link before pasting it somewhere else. It also allows you to change the format at the last moment.

That said, if you don't need it, you can simply disable this action. But, make sure you set %Variable%DND_SCB__MarkDownType% to your desired format.

1 Like

How do I set %Variable%DND_SCB_MarkDownType% as my desired format exactly? Where do I change this?

Also, is it possible to have this only copy from Microsoft Edge web browser?

Thank you.

You may leave the action I said above enabled and run the macro once. It will remember your choice.

The macro will set the format to MD if you have not set.

It does not really matter as long as you run it only when Edge is the front browser. However, to make the macro run only when Edge is the front browser, put the macro in a macro group and set it to add Microsoft Edge to the list.
image

2 Likes

I seem to have gotten it to work, but why does it create a large two line gap when I paste? I pasted it at the white cursor location and it pastes two lines down.

Screen Shot 2021-10-01 at 5.07.34 PM