用applescript获取当前鼠标坐标并且用鼠标左键在当前坐标点击
- User
用applescript获取当前鼠标坐标并且用鼠标左键在当前坐标点击
- Assistant
下面是一个使用AppleScript获取当前鼠标坐标并点击的示例代码:
applescript-- 使用AppleScript获取当前鼠标坐标 tell application "System Events" set mouse_pos to position of the mouse end tell set x_pos to item 1 of mouse_pos set y_pos to item 2 of mouse_pos -- 使用AppleScript模拟鼠标点击 tell application "System Events" -- 移动鼠标到指定位置 set mouse_pos to {x_pos, y_pos} tell process "SystemUIServer" click at mouse_pos end tell end tell请注意,使用上述代码需要先在“系统偏好设置->辅助功能->启用访问辅助设备”中将“AppleScript编辑器”添加到“允许以下程序控制您的计算机”列表中。
此外,在进行上述操作时,请确保当前鼠标位置没有移动,否则可能无法正确定位到目标位置。
- User
用applescript获取当前鼠标坐标并且用鼠标左键点击
- Assistant
抱歉,AppleScript无法直接获取当前鼠标位置,但可以借助使用Objective-C来实现。下面是一个使用AppleScript和Objective-C混合的示例代码,它可以获取当前鼠标位置并模拟鼠标左键点击:
applescript-- 使用AppleScript和Objective-C混合代码获取当前鼠标位置 set objcScript to " set mousePos to current application's NSEvent's mouseLocation() -- 获取当前鼠标位置 return {item 1 of mousePos, item 2 of mousePos} " -- 运行Objective-C代码并获取鼠标位置 set mousePos to do shell script "/usr/bin/osascript -e " & quoted form of objcScript -- 解析鼠标位置 set {mouseX, mouseY} to paragraphs of mousePos -- 使用AppleScript模拟鼠标左键点击 tell application "System Events" tell process "SystemUIServer" click at {mouseX, mouseY} end tell end tell请注意在上述代码中,我们使用了Objective-C的
NSEvent类来获取鼠标位置,并通过do shell script命令运行Objective-C代码。请确保你的系统支持Objective-C,并将Objective-C代码存储在一个独立的文件中,然后在AppleScript中引用这个文件。希望以上内容能帮到你!如果还有其他问题,请随时提问。
- User
applescript获取当前光标坐标
- Assistant
抱歉,AppleScript本身无法直接获取当前光标坐标。但是,你可以使用一个小的Objective-C脚本来辅助获取光标的位置。以下是一个示例代码:
applescript-- 使用Objective-C获取当前光标位置 set objcScript to " #import <Foundation/Foundation.h> #import <ApplicationServices/ApplicationServices.h> NSPoint mouseLocation() { NSPoint point = [NSEvent mouseLocation]; CGPoint cgPoint; cgPoint.x = point.x; cgPoint.y = point.y; return cgPoint; } NSString *getMouseLocation() { NSPoint point = mouseLocation(); NSString *location = [NSString stringWithFormat:@\"%f,%f\", point.x, point.y]; return location; } getMouseLocation(); " -- 运行Objective-C代码并获取光标位置 set cursorPos to do shell script "/usr/bin/clang -framework Foundation -framework ApplicationServices -x objective-c -o /tmp/cursorPos - <<EOF " & objcScript & " EOF /tmp/cursorPos" -- 解析光标位置 set AppleScript's text item delimiters to "," set {cursorX, cursorY} to text items of cursorPos cursorX as number cursorY as number