How do I get a link over which my mouse cursor hovers?

I am trying to automate downloading of a url over which my mouse cursor hovers. I need to copy the url of the link over which my mouse hovers without left-clicking it and then use the copied link to create a new download in my download manager, “Progressive downloader”.
How do I go about it?

  • Right click at 0,0 relative to the current mouse location
  • Pause for the menu to appear
  • Insert Text by Typing “Copy Link%Return%”
  • Open URL “%CurrentClipboard” with desired application

<img src="/uploads/default/1045/497ff4a464f9664e.png" width=“525” height=“352”

After several episodes of trial and error, I was able to get it to work using the workflow you pasted. However, I had to insert pauses between the various actions to allow for the copy action and for the download application to become the active app. It is working but I am hoping it can be further fine-tuned.

Thanks a million.

Hey Mirizzi,

This is a trifle tricky, because it depends upon the URL showing in Safari's Status Bar, and websites can do more than show the URL of the link you're hovering over.

This runs from an 'Execute AppleScript' Action.

-Chris

Edit: Script updated 2015/05/16 to make the regex more comprehensive.

--------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/05/14 13:55
# dMod: 2015/05/16 00:17
# Appl: Progressive Downloader, Safari, System Events
# Task: Download the item the mouse is hovering on.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Progressive_Downloader, @Safari, @System_Events, @Download
#     : @Mouse, @Hover
--------------------------------------------------------------

set _destination to path to downloads folder

tell application "Safari"
  tell front document
    set _referer to its URL
  end tell
end tell

tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  tell application process "Safari"
    # set frontmost to true
    tell front window
      set _url to value of static text of group 2
      if class of _url is list and length of _url = 1 then set _url to item 1 of _url
    end tell
  end tell
end tell

if _url contains "http:" then
  set _url to do shell script "perl -wlne 'if ( m!https?://[^[:blank:]”'\"'\"'\"]+!i ) { print $& }' <<< " & quoted form of _url
  if _url starts with "http" then
    progressiveDownloader(_referer, {_url}, _destination)
  end if
end if

--------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------
on progressiveDownloader(_referer, _linkList, _destination)
  tell application "Progressive Downloader"
    if not running then run
    activate
    repeat with i in _linkList
      set _task to make new task
      tell _task
        set address to i
        set referer to _referer
        set destination to _destination
      end tell
    end repeat
    if (count of tasks) > 0 then
      # resume _task
    else
      error "No Tasks in Progressive Downloader!"
    end if
  end tell
end progressiveDownloader
--------------------------------------------------------------

I tried it using an online youtube downloader but it did not work. I tried to download the 360p video from this page, http://en.savefrom.net/#url=http://www.youtube.com/watch?v=PuPAM0qhl4g .

Hey Mirizzi,

If you can't paste the link into Progressive Downloader and have it work then the script can't do any better.

Video links tend to be rather tricky.

-Chris

I can copy and paste the link into Progressive Downloader and it downloads the file with no issues.

Hey Mirizzi,

Okay. It wasn't clear to me what URL you were trying to download, until I looked at the page for the third time.

This is the evil beastie we glean from Safari's Status line:

Go to “http://r1---sn-uvu-o53e.googlevideo.com/videoplayback?mt=1431751572&sver=3&mime=video%2Fx-flv&initcwndbps=980000&ip=118.175.94.147&mm=31&expire=1431773222&key=yt5&ipbits=0&dur=230.557&itag=5&pl=24&source=youtube&mv=m&upn=hWHodzlHp8A&pcm2cms=yes&ms=au&id=o-AEYHZEUQ-J5fcHdb3TLesVaz_QK6A6_BOsza8YsEUMhq&fexp=901802%2C916650%2C919154%2C936117%2C9405972%2C9407887%2C9408142%2C9408706%2C9408710%2C9408788%2C9412773%2C9413000%2C9413134%2C945137%2C948124%2C952612%2C952637%2C952642&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Csource%2Cupn%2Cexpire&signature=C15A643E0F229E473C3B2ED6BBBB765C64441CED.35FB1D9583A9974A4DD0602AC732A1DAE4348244&title=HUG+Love+-+R+G+Short+%238+%28480p+HQ%29”

Some sneaky person put the link in smart-quotes, and I didn't account for them in the regex of the first script.

Here's what we want to extract:

http://r1---sn-uvu-o53e.googlevideo.com/videoplayback?mt=1431751572&sver=3&mime=video%2Fx-flv&initcwndbps=980000&ip=118.175.94.147&mm=31&expire=1431773222&key=yt5&ipbits=0&dur=230.557&itag=5&pl=24&source=youtube&mv=m&upn=hWHodzlHp8A&pcm2cms=yes&ms=au&id=o-AEYHZEUQ-J5fcHdb3TLesVaz_QK6A6_BOsza8YsEUMhq&fexp=901802%2C916650%2C919154%2C936117%2C9405972%2C9407887%2C9408142%2C9408706%2C9408710%2C9408788%2C9412773%2C9413000%2C9413134%2C945137%2C948124%2C952612%2C952637%2C952642&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Csource%2Cupn%2Cexpire&signature=C15A643E0F229E473C3B2ED6BBBB765C64441CED.35FB1D9583A9974A4DD0602AC732A1DAE4348244&title=HUG+Love+-+R+G+Short+%238+%28480p+HQ%29

I have updated the original script in the original post — the main change being this line:

set _url to do shell script "perl -wlne 'if ( m!https?://[^[:blank:]”'\"'\"'\"]+!i ) { print $& }' <<< " & quoted form of _url

I would much rather use the Satimage.osax AppleScript Extension than shelling out to Perl. It adds regular expressions and other goodies organically into AppleScript, and with it installed we end up with this much nicer bit of code:

try
  set _url to find text "https?://[^[:blank:]\"'”]+" in _url with regexp and string result
on error
  set _url to false
end try

if _url ≠ false then
  # CONTINUE...
end if

In any case the script now works for me on that page.

Over time you will probably find more where there is a problem that needs to be worked around.

-Chris

Although this is a very old topic (May 2015), here is a much simpler, more reliable solution using Execute a JavaScript in Front Browser action provided in KM8, and the Append Variable option in KM9:

MACRO:   Get Hyperlink Under Mouse [Example]

**Requires: KM 9.0.2+&nbsp;&nbsp;&nbsp;macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/5/6/56859beea25faae80ebdad3a719365dcc45a5b5a.kmmacros">Get Hyperlink Under Mouse [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **The the URL of the Hyperlink Under the Mouse**

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Hover the Mouse Over a valid Hyperlink
3. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
.
**Make These Changes to this Macro**
1. Assign a Trigger to this macro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro, and the Macro Group it is in.
.

**REQUIRES:**

1. **KM 9.0+** (may work in KM 8.2+ in some cases)
2. **macOS 10.11.6 (El Capitan)+**

TAGS:  @URL @Hyperlink @Browser @JavaScript

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**==USE AT YOUR OWN RISK==**

* While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.
<img src="/uploads/default/original/3X/a/d/addd849ce00f8c76f3a908a4cd938e7b71c047c7.png" width="522" height="835">
3 Likes