我正在尝试编写一个快速按下“w”和“q”键的脚本(目的是在游戏练习模式中执行重复活动)。
我对Applescripts几乎一无所知,但我能够将以下哪种方式完成工作:
tell application "System Events"
delay 5
repeat with i from 1 to 50
delay 0.25
keystroke "w"
delay 0.25
keystroke "q"
end repeat
end tell
基本上我按下“run”,alt-tab到游戏,让脚本运行。
问题是我无法强制退出脚本。我尝试了here建议使用命令+选项+转义,但我无法点击“强制退出”并且它不会关闭。
我正在查看handlers的文档,但我不确定我应该做什么。我不想关闭游戏应用程序,只需停止脚本。
理想情况下,我可以实现某种杀死Applescript的“on key pressed”监听器。
编辑 以及我现在的评论中的建议:
use sys : application "System Events"
use framework "Cocoa"
tell application "System Events"
delay 5
repeat with i from 1 to 50
delay 0.25
keystroke "w"
delay 0.25
keystroke "q"
if modifierKeydown() then exit repeat # Exit on keypresswq
end repeat
end tell
# Returns true if any of
# { function, control, option, command }
# are depressed; false otherwise
on modifierKeydown()
set __m to current application's ¬
NSEvent's modifierFlags() as any
return (__m > 262143)
end modifierKeydown
这引起了:
错误“系统事件出错:无法继续modifierKeydown。”数字-1708
答案 0 :(得分:1)
使用my answer来解决类似问题,这是针对您的具体情况的半定制解决方案:
use sys : application "System Events"
use framework "Cocoa"
# Bring your game window into focus
tell application "The Name of Your Game" to activate
# Might be needed if your game window takes a long time to
# enter focus. But if it's already running, it should be instant
# and this little loop won't be needed.
# repeat while process "The Name of Your Game" is not frontmost
# end
# Get frontmost window of the frontmost application
set P to a reference to (the first process whose frontmost is true)
set W to the front window of P
repeat with i from 1 to 50
# Terminate loop if the focus changes to another window
if the front window of P is not equal to W then exit repeat
delay 0.25
tell sys to keystroke "w"
delay 0.25
tell sys to keystroke "q"
if modifierKeydown() then exit repeat # Terminate loop if modifier key is pressed
end repeat
return
# Returns true if any of
# { function, control, option, command }
# are depressed; false otherwise
on modifierKeydown()
set __m to current application's ¬
NSEvent's modifierFlags() as any
return (__m > 262143)
end modifierKeydown