Script to open Safari link in new tab

I use a Wacom tablet as my trackpad. I am suffering from a condition called lazy arm. Lately, instead of putting my left hand on the keyboard and pushing command while clicking a link, I have been letting my right hand do all the work by tapping a right click and using the contextual menu to open link in new tab. I could just use the keyboard, but there’s no fun in that.

My tablet has an option for a 3 finger tap. This tap can do a variety of things, but none of them are a middle click. What I do have is, the option to open/run. I was hoping to be able to write a simple script to perform CMD-click. Seems easy enough, but I have searched high and low here and on the web, and I can’t find anything that will work! The only options I found would just perform this with specific coordinates. Everything else I found is related to playing games and just doesn’t apply. Found some references to something called Extra Suites that sounded really promising, only to find the developer stopped the project almost 15 years ago!

I am running Mac 10.12.3. Does anyone know of any way to script command-click? Any tips would be greatly appreciated. I have very little experience writing scripts, but this isn’t my first rodeo. I am willing to work at it so I can continue to be lazy. I just though it sounded so easy, why not. Now I have invested way to much time into it to not figure it out.

Model: iMac
AppleScript: Version 2.9 (191). AppleScript 2.5
Browser: Safari 10.0.3 (12602.4.8)
Operating System: Mac OS X (10.10)

While the scripting tools to control the mouse do work based on location, they can also retrieve the current location.

http://www.hamsoftengineering.com/codeSharing/MouseTools/MouseTools.html

Here are a few bits from a script I had using MouseTools as reference:

set mouseToolsPath to (path to home folder as text) & "MouseTools"

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (mouseX as text) & " -y " & (mouseY as text) & " -leftClick"
do shell script quoted form of POSIX path of mouseToolsPath & " -releaseMouse"

So you should be able to assign an Applescript to three-finger tap, have that script call MouseTools via Do Shell Script to return the current mouse location [-location], then do a right click at that location [-rightClick].

Maybe you don’t even need to get and set the location? Maybe you can just use MouseTools to call right click and it operates where the mouse is?

Seems like this should be doable. Best of luck, let me know how it goes.

Thanks so much for the reply here. I had some problems with the new iMac. Had to return it and then took a lot of time deciding on if I wanted to get another one or wait for something better. Well, I am back in action with another one now, so I am back at this.

So I have tried to start using mousetools, which seems to be able to do exactly what I want. But I am sort of stuck here. It’s been a long while since I worked with AS, and clearly, what I learned before didn’t stick. What I have so far is this. I am not sure if I am supposed to be putting in the commands one after another like this, or like in your example, if each action should be its own do shell command.

do shell script quoted form of POSIX path of pathToMouseTools & " -location" & " -commandKey" & " -leftClick"

This seems to just perform a left click, so I assuming that I have started incorrectly. But I also tried these, which don’t work either!

do shell script quoted form of POSIX path of pathToMouseTools & " -commandKey"
do shell script quoted form of POSIX path of pathToMouseTools & " -location" & " -leftClick"
do shell script quoted form of POSIX path of pathToMouseTools & " -location"
do shell script quoted form of POSIX path of pathToMouseTools & " -commandKey"
do shell script quoted form of POSIX path of pathToMouseTools & " -leftClick"

I can’t wrap my head around how I am supposed to compile this. Am I at least on the right path? (no pun intended)

Hmmm… good question.

Looking at the documentation, I’d have thought this would be sufficient to command + left-click at the current mouse position:

set mouseToolsPath to (path to home folder as text) & "MouseTools"
do shell script quoted form of POSIX path of mouseToolsPath & " -commandKey -leftClick"

But all I’m getting is a left click.

And oddly, this:

set mouseToolsPath to (path to home folder as text) & "MouseTools"
do shell script quoted form of POSIX path of mouseToolsPath & " -leftClick"

Is doing nothing…

These two items from the documentation certainly make it sound like this should be working:

This hack is working for me, but it’s not ideal:

set mouseToolsPath to (path to home folder as text) & "MouseTools"

