MacOS – AppleScript: How to double-click & right-click at cursor location


I want to be able to double-left-click, then immediately right-click using AppleScript.

The location of the click is wherever my cursor is located at the time that the AppleScript is triggered.

The AppleScript will be triggered by FastScripts via keyboard shortcut. I would like the script to work across all applications.

I installed cliclick. When I type cliclick -h into Terminal, I see that it can double-click, but there is no mention of the right-click.

Best Answer

Since you already know how to double-click with cliclick, my answer focuses on the right-click and traditionally on a Mac one used to preform a right-click via control-click, and as such you need to do the same thing when using cliclick.

As an example, using cliclick in Terminal, it will attempt to open the manual page for cliclick. I said attempt to open because cliclick doesn't have a manual page but will use the command line to preform some actions that will control-click (right-click) to bring up the context menu, move the mouse to click Open man page as I've selected cliclick in the command line before pressing enter to execute the command line.

The image below shows the command line twice just so you can plainly see the full command line in the first line as part is obfuscated in the second line that's preforming the actual clicking on the
Open man page from the context menu.

enter image description here

The image below is what appeared and was clicked when the cliclick command line was executed.

enter image description here

The image below is cropped but is presented to show it tried to open the manual page for cliclick.

enter image description here

I do not use FastScripts, however the example command should help you to understand what to pass cliclick to do whatever it is you're trying to do.

The cliclick command line explained:

  • cliclick kd:ctrl c:+0,+0 ku:ctrl m:+5,+5 c:+0,+0 - Full command line.
  • cliclick - The binary executable.
  • kd:ctrl - Press the control key down.
  • c:+0,+0 - Click relative to where the mouse is presently.
  • ku:ctrl - Let the control key up.
  • m:+5,+5 - Move the mouse five pixels on the x,y axis relative to where the mouse is presently.
  • c:+0,+0 - Click relative to where the mouse is presently.

  • Note: The full command line above can also be expressed as,
    cliclick kd:ctrl c:. ku:ctrl m:+5,+5 c:. since a . can be used in place of x,y axis coordinates e.g., +0,+0 for the relative position of the mouse. I personally prefer to use the former over the latter, however both forms work.

As you can see in the first image that cliclick was selected and the mouse, show as a text cursor, is right over the selection, then when the cliclick commands were executed, it opened the man page Terminal window for it, although these isn't one. However, it shows that a right-click was made and the mouse moved to the entry on the context-menu and clicked.

In AppleScript the above command line would be wrapped in a do shell script command as shown below:

do shell script "/usr/local/bin/cliclick kd:ctrl c:+0,+0 ku:ctrl m:+5,+5 c:+0,+0"

Note: Use the fully qualified pathname to the cliclick binary executable if it's in a location that is not in the PATH passed to AppleScript.