If there is a formatted URL on the clipboard, HTML with href tag etc, is there a way to extract the underlying URL text? the http://... etc, rarther than the "pretty" text.
I thought the Filter Clipboard with a URL Fragment might be the way, but that seems to be for something different? Thanks
I'm not sure what the URL filter options do, other than the encode one, and there's not much help in the wiki. But a relatively simple regular expression can extract the URL:
Thanks guys for the replies. The problem is KM clipboard actions seem to only see the formatted text.
E.g. if I copy the link to the Forum at the bottom of the KM website it sees the text "Forum", not the underlying HTML / URL.
Applications from which we copy typically place several different pasteboard items (different formats) in the system clipboard.
The receiving application decides which of these to read.
Keyboard Maestro will default to reading the UTF8 text component – in the case of a browser-copied link, just the label.
One way to extract any public.html pasteboard item that is in the clipboard, so that you can work with the HTML source, is to run an Execute JavaScript for Automation action like this (which you could precede with a Copy action to copy your browser selection).
// (() => {
// "use strict";
ObjC.import("AppKit");
const main = () =>
either(
x => x
)(
x => x
)(
clipOfTypeLR("public.html")
);
// ----------------------- JXA -----------------------
// clipOfTypeLR :: String -> Either String String
const clipOfTypeLR = utiOrBundleID => {
const
clip = ObjC.deepUnwrap(
$.NSString.alloc.initWithDataEncoding(
$.NSPasteboard.generalPasteboard
.dataForType(utiOrBundleID),
$.NSUTF8StringEncoding
)
);
return 0 < clip.length ? (
Right(clip)
) : Left(
"No clipboard content found " + (
`for type '${utiOrBundleID}'`
)
);
};
// --------------------- GENERIC ---------------------
// Left :: a -> Either a b
const Left = x => ({
type: "Either",
Left: x
});
// Right :: b -> Either a b
const Right = x => ({
type: "Either",
Right: x
});
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = fl =>
// Application of the function fl to the
// contents of any Left value in e, or
// the application of fr to its Right value.
fr => e => "Left" in e
? fl(e.Left)
: fr(e.Right);
return main();
// })();
Exactly which option is that in the contextual pop-up? I tried with Copy Link Address, and my simple macro worked fine.
It failed with Copy Link to Highlight, but that includes a bunch of extra text, so I didn't expect it would work. Edit > Copy just copies text, so that doesn't apply, and I don't see any other copy link options in Chrome's contextual menu.
That’s strange Rob. It is the “Copy” option (no other words) via a right click context menu on the link.
For me that one copies the link as it shows in the browser, allowing me to paste it in an email as nicely readable text that is still a link the reader can click on to open the link.
It seems it depends on where it's being pasted, as @ComplexPoint basically explains. If I use Copy and paste into a KM variable, then I just get the copied text. If I paste into a rich field in KM (or TextEdit or Mail, etc.) then I get the formatted link.
If I were doing this, I'd probably just use the Copy Link Address menu item instead of Copy, because then you just get the URL. But I don't know your full needs, so that may not work for you. (Or I'd use Safari, where it just seems to work with Copy ).