【问题标题】:How do I simulate a mouse click through the mac terminal?如何通过mac终端模拟鼠标点击?
【发布时间】:2010-11-20 02:19:42
【问题描述】:

有没有任何方法可以在没有任何程序或脚本的情况下做到这一点? (也许通过 osascript?)

【问题讨论】:

    标签: macos terminal osascript


    【解决方案1】:

    您可以使用 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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 2022-11-18
        • 2019-12-10
        • 2011-09-03
        • 2013-10-22
        相关资源
        最近更新 更多