Friday, November 15, 2024

macos – Can holding down a mouse button be automated?

If you can use the PageDown key to scroll after clicking within the JS Frame, this simple AppleScript should work. Hopefully the code is self-explanatory – if not I would suggest to follow an online tutorial on scripting.

set numPgEnds to text returned of (display dialog "Enter number of page end presses." default answer "20" with icon note buttons {"Cancel", "Continue"} default button "Continue")
set delayLength to text returned of (display dialog "Enter delay between page end presses." default answer "5" with icon note buttons {"Cancel", "Continue"} default button "Continue")
set n to 0
delay 2
repeat while n < numPgEnds
    tell application "System Events" to key code 119
    delay delayLength
    set n to n + 1
end repeat

I also used a piece of freeware called MouseTools to automate clicking. It’s no longer available from the original company (or their web domain now belongs so some other company), but there are GitHub repositories with the same name which might fit the bill.

The version I had was controlled via bash/zsh (i.e. Terminal) commands, which I ran via AppleScript. Here’s an example AppleScript function that clicks once at a particular location on screen. The theSwitches paramater was used to simulate holding down modifier keys such as Shift and Command.

on click(mouseToolsPath, x, y, theSwitches)
    do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & theSwitches
end click

I was able to get the location of the mouse pointer after a short delay of starting the script and then plug that into the above function, repeating with loops as necessary.

Related Articles

Latest Articles