Copy link address (Chrome or Safari) (Newbie alert)

Hello. Newbie alert (moved from Quickeys).

Trying to get a macro to work where I want it to copy to clipboard a link address from a certain page. I have navigated to this page with KM without a problem. It has a single word on screen, so it could be scraped. I cannot find a solution and “your topic is similar to” search suggestions do not really match.

I can click it with the option Click Google Link XXX where the word that anchors the link, e.g. download or get. But I want to COPY the address (I’d right-click and select copy in Chrome normally) so I could open a document and paste it there.

I am not a Javascript guru but could not see any specific field or form to try.

Could someone give a few clues to help me continue figuring this out? I don’t want to use screen recording as browser sizes/font sizes can change and then it would break things).


Hi @Luoto,

I'm not completely clear what you're asking. It sounds like you're wishing to be able to copy the URL to which an HTML link refers. This is already a feature in Safari, as demonstrated here:

If this isn't what you're wanting, can you explain clearly what you want this macro to do ?

Sorry, maybe my head was even less clear than I thought (came back from the hospital and perhaps should have slept).

Yes, I know there is a feature with “copy link” in Safari and Chrome, but within KM I could not see a way to:

a. locate the link with certainty (if there were several links on a page but I only wanted the one called “GET”

b. then fire the right-mouse click to get this copied to the clipboard (to be fair, I have not tested this step, since I could not get the first step figured out).

So, to summarise, I get to a page that I want to (good!)
then look for a link with a certain displayed name, e.g. GET
on that link somehow copy its underlying A HREF= to clipboard

Is there a way to work towards this, so I can continue to refine (!) my first KM macro.


This is easy enough using JavaScript in Browser, but we need to see the underlying HTML.

  1. Right-click on the target name/get
  2. Select “Inspect”
  3. This will open the Safari or Chrome Dev Tools, with the HTML highlighted
  4. Select a major parent element (usually a <div>) above that, right-click, and Copy HTML
  5. Post the HTML here using Code Block

Hi @Luoto,

Thanks for clarifying, that was easier to understand.

I presume the displayed text for a given link could be of any length and word count ? The example you gave using "GET" seems very precise, but I'm going to proceed as if you intended a more generalised example.

In order to use a link's anchor text (the text displayed to a user in the form of a hyperlink) to retrieve its target (the URL given by the value of the href attribute), you need to use a bit of JavaScript.

Therefore, firstly, you want to make sure that the relevant JavaScript settings permit us to run code within a browser tab. For Google Chrome, I believe the content settings control this: chrome://settings/content. In Safari, you can enable the Develop menu in the application preferences under the Advanced pane, then make sure Allow JavaScript From Apple Events is ticked.

One example piece of JavaScript code that will get you a hyperlink's target from its anchor text is:

var anchorText = 'Copy link address';

This performs a case-insensitive, fuzzy search, which will match any anchor text that contains the given anchorText.

To perform an exact match, this code will achieve that:

var anchorText = 'Copy link address (Chrome or Safari) (Newbie alert)';

To demonstrate how this is implemented within a KM macro, I've created this one:

Find Links Using Anchor Text.kmmacros (27.7 KB)


A Typed String-triggered macro, triggered by entering text of the form A[%anchorText%] in any text box, where:

  • A: is a literal uppercase or lowercase "A", signifying that the anchor text to be supplied is to be matched exactly (uppercase) or fuzzily (lowercase).
  • %anchorText%: is the text supplied against which hyperlinks are to be matched by their text content, to within the allowances provided for by A or a.

The trigger text is removed and replaced with the matching URL(s), which are also stored on the clipboard.


A[Find Links Using Anchor Text.kmmacros]

A[Find Links Using](empty string) (i.e. no matches)

a[Find Links]



Thank you both.

I presume I change the field %TriggerValue% for the specific text I want, in this case GET? or FloweryApple if I wanted to search for FloweryApple?

I tried that and ran it on the page but nothing was in the clipboard, just “Find Links Using Anchor Text” (without quotes). (is this right, or has it placed the url as a variable as there is reference to them in the macro)?

I ran it as execute macro within my own macro which had so far opened the page, selected a link it could find and then the page it was examining for a link.

Here’s an extract of the HTML as I was asked for.

<td align='center' rowspan=2 valign='top'>
<a href=''>
<a href='http://SECOND_ADDRESS_NOT_TO_USE.IO/book/index.php?md5=DFB4E8FBF4CA564C69B606935AD2FAF5&oftorrent='>Download via torrent </a>
<input id="textarea-example" value="Ronald D. Smith-Strategic Planning for Public Relations-Lawrence Erlbaum Associates (2005).djvu" type="text" size="9">
<button class="btn-clipboard" data-clipboard-target="#textarea-example"> (need rename file)</button>
<script>new Clipboard(".btn-clipboard");</script></br>

There are two links, one is for HTTP and the other isn’t I’d want the first one, so guess I would put the pull URL up to get.php (so the script would look at the first instance of this URL in the html)

I appreciate this extensive handholding at the beginning!

No, you don’t replace the %TriggerValue% - that’s probably why the macro didn’t work for you. That first action is specific to the type of macro I created, namely one that utilises a Typed-String trigger. The %TriggerValue% will contain the text that was typed leading to the macro being triggered. The action then takes that text content and separates it into parts that the rest of the macro uses.

The part that ends up becoming the anchor text one wishes to search for is stored in the variable input.

If I test this macro (which I still have in operation on my system) here on this page whilst I’m currently typing this reply, I should be able to retrieve the URL target the is hyperlinked by the text “GET”:


As the GIF animation demonstrates, by typing A[GET], suddenly the URL gets pasted from the clipboard into my text box.

The only reason I created a macro of this fashion is because you didn’t specify how you wanted your macro to function, beyond stating that it would, at some point, be retrieving URLs given a search string, but not saying how it would be triggered or much by way of the situations in which you’d be using this macro. For instance, how were you wanting the anchor text to be fed into your macro ? And what would be the mechanism of triggering the macro to run ?

If you want to give more details, I can try and modify the macro to something more to suited to your actual use cases. For now, I’m not sure what else to offer.

Let me know if you’d like me to adapt the macro for you.

Based on this HTML, here's a simple JavaScript in Browser solution:

var getXPath = '//a[contains(., "GET")]';
var nodes = document.evaluate(getXPath, document, null, XPathResult.ANY_TYPE, null)
var getElem = nodes.iterateNext();

Caveat: This assumes that this is the FIRST link on the page that contains "GET".
It would be better if you have a <div> element above this link that contains either an id or a class that we could use for the XPath.

Please test and let us know if this works for you.

Here's a simple macro as an example:

Example Results

MACRO:   Get Link Using XPath Contains Text [Example]

~~~ VER: 1.0    2018-04-26 ~~~


Get Link Using XPath Contains Text [Example].kmmacros (2.8 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.



Thank you very much to the both of you for your kind solutions! I tried the first one yesterday before I ran out of time and I could not get that to work (I think because of my understanding deficiency). The second solution, which came overnight, let me do what I wanted. Boy, have I a lot to learn.

I have managed to integrate this into my first macro that controls Chrome, checks for it to load and then execute the script, and append carriage return to the text afterwards in the file I want to store the link in.

It was asked whether there was a DIV or ID class for the link: There wasn’t and I looked back to the start of the web page. It worked for me though so thank you!

May I ask a supplementary question, and possibly get a guide to what to search for (or a modified solution)?

  1. On another part of my planned larger macro I would like to fire the first link in my Gmail, to avoid needing to manually click the link and then start the (existing) macro, just removing one stage. There is no physical link name, so I cannot use the click link feature in KM. The link IS displayed within the (visible) email and always begins where xxxxx will change dependent on the URL BUT IT IS THE ONLY visible text of when I search on the page.

Phew, one feels dumber than a doorknob with all of this Javascript :slight_smile:

So, have we solved your original issue/question?

If one of the above posts solves your problem/question as originally stated, please check the “Solved” checkbox (click for details) at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.

Generally it is preferred to start a new topic with a new question. But in this case it seems close enough to your OP that we’ll try to answer here.

By now, you should know the drill:

Please post the HTML around the link of interest. Try to include a parent HTML element about it that has and “id” or “class” attribute.

It would also be helpful if you can post a screenshot of the region of the screen of the target link. You can blur any sensitive info, but try to leave as much as possible.

Thank you! I have marked the topic as resolved.

Here is the information for the other link, you kindly agreed to consider!


There does not seem to be any clear or useful DIV and Table to copy (it is very long and has nothing to do with the link) but it starts with

and the links appear before the next div of class="CToWUd">

IT IS STRANGE but if I view source of the entire mail in Gmail the downloiad link is not shown, so maybe the inspect is looking at a called reference? I've looked through several mails and the URL does change in the following extract, so if that could somehow be matched and clicked it would be perfect.

I think the source gunk changes each time too for Google's love of tracking.

Does this help? Thanks!

edit: struggling to get the code to display, even though in the message window it is visible. Attaching also. (953 Bytes)

I'm a bit fuzzy on your source data and requirements, but if it is:

Return the first link in the body of a gmail email

Then this should do it:

var targetElem = document.querySelector('div.a3s.aXjCH a');
if (targetElem) {
  var link = targetElem.href;
} else { var link = "NOT_FOUIND"}


Note that the key <div> in this case is the one that starts the body of the email:



Thank you for your continued help. My apologies if I was not so clear.

On that “email” page the desire is to FIND the link and then OPEN it in the same tab, to allow the next part of the script to run (using the previous Javascript you gave me, which I have working).

I am presuming there is some form of opening instruction from the link missing?

This is how I have applied it:

If so, how might I OPEN the link discovered, so that the rest of the script continues?

I looked for Javascript Open Link online but the examples all give a “stated” URL rather than the value (?) link you have assigned?

This feels to be a baptism by fire, but something interesting to watch and try to decipher. One day, maybe, it will become clearer.

Thank you!

There are a number of ways of doing this. Using a KM method, all you need is one KM Action: Set Chrome URL. You will also need to change the output of the Script to "Save to variable" as shown below.


You may want to study the various Browser Actions in the KM Wiki.

Thank you. This was perfect. I have been devouring the online resources but there is a lot to take in. I had not understood the variable issue (I do now, thanks!) and also changed to use a new tab opening element.

I do appreciate the assistance! On to the next considered workflow, and to see where I get stuck!