Get Safari Link Clicked


When I click a link in a web page displayed with Safari, I would like to inspect the link before following it.

If the link is to a pdf document, I want the document to be downloaded, not display it in the browser. I know I can achieve this by holding down the option-key, but I want the download to happen based on the URL clicked ending in .pdf. I.e. I want to get around the pdf support in Safari.

There used to be a simple command:

defaults write WebKitOmitPDFSupport -bool YES

To do this, but it no longer works with the latest version of Safari.

So I need a macro that is activated when I click the left mouse button on a link, the macro then examines the link under the mouse, looks to see if it ends in .pdf, and then clicks again but with the option modifier down, so that Safari downloads the document instead of displaying it.

I found no way of catching text that is under the mouse, but I may have overlooked something.

Thanks in advance.

Hey Robert,

This will get you the URL whose link you're hovering over in the front browser.

It's up to you to manage your mouse button trigger.


Download Macro(s): Front Browser – Extract The Link URL Under the Mouse Cursor v1.00.kmmacros (7.2 KB)


Keyboard Maestro Export

  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

1 Like

Hi Christopher:

Yes, that will do it, thanks very much!

Proving several things:

  • I did not overlook anything in Keyboard Maestro.
  • My absolute disgust of JavaScript must have unconsciously come into play.
  • It always is faster when more than one brain is applied to a problem.
  • Apple is making things more and more difficult.

Some of these you may disagree with, but I also bet that I've been in computing several times longer than you and therefore have developed a very different taste for it.

Thanks again,

Not really.

I'm not in love with JavaScript (or AppleScript for that matter), but I find them both to be very useful and actively continue to expand my knowledge of them as time and energy permit.

I've been involved with computers since the late '70s – so several times seems quite unlikely. :sunglasses:

:slight_smile: OK, not several times then (1968 for me).
Unusual, as most people on these boards are much younger.
AppleScript is extremely inconsistent but useful indeed, and JavaScript has that horrible C syntax.
But OK.

Your solution works to an extent, the basic idea is fine. If I can manage to do so, I need to refine it to handle several situations like clicking the show previous and next page buttons and other stuff in the title bar of a window.
And alas it cannot solve the problem of a link that gets transformed at the server side.
The real solution of course is to convince Apple to let users decide what they want, and that's increasingly impossible.
But at least the JavaScript snippet is a step around Apple's arrogance.
Thanks again.

You mean the toolbar?

There are already keyboard shortcuts for next and previous – no need to click.

Apple are catering to the majority, who aren't as technically sophisticated -- or as experienced -- as you. Since you've been round the IT block a few times, you'll probably remember the fun we used to have with PDF Reader plugins...

I, too, would like to be able to select helper apps like we used to. While we wait for Apple to see the light (and I won't be holding my breath on that!) there's a couple of other potential workarounds.

Use another browser that does give you the control you want. There are plenty out there and almost all of them give you more control than Safari.

KM solution -- you might be able to use eg a "Focused Window... Title changes" trigger, limited to Safari. Test the URL on every change, and if it ends with ".pdf" then download the PDF and go back in the browser window.

Meanwhile -- submit feedback. If they don't know, there's no hope of a change.

1 Like

ccstone: no I don't mean when I need to click those, but the conflict when your macro is active.
As I have configured things now, when I click anywhere inside a Safari window, your macro is called when the mouse is released. If the mouse is hovering over a link, then it all works fine.
If it is over anything else inside the web page area, it is also fine.
the triggering is:
triggered when apple optical mouse button1 is released and when Safari is at the front.
Now that trigger event also happens when I click in the window title bar (which also is the toolbar).
What I therefore still need to do is to distinguish the title bar/toolbar area from the rest of the window, before looking at what's under the mouse. Not difficult, but tedious and I have not got round to it (urgent tax return stuff comes first).
Don't worry, I'll get it fixed, and it's not urgent anyway, as I will only go to Ventura when my new M2 laptop arrives. It's ordered, but I'm not really looking forward to it...

Yeah, well.
I have submitted feedback to Apple on numerous occasions. As you say, they are now catering to what one of my friends calls "the unwashed", and bent on making money only.
And as you also say, I won't hold my breath on Apple seeing the light. They don't care. However, they still do have developers, so to some extent they do have to listen to those.
--using another browser is no option, because they are not scriptable with Applescript, and I need that for web development
--KM solution is OK, I'm grateful ccstone thought of the JavaScript solution
--Apple sometimes makes the same error twice. I happen to be an "international", i.e. I do not really have a country. I worked at CERN for nearly all my active life. I use English, 24h-clock, metric, euros, and apostrophe for digit separator in numbers. Some years ago (Tiger) Apple (probably "sir" Jonatan Ive) decided that the preferences panel was unnecesarily complex, and removed the individual settings. You had to be happy with the settings for your country. But there is no country locale that gives me the combination of formats I want. The apostrophe is present in the Swiss setting but then the currency is wrong, etc. Fortunately, I was able to surgically cut the preference panel out of Panther, paste it into Tiger and carry it along through OS versions until the separate choices were back in Snow Leopard (I may have the OS names wrong, it was long ago). Now in Ventura they have been removed again! There is no way to get my choices, and all my documents containing numbers would go wrong. I did find a Ventura workaround through a terminal command, and so far it works. But for how long. I have reported this omission as a serious bug.
And on and on.
Once in a while I look closely at Linux Ubuntu. But it's hell unless one's only usage is to produce more Linux forks.
Have a good weekend, both of you.

I'd try testing the contents of the on-hover event.