如何使用 AppleScript 单击屏幕? [英] How can you click the Screen using AppleScript?

查看:75
本文介绍了如何使用 AppleScript 单击屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用测试脚本测试 click at 命令,但出现错误 -25200,并且在我查看的所有地方,我都不明白为什么会出现此错误.

属性 mousex : 1鼠标属性:1属性屏幕x:2559属性筛选:1599告诉应用程序系统事件"重复100次点击 {mousex mod screenx, mousey mod screeny}将 mousex 设置为 mousex + 100将鼠标设置为鼠标 + 100延迟 0.2结束重复结束告诉结束运行

解决方案

在这种情况下使用 click at 不是正确的方法.

<小时>

以上代码在 13 英寸 MacBook Air 2019 的 macOS 11.4 Beta 中测试

<小时>

正如 user3439894 所说:

<块引用>

还有:cliclick

我决定重写脚本以使用 cliclick,因为 cliclick 看起来更现代,速度也更快:

属性 screenX : 1439属性 screenY : 899用 mousePos 从 1 到 100 重复做shell脚本/usr/local/bin/cliclick c:"&((mousePos * 100) mod screenX) &,"&((mousePos * 100) mod screenY)延迟 0.1结束重复结束运行

这与上述脚本的工作方式完全相同.如您所见,屏幕大小仍然与使用 MouseTools 时相同,仍然不知道为什么会这样.如果您想通过 cliclick 获取位置,请使用以下脚本:

重复10次将 mousePos 设置为执行 shell 脚本/usr/local/bin/cliclick p:."显示通知 mousePos延迟 1结束重复

<小时>

以上代码在 13 英寸 MacBook Air 2019 的 macOS 11.4 Beta 中测试

<小时>

cliclick 和 MouseTools 都可以使用 Homebrew 安装,输入命令 brew install cliclickbrew install mousetools 在终端中.

So I tried to test the click at command using a test script and I got the error -25200 and everywhere I looked, I don't see why I got this error.

property mousex : 1
property mousey : 1
property screenx: 2559
property screeny: 1599

tell application "System Events"



    repeat 100 times
         click at {mousex mod screenx, mousey mod screeny}
         set mousex to mousex + 100
         set mousey to mousey + 100
         delay 0.2
     end repeat

end tell
end run

解决方案

Using click at is not the right approach in this situation. Something like MouseTools would fit your needs better. The correct way of doing this would be:

property screenX : 1439
property screenY : 899

repeat with mousePos from 1 to 100
    do shell script "/usr/local/bin/mousetools -x " & ((mousePos * 100) mod screenX) & " -y " & ((mousePos * 100) mod screenY) & " -leftClick"
    delay 0.1
end repeat
end run

Note that I made a few changes: I don't think having two variables with the exact same value at all times is efficient so I changed both of them to mousePos. But as you can see, there is no set mousePos to mousePos + 100 but instead I used repeat with. Repeat with automatically increases the variable by one every time it loops, so I had to add the * 100. Also, the screenX and screenY values are changed when using MouseTools, you can get your screen size by running this script and moving your mouse to the bottom right:

repeat 10 times
    set mousePos to do shell script "/usr/local/bin/mousetools -location"
    display notification mousePos
    delay 1
end repeat

The end result when running this code in Affinity Photo, a similar product to Photoshop, which I saw you were planning to use it for:


The code above was tested in macOS 11.4 Beta on a 13-inch MacBook Air 2019


EDIT: As user3439894 said:

There is also: cliclick

I decided to rewrite the script to use cliclick because cliclick seems more modern and also faster:

property screenX : 1439
property screenY : 899
repeat with mousePos from 1 to 100
    do shell script "/usr/local/bin/cliclick c:" & ((mousePos * 100) mod screenX) & "," & ((mousePos * 100) mod screenY)
    delay 0.1
end repeat
end run

This works the exact same way as the script above. As you can see, the screen size is still the same as when using MouseTools, still no idea why that is. If you want to get the location with cliclick, use this script:

repeat 10 times
    set mousePos to do shell script "/usr/local/bin/cliclick p:."
    display notification mousePos
    delay 1
end repeat


The code above was tested in macOS 11.4 Beta on a 13-inch MacBook Air 2019


Both cliclick and MouseTools can be installed using Homebrew by typing the command brew install cliclick or brew install mousetools in the Terminal.

这篇关于如何使用 AppleScript 单击屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