2013-02-24 86 views
0

我的腳本從AppA獲取一些文本並將其粘貼到AppB上的文本編輯中。當AppB啓動時(通過腳本),文本編輯被禁用,當用戶執行操作時變爲啓用狀態。該行動需要保持手動。AppleScript啓用編輯框

該腳本在用戶有時間處理任何事情之前執行錯誤。我的想法是檢查是否啓用了編輯,但是出現此錯誤。 「無法獲得應用程序」系統事件「的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

通常我進入一個重複循環併爲您的文本字段(或任何界面元素),試圖用它做任何事情之前變得可用。這樣的事情會工作,並應消除你的錯誤

不是冰我也加入了一個時間檢查這個過程,以便我不會陷入重複循環。在這種情況下,我最多等待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 
+0

這很好用。謝謝。 – Mike 2013-02-25 03:21:46