Applescript Infinite Repeat Keystrokes挂起应用程序

时间:2014-03-14 16:52:04

标签: applescript

因此,对于无限猴子定理测试,我需要使用applescript将随机数(0-6)输入到TextEdit中。 我有一个工作代码,不会减慢CPU或任何东西,但我不能终止它运行一次而不终止TextEdit。

所以我的问题是: 如何改进此代码,以便我可以毫无顾虑地终止它。

delay 2
repeat
    delay 0.2
    tell application "System Events"
        if (item 1 of (get name of processes whose frontmost is true)) is "TextEdit" then
            set r to (random number from 0 to 1)
            if (r = 0) then
                keystroke "0"
            else if (r = 1) then
                keystroke "1"
            end if
        end if
    end tell
end repeat

1 个答案:

答案 0 :(得分:0)

创建一个具有.2延迟和击键的大样本需要很长时间。

尝试这样的事情:

set csvOutputPath to (POSIX path of (path to desktop as text)) & "test.txt"
set fileRef to (open for access csvOutputPath)
close access csvOutputPath

set numberList to {}
set counter to 0
repeat
    set end of numberList to (random number from 0 to 1)
    set counter to counter + 1
    if (counter mod 10000) = 0 then
        write (numberList as text) to csvOutputPath starting at eof
        set numberList to {}
    end if
end repeat