启用Applescript检测的编辑框

启用Applescript检测的编辑框,applescript,Applescript,我的脚本从AppA获取一些文本,并将其粘贴到AppB上的文本编辑中。当AppB启动时(通过脚本),文本编辑被禁用,当用户执行操作时变为启用。该操作需要保持手动 脚本在用户有时间做任何事情之前执行,这是一个错误。我的想法是检查编辑是否已启用,但这会导致此错误。无法获取应用程序“系统事件”的“AppB”的AppB窗口。它只抛出一次错误 我怎样才能避免这个错误呢?一个try块来消除这个错误会更好吗 on idle tell application "System Events" to set App

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

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

我怎样才能避免这个错误呢?一个try块来消除这个错误会更好吗

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
处于空闲状态
告诉应用程序“系统事件”将AppIsOpen设置为(应用程序进程“AppA”存在)
如果(AppAIsOpen)那么
将AppAWasOpen设置为true
告诉应用程序“AppA”
将hdg设置为TxRprt
将梁设置为hdg作为字符串
结束语
如果((计数光束)<3),则将光束设置为文本-3到-1(“000”&光束)
如果(梁不是先前的文本),则
告诉应用程序“AppB”启动
告诉应用程序“系统事件”
告诉应用程序进程“AppB”
如果窗口“AppB”的文本字段1已启用,则--error here
将窗口“AppB”的文本字段1的值设置为beam--或此处
如果结束
结束语
结束语
将上一个文本设置为beam
如果结束
返回检查间隔
否则如果(AppAgWasOpen)那么
退出
返回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