How can I automate mouse clicks and keyboard input with Automator in Mac Mojave?

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?


Solution 1:

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

Solution 2:

Yes, you can use Automator -> Utilities -> Watch Me Do and record mouse and keyboard actions and then add an Automator -> Utilities -> Loop and configure it to loop however many times you like.