How to automate mouse clicks and keyboard input with Automator in Mac Mojave

applescriptautomator

If I position my mouse cursor at a certain point on the screen, is it possible to use the Automator or AppleScript to "click" the mouse, enter a keyboard shortcut (say, command-5), press the return key, and then repeat those three things n times?

Best Answer

This code requires the third-party utility, Cliclick.

“Cliclick” is short for “Command-Line Interface Click”. It is a a tiny shell/Terminal application that will emulate mouse clicks or series of mouse clicks (including doubleclicks and control-clicks) at arbitrary screen coordinates. Moreover, it lets you move the mouse, get the current mouse coordinates, press modifier keys etc.

It's free to download but it's donationware, and is easy to install.

On my system I have. cliclick in the following directory: /usr/local/bin/. Because of this location, in my AppleScript code and in Terminal app, I need to use the full path to cliclick to call the command. For example: do shell script "/usr/local/bin/cliclick c:." In AppleScript it's telling cliclick to click at current mouse location.

This AppleScript code works for me using the latest version of macOS Mojave.

repeat 3 times -- Enter Your Desired Loop Count
    -- Clicks At Current Mouse Location
    do shell script "/usr/local/bin/cliclick  c:."

    tell application "System Events"
        delay 0.2 -- Adjust Value If Necessary
        keystroke 5 using {command down}
        delay 0.2 -- Adjust Value If Necessary
        keystroke return
    end tell
end repeat