Hi. Sometimes I’d like to quickly preview a webpage without resort to a web browser, in the same way you can do that in Apple Mail (right click a link to preview web page). Is there a way to reproduce this for any link (including hyperlinks) in any app? What I do now is: I created a simple Automator app which gets the clipboard, extracts URL, puts it up in a website popup. Pretty convenient, but you still have to copy the link first. Then I invoke the web pop-up app using KM hotkey. If possible I’d like to at least be able to use the KM html prompt to display the page, but that may be a really dumb idea. I’m no pro!
Thanks.
You could select the link, and then trigger a KM Macro which would:
- Copy the link
If you don't mind, I'd like to see your Automator workflow.
Yes, I have an automator workflow which acts as a service, which allows you to select a link – a full link, not a hyperlink – and preview it. But that doesn’t help me when I am reading an article with hyperlinks, because I can’t select the hyperlink and activate the service. I have to copy the URL behind the hyperlink. I wanted to do that and preview the link, without having to open a browser tab or save to a read later service. So, here is the service workflow, and here is the app version.
The service version really only works when you have a full link to select. With a hyperlink, you would copy the URL, then, using a KM hotkey, you trigger the app version, which gets the clipboard, extracts the URL and pops up the web preview. (The service workflow goes in the Services directory in Library; the app goes directly into Applications directory).
Here is the KM hotkey and app version in action using Tweetbot hyperlinks. (Selecting a shortened URL does not work with the service version, either.)
Here is the app version in action using a hyperlink.
To make the service version effective for hyperlinks, after selection of the hyperlink text, the workflow would have to copy the link, and I don’t know of an action that takes selected hyperlink text and gets behind it to copy the URL.
Although this has had ONLY LIMITED testing, I think it should work for you to extract the URL from a selected hyperlink. Give it a try and let me know if it works for you.
This macro uses RegEx to extract the URL:
/<a.+href[ \t]*=[ \t]*["'](.+?)["']/i
You can view the RegEx with explanation here:
RegEx101.com
There is another approach that uses Execute JavaScript in Browser Action (KM Wiki) to extract the URL. It might be more reliable on web pages.
###MACRO: @Web Extract URL from Hyperlink
~~~ VER: 1.0 2017-05-23 ~~~
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/9/9da0fbd80e61a9dff335c3a179680d3790e48a95.kmmacros">@Web Extract URL from Hyperlink.kmmacros</a> (2.2 KB)
---
###ReleaseNotes
**NOTICE: This macro/script is just an _Example_**
* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete. It is provided as an example to show you one approach to solving a problem.
---
<img src="/uploads/default/original/2X/4/4f3ee762ab8e0085219e03ed77bab78762fc75dd.png" width="529" height="740">
###JavaScript
```javascript
ObjC.import('AppKit');
var htmlStr = pboardUnpacked('public.html')
var reURL = /<a.+href[ \t]*=[ \t]*["'](.+?)["']/i;
urlStr = reURL.exec(htmlStr)[1];
urlStr
//~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~
// Types: 'public.rtf', 'public.html' etc
function pboardUnpacked(strType) {
return ObjC.unwrap(
$.NSPasteboard.generalPasteboard.stringForType(
strType))
}
```
@BKammer, here is the JIB macro I mentioned. You can choose what works best for you. This is based on the excellent macro by @ComplexPoint. I made a simple mod to the JavaScript to return only the URL.
##Macro Library @Web Copy webpage link under cursor as Markdown @URL (Author.@ComplexPoint)
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/0/0e698a46a3ccc306740ecc273fac8658ac756ee7.kmmacros">@Web Copy webpage link under cursor as Markdown @URL (Author.@ComplexPoint).kmmacros</a> (27 KB)
---
###Example Results
<img src="/uploads/default/original/2X/c/c21fd781bbf28ebca172bfa724c83fe36e5fda1e.png" width="463" height="184">
---
###ReleaseNotes
REFERENCE:
Topic Title: Copy link under cursor in Safari or Chrome as Markdown
URL: https://forum.keyboardmaestro.com/t/copy-link-under-cursor-in-safari-or-chrome-as-markdown/1874?u=jmichaeltx
Date: 2015-08-16
Author: ComplexPoint
MODIFIED VERSION
(All Actions with changes are in Magenta color)
Date: 2017-05-23
By: @JMichaelTX
Purpose: Change output from MD to just text of URL
(function () {
// OR MOUSE HOVERING OVER A LINK ?
var nh = document.querySelectorAll(':hover'),
iLast = (nh ? nh.length : null),
nodeHover = (iLast ? nh[iLast - 1] : null),
// LINK IN ANY NODE UNDER MOUSE ?
oLinkNode = nodeHover ? document.evaluate(
document.kmvar.xPath,
nodeHover,
null, XPathResult.FIRST_ORDERED_NODE_TYPE, 0
).singleNodeValue : null;
// --- RETURN ONLY THE URL ---
return oLinkNode ? (
(oLinkNode.getAttribute('data-href') || oLinkNode.href)
) : ''
})();