tell application "System Events"
	key down command
end tell
do shell script quoted form of POSIX path of mouseToolsPath & " -commandKey -leftClick"
tell application "System Events"
	key up command
end tell

Among other things, I think “key down” and “key up” are deprecated, although they’re still functional in Sierra.

I feel like something’s wrong with MouseTools, it certainly doesn’t appear to be performing according to the documentation for us. Maybe someone else here sees the error? You might want to shoot HAMSoft (the MouseTools developer) a message and see if they have any input.

  • Tom.

Also, FYI, if you mess with the System Events “key down” command, the first thing I’d do is open a new script window with:

tell application "System Events" to key up command

and leave it to the side, because if for any reason the script stops before executing the “key up command” line, it just leaves command down permanently, which can be hard to fix because you can’t do much of anything while it’s down.

But you can click on the window with the script above and click the “run” icon in it to get back to normal.

  • Tom.

At least I feel a little better knowing it’s not just me. I tried every possible combination I could think of! And still was just trying more when this reply came in.

I have not yet tried your little hack…your warning message just scared me a bit!

Furthermore, I have noticed another rookie problem. I am not able to properly define the path to mousetools. I used

set mouseToolsPath to "HD:Users:myusername:" & "MouseTools"

I always get:
sh: /HD/Users/myusername/MouseTools: No such file or directory

I had to use this to make it work

try
	pathToMouseTools
on error
	set pathToMouseTools to choose file
end try

So I will email Hank and see if he can shed some light on the problem here. Hopefully this is something that can be fixed!

I tried this, which is directly from his provided examples, and it doesn’t work either:

set mouseToolsPath to (path to home folder as text) & "MouseTools"
set {mouseX, mouseY} to paragraphs of (do shell script quoted form of POSIX path of mouseToolsPath & " -location")
do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (mouseX as text) & " -y " & (mouseY as text) & " -leftClick -shiftKey"

Which makes me think there’s a definite problem with functionality.

I wondered if it’s a system protection. I didn’t see a way to add MouseTools itself under “Accessibility” in the “Security and Privacy” System Preferences to allow it to control the computer, but I did add Terminal and that made no difference… of course, “do shell script” has nothing to do with Terminal.app, they run directly. But I wonder if somehow getting MouseTools whitelisted for the “Allow the apps below to control your computer” section isn’t the issue.

Also, with regard to “key down,” by “permanently” I don’t mean, like, you have to throw the computer out and buy a new one. Logging off and back on should do it. Or keeping the other script open and running it also fixes the problem.

If you installed Mousetools to your home folder, then just out of curiosity, why enter the full path instead of just taking what I did?

set mouseToolsPath to (path to home folder as text) & "MouseTools"

Haha, I knew what you meant! I could just picture it happening and being troublesome, that’s all. Throw out the computer, oh my gosh, so funny! So really, I just have to throw out the keyboard if the command gets permanently down, right?

Looking at your script you wrote, you still have mousetools commandKey, assuming if you tried it, it worked fine since that doesn’t seem to be working anyway.

Regarding the path to mousetools. Silly me, I thought I had to put the path there. Your way works fine. But now that it works fine that way, why didn’t it work when I put the actual path?

Ok, so I tried your technique and it worked fine. As far as I’m concerned, problem solved. Thanks so much for that, simple little tip did the trick. Will wait for a response from Hamsoft to see if it might work that way later.

While fiddling, I found another solution that works. Found this automator plugin called automator mouse click. Unfortunately, it’s not free, but it works. I just used it to generate a middle click, and made it a service for Safari with Automator. Then, I assigned it a keyboard shortcut in prefs and associated that keyboard shortcut with my gesture on my Wacom tablet. It does exactly what I wanted. It is a trial version with limited usage before requiring registration. I didn’t use it enough to hit the limit, but it did work fine. It’s a clean solution, but not really worth paying for unless I have other uses for it.

I will stick with your solution for now. Thanks so much!

