2013-10-14 32 views
2

我是蘋果手機新手,並正在嘗試使用它。我正在使用類似於此的代碼來將文本輸入到textedit中。在Applescript中,如何在不是前窗的窗口中輸入文本?

tell application "textedit" 
    activate 
    set n to a random number between 1 and 100 
    repeat 100 times 
     tell application "System Events" 
      delay 4 
      keystroke n 
      delay 3 
     end tell 
    end repeat 
end tell 

這隻要工作正常的文字編輯是在前面的窗口,但如果我在Chrome中做一些事情,將輸入的文本到任何文本字段是活躍在Chrome中。即使textedit在後臺,我如何讓它輸入文本?

在此先感謝:)

+0

擊鍵總是發給最前面的應用程序。因此,如果您想讓按鍵始終轉到TextEdit,那麼您必須在發出擊鍵之前激活TextEdit。這就是它的工作原理。您不能向後臺應用程序發出擊鍵。 – regulus6633

回答

1

系統事件擊鍵不是最好的辦法。

tell application "TextEdit" 
    set newDoc to make new document with properties {text:("Begin Count" & return)} 
    repeat with i from 1 to 100 
     set text of newDoc to newDoc's text & i & linefeed 
     delay 1 
    end repeat 
end tell 
相關問題