如何通过 mac 终端模拟鼠标点击? [英] How do I simulate a mouse click through the mac terminal?
问题描述
没有任何程序或脚本,有没有办法做到这一点?(也许通过 osascript?)
Is there any way to do this without any programs or scripts? (Maybe through osascript?)
推荐答案
您可以使用 Applescript 自动点击鼠标.
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
<小时>
如果你想在浏览器窗口中点击,你可以在 Javascript 的帮助下使用 Applescript
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
<小时>
纯粹通过终端,您可以创建一个名为 click.m
或任何您喜欢的名称的文本文件,使用以下代码保存
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;
}
然后按照说明编译:
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]
<小时>
注意:Jardel Weyrich 提供了更详尽的代码示例,此处和 John Dorian 提供了用 Java 编写的出色解决方案,此处
note: a more thorough code example has been provided by Jardel Weyrich, here and John Dorian provided a great solution written in Java, here
这篇关于如何通过 mac 终端模拟鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!