You may use Cliclick available for free at :
http://www.bluem.net/en/mac/cliclick/

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) samedi 13 mai 2017 10:47:44

Yvan
Thanks. Since vegas is considering it solved for now, I don’t think I’ll mess with a ClickClick solution, but it’s good to know it’s out there since MouseTools seems to be misbehaving.

vegas

I still had the “-commandKey” argument on MouseTools even though I was using System Events for [command down] because in my testing, MouseTools wasn’t doing anything when I just used

do shell script quoted form of POSIX path of mouseToolsPath & " -leftClick"

and when I used

do shell script quoted form of POSIX path of mouseToolsPath & " -commandKey -leftClick"

I was getting just a left click.

I hate to even ask this, but when you paste in

set mouseToolsPath to "HD:Users:myusername:" & "MouseTools"

I’m assuming that where it says “myusername” you’re substituting that with your actual username? (Unless your username is “myusername.” That would be funny.)

Assuming you are putting your actual username in there, you can edit the script below with your actual username, and it should reveal to you the error in what you were doing.

set mouseToolsPath1 to (path to home folder as text) & "MouseTools"
set mouseToolsPath2 to "HD:Users:myusername:" & "MouseTools"

display dialog "The working path is:" & return & mouseToolsPath1 & return & "The non-working path is:" & return & mouseToolsPath2

I suggest doing it this way because there’s no inherent reason what you did can’t work, I think you just have an error in your path. Did you use “HD” when your drive is called “Macintosh HD?” Do you have a typo in your username? Hard for me to tell from here, but the script should reveal your error.

  • Tom.

Yvan,

Thanks for posting. If I have some free time, I might check it out. But as t. said, since it’s solved already, I probably won’t be investigating any other solutions unless my current one fails, or I need another similar solution later. But I will at least check it out and save it for later. Someone else coming along might benefit from your post, so I’m glad you made it here.

t.,
Yes, you’re right. I too tried it both ways and realized afterward that it only worked like that as well. So, for whatever reason, it’s working now well enough!

On the file path, no my username isn’t myusername! Nor is my hard drive HD, but I was very careful to make sure there were no typos anywhere. I will check out the little addition you added to check for errors, that will come in really handy. Thanks for sharing that.

Great guys, your work here is done! I will report back if I hear anything from my email to Hamsoft.

Ok Yvan, I give in! I jut set up a new OS install tonight and figured that since I had to set up the solution I used before all over again, it was a perfect time to give cliclick a try. As soon as I opened the link you shared, I instantly recognized it. I recognized it, because I came across it while I was searching for solutions to this very same problem many weeks ago. The problem I had with it was, well…I didn’t have the slightest bit of an idea how to use it!

I am familiar with some basic terminal commands, it ain’t my first rodeo, but don’t expect me to hold on after the gate opens. (sorry if you don’t get the reference, what I’m trying to say is, I can do this if you help me a little!) I have cliclick installed in usr/bin, which is, by default in my $path variable, no problem there. I also understand the commands provided. I see that I need to use “.” to make a click on the point of my mouse. “kd:cmd” to make the command key down and “ku:cmd” to release the command key after the click. What I don’t understand is, how do I call upon this utility in my script? I am sure it will make total sense once I see an example, but the readme doesn’t provide much info on how to use it. I don’t need you to make this for me…unless you want to. :-). But if you could at least give me an example of how I would implement this, it would help me out a lot. Just as I found a month ago, cliclick certainly does seem to be able to do what I want, I just need some help to make it work!

Here are two pieces of code extracted from a large script driving LibreOffice which has no AppleScript support.

property path2cliclick : "/Users/admin/bin/cliclick"

# trigger a row in an open dialog
tell application "System Events" to tell process "soffice"
	set frontmost to true
	tell window 1
		# several instructions triggering several UI elements
		set {xLeft, yTop} to position of row 2
		set {itsWidth, itsHeight} to size of row 2
		tell me to do shell script path2cliclick & " dc:" & xLeft + itsWidth div 2 & "," & yTop + itsHeight div 2
	end tell # window 1
