2

I want to right click a regular link in (primary) Chrome, Safari (secondary) or Firefox/Tor Browser (tertiary) and then send it somewhere else using Applescript. How do I do this?

I could imagine that I first use Javascript to get the actual link and then return it as text to an Applescript for further processing. Does it make sense? If so, I would appreciate some hints about how to get going.

d-b
  • 695
  • 3
  • 14
  • 43

1 Answers1

3

You can do this by way of an Automator service, which will add a contextual (right-click) menu item when you click on a link in Safari (or Chrome).

Read the Mac Automation Scripting Guide: Making a Systemwide Service, which has a step-by-step guide on how to create an Automator service.

It will look something like this during construction:

macOS Automator Workflow

This service, as you can see, is active only in Safari when there is a block of text selected. Unfortunately, you can't limit it to just hyperlinks, but it does include them.

If the service is available (i.e. you are in Safari and have selected some text), the menu item corresponding to the service appears in the right-click submenu called "Services", and it'll be named according to what you saved the workflow as in Automator. In my case, the service is named "Process URL".

When clicked, it executes the AppleScript you can see in the workflow (it does this in the background, invisibly).

This is where your suggestion of using JavaScript was a good one to employ: the AppleScript executes a bit of JavaScript in Safari (provided you have the right permissions enabled for it to do so, ticked in Safari's "Develop" menu).

This JavaScript returns one of two results:

▸ If the text selected is plain text with no hyperlink underneath it, it returns an empty string;

▸ If the text selected is hyperlink text, it returns the hyperlinked URL.

It stores this in the variable href, which you can then do whatever you want with.

The good thing about hyperlinks is that you don't have to click and drag to select the text (although you can if you wish, and only part of the text needs to be selected). Instead, you can just right-click on a link, and it automatically selects it for you, making it ideal to activate the service is this manner.

CJK
  • 5,732
  • 1
  • 8
  • 26
  • Thank you. I am almost there now. However, two problems (1) This service does not show up in the context menu in Safari when clicking a link. A bunch of other services are listed there, including some I have created myself. However, it is listed under the Apple menu -> Services -> General (not Safari, and it doesn't matter if I set it to any application or just Safari, it is always under General). Why? – d-b Jun 16 '18 at 09:25
  • (2) Furthermore, it doesn't work…If I run the script from Script Editor with a link selected in Safari it works as expected. I can also run the Javascript from the console in Safari successfully but when running the work flow from the service menu in the Apple menu I get an error message "There was a problem with the input to the Service." Do you know why? – d-b Jun 16 '18 at 09:28
  • https://imgur.com/a/vNsXOta I added on run, return input, end run as a test if that would make it appear in the service menu. – d-b Jun 16 '18 at 11:29
  • 1
    You don’t need the `on run...end run` explicit handler, nor the `return input`. But they also don’t do any harm. The workflow looked fine. If restarting your computer doesn’t resolve the issue, start from scratch and make a service for, say, _Chrome_ instead to see if that appears in the menu. Also, if you go to _System Preferences > Keyboard > Shortcuts > Services_ you can see where your service appears in the category listings. Mine appears under “Text”, as expected. [**See here too**](https://apple.stackexchange.com/a/226508/266364) – CJK Jun 16 '18 at 11:56
  • Will try on my second computer soon. – d-b Jun 16 '18 at 22:54