开发者

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

开发者 https://www.devze.com 2023-01-26 15:11 出处:网络
Is there any way to do this without any programs or scripts? (Maybe through osascri开发者_运维百科pt?)You can automate a mouse click using Applescript.

Is there any way to do this without any programs or scripts? (Maybe through osascri开发者_运维百科pt?)


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


Hey. I had this same question, and I'm not sure if this is what you're looking for, but it will let you move the mouse to a given coordinate on the screen and execute a click. You need intel though, which I don't, so I wasn't able to use it, but I'll give you any help you might need. Link: http://hints.macworld.com/article.php?story=2008051406323031

0

精彩评论

暂无评论...
验证码 换一张
取 消