8

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?

5
  • 2
    Usually in Applescript, if you find yourself wanting to do something like this, you are doing it wrong.
    – Glen Yates
    Apr 29, 2019 at 19:33
  • I'm trying to take a lot of consecutive screenshots. I can't think of a better way... Apr 30, 2019 at 14:14
  • Are you trying to capture just a specific window, or the whole screen? I don't know about the new screenshot feature of Mojave, but you can install the old Grab utility (which is directly scriptable) back into Mojave. This would give you the ability to specify specific windows to capture.
    – Glen Yates
    Apr 30, 2019 at 15:02
  • 1
    "I can't think of a better way..." No, but that's why you're on StackExchange with thousands of programmers who possibly can think of a better way. You'll get better help if you describe what you want to achieve as your end goal (taking a lot of screenshots... how? clicking what button? using what app? one window, whole screen, a fixed area?) and letting people on this site tell you the best way to go about doing it.
    – CJK
    May 1, 2019 at 16:37
  • @hamilton.julius Instead of taking a bunch of screen shots, it might be easier to record your screen with Quicktime. Alternatively, you could use Automator to record your clicks, then edit the workflow and add in a screenshot action after each mouse click.
    – TimD
    May 2, 2020 at 6:43

2 Answers 2

5

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.

4

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .