Applescript检测启用编辑框

时间:2013-02-24 23:30:36

标签: applescript

我的脚本从AppA获取一些文本并将其粘贴到AppB上的文本编辑中。启动AppB时(通过脚本),禁用文本编辑,在用户执行操作时启用。该行动需要保持手动。

脚本在用户有时间做任何事情之前执行,这是一个错误。我的想法是检查是否启用了编辑,但是这会产生此错误。 “无法获取应用程序”系统事件“的<>”AppB“的AppB窗口。它只会抛出一次错误。

如何避免错误?尝试阻止只是吃错误会更好吗?

on idle
 tell application "System Events" to set AppAIsOpen to (application process "AppA" exists)
if (AppAIsOpen) then
  set AppAWasOpen to true
  tell application "AppA"
    set hdg to TxRprt
    set beam to hdg as string
  end tell
  if ((count beam) < 3) then set beam to text -3 thru -1 of ("000" & beam)
    if (beam is not previousText) then
      tell application "AppB" to launch
        tell application "System Events"
          tell application process "AppB"
        if text field 1 of window "AppB" is enabled then  -- error here
          set value of text field 1 of window "AppB" to beam  --or here
        end if
      end tell
    end tell
    set previousText to beam
      end if
    return checkInterval
else if (AppAgWasOpen) then
  quit
      return 1
end if

结束空闲

1 个答案:

答案 0 :(得分:1)

通常我会进入一个重复循环,并在尝试对其执行任何操作之前检查文本字段(或任何界面元素)是否可用。这样的东西可以工作,应该消除你的错误。

注意我还在这个过程中添加了一个时间检查,这样我就不会陷入重复循环。在这种情况下,我等待最多5秒钟,文本字段变为可用。

tell application "System Events"
    -- wait for the text field to become available
    set startTime to current date
    repeat until exists (text field 1 of window "AppB" of application process "AppB")
        if (current date) - startTime is greater than 5 then
            error "Could not find text field 1 of window AppB of application process AppB"
            exit repeat
        end if
        delay 0.2
    end repeat

    tell application process "AppB"
        if text field 1 of window "AppB" is enabled then -- error here
            set value of text field 1 of window "AppB" to beam --or here
        end if
    end tell
end tell