macos 如何通过 mac 终端模拟鼠标点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4230867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I simulate a mouse click through the mac terminal?
提问by chesspro
Is there any way to do this without any programs or scripts? (Maybe through osascript?)
有没有办法在没有任何程序或脚本的情况下做到这一点?(也许通过 osascript?)
回答by davidcondrey
You can automate a mouse click using Applescript.
您可以使用 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
如果您想在浏览器窗口中单击,您可以在 Javascript 的帮助下使用 Applescript
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
纯粹通过终端,您可以使用名称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;
}
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, hereand John Dorian provided a great solution written in Java, here
注意:Jardel Weyrich 提供了一个更全面的代码示例,这里和 John Dorian 提供了一个用 Java 编写的很好的解决方案,这里
回答by JShoe
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
嘿。我有同样的问题,我不确定这是否是您要找的,但它可以让您将鼠标移动到屏幕上的给定坐标并执行单击。不过你需要情报,我不需要,所以我无法使用它,但我会给你任何你可能需要的帮助。链接:http: //hints.macworld.com/article.php?story=2008051406323031