为什么这个脚本只有在我使用延迟时才能工作?

时间:2012-10-28 14:02:30

标签: macos applescript delay

我正在编写一个小脚本来自动化注册扑克锦标赛的过程,同时播放其他几个牌桌。 大厅窗口在后台,在前台有其他桌子在播放时弹出。因此,为了注册,我需要在前台设置大厅(ctrl + 1)并阻止应用程序弹出其他表(我已经使用了重复循环来执行此操作,但我不确定它是否是最佳方式)。

我无法理解为什么,当我把延迟0.5或0.2时,脚本按预期工作,否则如果我没有它们运行脚本它不起作用(不点击大厅中的注册按钮)。

这是至关重要的,因为在那些0.5秒内可能会有其他表格弹出并从主要大厅偷走焦点。请注意,我使用像素坐标点击按钮,因为它是非Cocoa应用程序,并且不允许我轻松点击按钮。

我该如何解决这个问题?

--REGISTRATION PROCESS
--make sure we complete the reg process without other tables popping up and stealing focus from the tourney reg window
set done to false
repeat until done is true
    delay 0.5
    --Lobby focused
    tell application "System Events"
        set frontmost of process "PokerStarsIT" to true
        keystroke "1" using command down
        delay 0.2
    end tell

    --Start the registration process: click "register" button.
    tell application "PokerStarsIT"
        do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (xCoordinate as text) & " -y " & (yCoordinate as text) & " -rightClick"
        set done to true                
    end tell
end repeat

1 个答案:

答案 0 :(得分:1)

我会这样试试。请注意,我使用窗口1的名称作为“Lobby”。确保输入窗口的实际名称,即那里的Lobby窗口。您还会注意到“do shell script”命令不在PokerStarsIT应用程序告诉代码块中。 “do shell script”是一个applescript命令,而不是PokerStarsIT命令。另外我认为你应该做“-leftClick”而不是“-rightClick”。祝你好运。

tell application "PokerStarsIT" to activate
tell application "System Events"
    tell process "PokerStarsIT"
        repeat
            keystroke "1" using command down
            if name of window 1 is "Lobby" then exit repeat
        end repeat
    end tell
end tell

--Start the registration process: click "register" button.
do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (xCoordinate as text) & " -y " & (yCoordinate as text) & " -leftClick"