Modifier(s) + Click to Copy URL from Hyperlink?

I discovered cmd+click for opening tabs far too late in my life for me to admit and I was curious if there was anyway to set this up in KBM. Would this involve some scripting?

Edit: I suppose I should add my goal for some context. Along with copying the link, I'll want to append it to a variable, and then eventually I'll want to open all the links in new tabs all at once. The latter of which I don't know how to do either but I figured I'd save asking how to do the latter for a seperate thread.

As far as I know there is no way for KM to intercept the primary or secondary mouse buttons. However if you are willing to make a compromise and use a mouse with extra buttons, it's probably fairly easy to program one of the extra buttons to do that. I use a mouse with about ten buttons, so I think I could live with that approach. But that might not be acceptable for you.

Actually I have succeeded in using the mouse driver that comes with ten button mice to redefine the primary mouse buttons to other values, which means I am indeed able to get KM to trigger on primary mouse buttons, but that's a bit of a "drag" because then I'm unable to replicate the "drag and drop" feature that comes in macOS.

I don't know why operating systems don't let you redefine the primary mouse buttons. It's either laziness or a security feature. But because of it, not even KM is able to intercept them.

However if you are willing to make a compromise and use a mouse with extra buttons

Absolutely, not a compromise at all, my mouse has extra buttons and I'm not doing a thing with it. So does making this work involve messing around with the driver or KBM or both? I'll ultimately want to append the url to a variable with a click so part of it will have to go through KBM.

Okay, I guess the hard work begins. I wasn't expecting that to be acceptable to you. :slight_smile:

Now I need to understand the issue a little better. Are you trying to log the URLs that the user click on, for later viewing? Is that the purpose? If so, we might not even require anything to do with the mouse.

KM can trigger on many mouse buttons without changes to the mouse's software. But in some cases you may have to go to the software that came with your mouse and assign a value to the mouse button you want to use. A few days ago I temporarily programmed one of my mouse buttons to send the scan code for the End key on my keyboard, which I don't use too often. In other cases you can use the mouse button directly as a trigger key in a KM trigger. That's the first issue. If you need help we may need to find out what kind of mouse you are using. The first thing is to try to see if KM accepts one of your mouse buttons in its USB Device Trigger trigger.

The second issue is different. We need KM to do two things when the user clicks on that mouse button. (1) We need to click the primary mouse button at the current mouse location, which is very easy and reliable, and (2) We need to fetch the URL from the link that next appears after the click. But the second one is not reliable because as you may know, web pages are like living creatures whose URLs can change at any time. There is no guaranteed reliable way to ensure that KM sees every URL that appears. But I think we can write code that catches the vast majority of most web page URLs.

You were probably thinking that we would get the link in the HTML code on the spot where the user clicked. I'm not even sure that would be possible. My approach is to sue KM to fetch the URL that appears after the user clicks. But that means there's a problem with my approach because I was planning to use the %SafariURL% token to get the URL. This isn't compatible with your scenario because you wanted to use CMD-CLICK which opens the URL in a separate tab. Chances are fairly good there's a solution to this, because CMD-CLICK always opens the new page in the LAST tab, but at the moment I'm not sure what it is. I think it might require some AppleScript and I'm pretty lame at that. Let me ponder it. It's probably easy to fix that but right now it's a bit beyond me.

Okay I googled it and experimented and found this command seems to fetch the URL of the window that you CMD-clicked on:

So now I think we have all the tools we need to solve this. But before I write any code, can you tell me if it sounds like I've understood your problem?

The first thing is to try to see if KM accepts one of your mouse buttons in its USB Device Trigger trigger.

Won't be able to grab my mouse until tomorrow but will definitely test it out when I do.

But the second one is not reliable because as you may know, web pages are like living creatures whose URLs can change at any time. But I think we can write code that catches the vast majority of most web page URLs.

Not sure if this matters but the links I'd want to pull are all links in lists on the same few websites. An example would be links to restaurants on yelp within the yelp search results. I don't know if that makes things more consistent. If it didn't pull 100% of the time outside of those situations, I wouldn't mind too much.

This isn't compatible with your scenario because you wanted to use CMD-CLICK which opens the URL in a separate tab.

Oh no, don't need to use cmd-click at all, mentioning that was just an anecdote of finding that it existed, I apologize, reviewing my post I could see how that could be misinterpreted

%SafariURL%

I'm sure it doesn't matter either way but I usually work with chrome

Does this all make my situation more clear? You are very generous for helping me with this by the way, thank you.

Chrome uses a different AppleScript syntax and since I'm not sure what that is I may start with Safari and deal with converting to Chrome later.

