Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simulate a mouse click through the mac terminal?

Is there any way to do this without any programs or scripts? (Maybe through osascript?)

like image 615
chesspro Avatar asked Nov 20 '10 02:11

chesspro


People also ask

How do you automate mouse movement?

GS Auto Clicker is a popular tool for automating mouse clicks. It is simple to use and have a simply UI to match. When recording a sequence of clicks, you can select which mouse button (right or left) should be clicked with, and you can program intervals i.e., time that must pass between clicks.


1 Answers

You can automate a mouse click using Applescript.

tell application "System Events"
    tell application process "Application_Name"
        key code 53
        delay 1
        click (click at {1800, 1200})
    end tell
end tell

If you want to click within a browser window you can use Applescript with the help of Javascript

tell application "safari"
    activate
    do JavaScript "document.getElementById('element').click();"
end tell

Purely via terminal, you can create a textfile with the name click.m or whatever name you like, save it with the following code

// File:
// click.m
//
// Compile with:
// gcc -o click click.m -framework ApplicationServices -framework Foundation
//
// Usage:
// ./click -x pixels -y pixels
// At the given coordinates it will click and release.
//
// From http://hints.macworld.com/article.php?story=2008051406323031
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSUserDefaults *args = [NSUserDefaults standardUserDefaults];

    int x = [args integerForKey:@"x"];
    int y = [args integerForKey:@"y"];

    CGPoint pt;
    pt.x = x;
    pt.y = y;

    CGPostMouseEvent( pt, 1, 1, 1 );
    CGPostMouseEvent( pt, 1, 1, 0 );

    [pool release];
    return 0;
}

then compile it as instructed:

gcc -o click click.m -framework ApplicationServices -framework Foundation

and move it to the appropriate system folder for convenience

sudo mv click /usr/bin
sudo chmod +x /usr/bin/click

and now you can run a simple terminal command to manipulate the mouse

click -x [coord] -y [coord]

note: a more thorough code example has been provided by Jardel Weyrich, here and John Dorian provided a great solution written in Java, here

like image 187
davidcondrey Avatar answered Sep 24 '22 08:09

davidcondrey