end tell

# click a checkbox in a print dialog
tell application "System Events" to tell process "LibreOffice"
	set frontmost to true
	tell window 1
		# several instructions triggering several UI elements
		tell checkbox 2
			set {xLeft, yTop} to its position
			set {itsWidth, itsHeight} to its size
		end tell
		tell me to do shell script path2cliclick & " c:" & xLeft + itsWidth div 2 & "," & yTop + itsHeight div 2
	end tell # window 1
end tell

I don’t remember where I got these infos :

USAGE
cliclick [-m ] [-f ] [-w ] [-r] command1 [command2] […]

OPTIONS
-r Restore initial mouse location when finished
-m The mode can be either “verbose” (cliclick will print a
description of each action to stdout just before it is
performed) or “test” (cliclick will only print the
description, but not perform the action)
-f Instead of passing commands as arguments, you may instead
specify a file from which cliclick will read the commands.
Each line in the file is expected to contain a command
in the same format/syntax as commands given as arguments
at the shell. Additionally, lines starting with the hash
character # are regarded as comments, i.e.: ignored. Leading
and trailing whitespace is ignored, too.
-w Wait the given number of milliseconds after each event.
If you find that you use the “wait” command too often,
using -w could make things easier. Please note that “wait”
is not affected by -w. This means that invoking
“cliclick -w 200 wait:500” will wait for 700 milliseconds.
The default (and minimum) value for -w is 20.
-V Show cliclick version number and release date
-o Open version history in a browser
-d Send a donation

COMMANDS
To use cliclick, you pass an arbitrary number of commands as arguments. A command consists of a command identifier (a string that tells cliclick what kind of action to perform) and usually one or more arguments to the command, which are separated from the command identifier with a colon. Example: “c:123,456” is the command for clicking (the “c” is the command identifier for clicking) at the position with x coordinate 123 and y coordinate 456. See below for a list of all commands and the arguments they expect.
Whenever a command expects a pair of coordinates, you may provide relative values by prefixing the number with “+” or “-”. For example, “m:+50,+0” will move the mouse 50 pixels to the right. Of course, relative and absolute values can be mixed, and negative values are possible, so “c:100,-20” would be perfectly valid. (If you need to specify absolute negative values in case you have a setup with a second display arranged to the left of your main display, prefix the number with “=”, for instance “c:100,=-200”.)

LIST OF COMMANDS

c:x,y Will CLICK at the point with the given coordinates.
Example: “c:12,34” will click at the point with x coordinate
12 and y coordinate 34. Instead of x and y values, you may
also use “.”, which means: the current position. Using “.” is
equivalent to using relative zero values “c:+0,+0”.

cp:str Will PRINT THE COLOR value at the given screen location.
The color value is printed as three decimal 8-bit values,
representing, in order, red, green, and blue.
Example: “cp:123,456” might print “127 63 0”

dc:x,y Will DOUBLE-CLICK at the point with the given coordinates.
Example: “dc:12,34” will double-click at the point with x
coordinate 12 and y coordinate 34. Instead of x and y values,
you may also use “.”, which means: the current position.

dd:x,y Will press down to START A DRAG at the given coordinates.
Example: “dd:12,34” will press down at the point with x
coordinate 12 and y coordinate 34. Instead of x and y values,
you may also use “.”, which means: the current position.

du:x,y Will release to END A DRAG at the given coordinates.
Example: “du:112,134” will release at the point with x
coordinate 112 and y coordinate 134.

kd:keys Will trigger a KEY DOWN event for a comma-separated list of
modifier keys. Possible keys are:
- alt
- cmd
- ctrl
- fn
- shift
Example: “kd:cmd,alt” will press the command key and the
option key (and will keep them down until you release them
with another command)