If the links are mostly form the same website then that will help because the predictability of the URL will be higher when we know the site we are dealing with.

I probably should have said Button7 instead of CMD-CLICK. That is, whatever the name of the button is which KM will recognize. I guess I misspoke. I'm not perfect.

The approach we will take will meet your requirements, I think, but the unpredictable part is the accuracy of the URLs since websites are prone to replace URLs from time to time and it's not possible for any human or any software to demand URL stability from web servers. It will probably be right most of the time, but I can't be sure until I test it. For now I'll do my testing on Yelp since you mentioned that site.

I don't think this is a difficult problem at all. But any time you deal with the real world there are things like timing issues that can be difficult to work out.

Let me create a draft macro now showing my approach. My first draft may not be perfect. It will take a few minutes.

I'm sure any miscommunication issues are on my end but just to be safe and make sure we're on the same page as far as the cmd-click, you had mentioned cmd-click opening a url in a seperate tab and cmd-click opening the page in the last tab, and I don't need anything to open in another tab.

Hmm, ok, so you don't need anything to open in another tab? That may make my life easier here. My solution is almost working. I presume now what you want is just to log the URL in the OPEN tab after the user clicks. If so, then I don't have to figure out my last little bug in my solution.

Yeah nothing in another tab, basically just the equivalent of right-clicking and copy link address, and then appending the clipboard to a variable

Okay. My code should be working, but as often happens I spend the last 15 minutes trying to figure out some teensy thing. Okay I think it's working. Let me tidy up its appearance and upload it in a minute or two.

Okay this code seems to work for me. Ignore the macro name. Notice that the hot key trigger says Home, but that's because I defined my mouse button to be the Home key. You can define it to other things, including things that aren't on your keyboard, but I may have to know your mouse model to help you figure that out.

[Edit:] First, notice how there's a Click Mouse action where your mouse is currently pointed. That's because when you assign a value to a mouse button, it doesn't actually click, so we have to.

Then notice that I have a Pause 3 second action. This is typically how long it takes for me to see a new page appear when I click on something. You may choose a different value. It is possible to make this piece of code better, but that would require many lines of code and I just wanted a bare bones solution working for now.

Then notice the AppleScript action which fetches the URL of the active Safari window.

The notice the last action which logs the URL. Please notice the extra CR at the end of the action. This is important.

I think this is the gist of the problem you wanted solved. If I'm wrong, let me know.

You also wanted a macro that opened up all the URLs in the saved variable. If you need help with that, let me know.

Move or Click Mouse.kmmacros (3.1 KB)

Simple. This will append the URL (Chrome or Safari) of the Front Tab to a KM Variable:

image

Thanks so much sleepy! I'll try it out and see how it works when i get back to my mac tomorrow. I don't know how to make the macro that opens the urls off the top of my head but I think it's good to challenge myself and I'll definitely ask if I hit a roadblock.

You are welcome. Sometimes it feels appropriate to let people steer the ship once it's pointed in the right direction. So that's what I did with you for the second half of the problem.

1 Like

I'm actually not trying to copy the url front tab in the browser

In this example list of restaurants from yelp https://www.yelp.com/search?cflt=restaurants&find_loc=Atlanta%2C+GA

I'd be trying to save the link of a restaurant's yelp page (example poor calvin's) from the search page

Oh, well in that case I think we have to revert to what I was talking about earlier. The link isn't visible to KM. That's why I wanted to have the link opened in a tab and fetch the link from the tab, which wouldn't be visible to the user since we can CMD-CLICK which doesn't activate the window.

Earlier on this page I wrote "You were probably thinking that we would get the link in the HTML code on the spot where the user clicked. I'm not even sure that would be possible" and that still stands. You can't fetch the link unless the page is opened, at least not without some really deep knowledge of AppleScript and maybe Javascript, which are both beyond me.

If you aren't satisfied with the link opening in an alternate tab, then maybe JM can chime in if its is possible for KM to get the value of a link the mouse is hovering over without clicking on it.

I'm doing any best to help. I'm not one of the top tier people here, though.

Ah darn, I'm very sorry that I completely misunderstood all of that.

I've just started looking into a chrome extension called OneTab that might be better for my purposes. I could cmd+x to open however many links and then click a couple buttons to collapse them all and export them in text.

That's not a big problem. It's another little step or two. No big deal. I'll try again.

If you just want to automate the right-click menu when you've already got the mouse over a link, that's easy enough:

Copy Hyperlink, Append to List of URLs.kmmacros (3.7 KB)
image

And this companion macro will open all of the URLs saved in the former macro, then reset it to empty:

Open All URLs in URL List.kmmacros (2.3 KB)
image

1 Like