如何通过mac终端模拟鼠标点击?

时间:2010-11-20 02:19:43

标签: macos terminal osascript

有没有办法在没有任何程序或脚本的情况下执行此操作? (也许通过osascript?)

2 个答案:

答案 0 :(得分:33)

您可以使用Applescript自动鼠标点击。

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

如果您想在浏览器窗口中单击,可以在Javascript

的帮助下使用Applescript
tell application "safari"
    activate
    do JavaScript "document.getElementById('element').click();"
end tell

纯粹通过终端,您可以创建名称为click.m的文本文件或您喜欢的任何名称,并使用以下代码保存

// 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;
}

然后按照指示编译:

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

并将其移至相应的系统文件夹以方便

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

现在您可以运行一个简单的终端命令来操作鼠标

click -x [coord] -y [coord]

注意:Jardel Weyrich提供了一个更全面的代码示例,here和John Dorian提供了一个用Java编写的出色解决方案,here

答案 1 :(得分:1)

嘿。 我有同样的问题,我不确定这是否是您正在寻找的,但它会让您将鼠标移动到屏幕上的给定坐标并执行单击。你需要英特尔,我没有,所以我无法使用它,但我会给你任何你可能需要的帮助。链接:http://hints.macworld.com/article.php?story=2008051406323031

相关问题