kp:key Will emulate PRESSING A KEY (key down + key up). Possible keys are:
- arrow-down
- arrow-left
- arrow-right
- arrow-up
- brightness-down
- brightness-up
- delete
- end
- enter
- esc
- f1
- f2
- f3
- f4
- f5
- f6
- f7
- f8
- f9
- f10
- f11
- f12
- f13
- f14
- f15
- f16
- fwd-delete
- home
- keys-light-down
- keys-light-toggle
- keys-light-up
- mute
- page-down
- page-up
- play-next
- play-pause
- play-previous
- return
- space
- tab
- volume-down
- volume-up
Example: “kp:return” will hit the return key.

ku:keys Will trigger a KEY UP event for a comma-separated list of
modifier keys. Possible keys are:
- alt
- cmd
- ctrl
- fn
- shift
Example: “ku:cmd,ctrl” will release the command key and the
control key (which will only have an effect if you performed
a “key down” before)

m:x,y Will MOVE the mouse to the point with the given coordinates.
Example: “m:12,34” will move the mouse to the point with
x coordinate 12 and y coordinate 34.

p[:str] Will PRINT the given string. If the string is “.”, the current
MOUSE POSITION is printed. As a convenience, you can skip the
string completely and just write “p” to get the current position.
Example: “p:.” or “p” will print the current mouse position
Example: “p:‘Hello world’” will print “Hello world”

tc:x,y Will TRIPLE-CLICK at the point with the given coordinates.
Example: “tc:12,34” will triple-click at the point with x
coordinate 12 and y coordinate 34. Instead of x and y values,
you may also use “.”, which means: the current position.
Note: If you find that this does not work in a target application,
please try if double-clicking plus single-clicking does.

t:text Will TYPE the given TEXT into the frontmost application.
If the text includes space(s), it must be enclosed in quotes.
Example: “type:Test” will type “Test”
Example: “type:‘Viele Grüße’” will type “Viele Grüße”

w:ms Will WAIT/PAUSE for the given number of milliseconds.
Example: “w:500” will pause command execution for half a second

Version 3.2, released 02/17/2016
Author: Carsten Blüm, carsten@bluem.net
List of contributors: https://github.com/BlueM/cliclick/graphs/contributors
Website: www.bluem.net/jump/cliclick/

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) dimanche 14 mai 2017 15:32:26

Ok, so I had to do a complete reinstall of the OS on the new Mac. Just got everything back up and running. I got cliclick installed and managed to do this very simple solution with it. It does work, and it’s free! Thanks, Yvan!

It does work, but there is one problem I have. I am not sure if it is a limitation of the Wacom drivers, or if it’s something I am doing wrong. In order to get it to run, I had to save it as an application. This is fine, but it has one little annoying result. When the application runs, it changes the top window to the application instead of running in the background. So when I go click another link in Chrome (not using Safari anymore) the window is no longer in focus and I have to click it somewhere, adding another necessary click.

Wacom’s documentations says it can run a script, but the documentation is generic for Windows/Mac, and doesn’t really specify what requirements the scripts have. It’s not clear if it must be an application, or if there is a way to just run a plain script. Anything I do other than save as application, just opens up script editor. I tried saving it as run only, but that still opens script editor on run. The easiest solution looks like figuring out how to make it run in background. I did some quick research on this just now, but my brain is so fried that I just got more confused! Any ideas?

Thanks for the feedback.

I don’t have a Wacom tablet to test.

What’s the problem with the script saved as a script?
Is it that it fails or just that you have to open it in the editor.
If it’s the second case, just store the script in the dedicated folder so that you will be able to reach it from the Scripts menu.

The folder is : “/Users/homeFolder/Library/Scripts/”

You may attach a shortcut to such script.
Just use FastScripts by Red Sweater Software available at https://red-sweater.com/fastscripts/
The free version allow you to define 10 shortcuts.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 20 mai 2017 08:57:27

Oooohhh, aaahhh! That works smoothly, and very fast. I don’t like that I need another thing running in my already cramped menu bar, but it works. Not what I originally envisioned, but my problem is solved and it didn’t cost me anything but my time. Learned a few things along the way, so I can’t complain! Thanks Yvan and t.spoon!