0

我的脚本从 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

结束空闲

4

1 回答 1

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
于 2013-02-25T00:33:10.777 回答