Applescript现实击键

时间:2014-12-06 23:59:43

标签: applescript keystroke

是否可以使用Applescript以随机速度快速输入文本&有效率的?我有以下脚本:

运行{input,parameters}:

tell application "System Events"
    keystroke "H"
    delay 0.2
    keystroke "i"
    delay 0.2
    keystroke " "
    delay 0.2
    keystroke "t"
    delay 0.2
    keystroke "h"
    delay 0.2
    keystroke "e"
    delay 0.2
    keystroke "r"
    delay 0.2
    keystroke "e"
    delay 0.2
    keystroke "!"
    delay 0.1
    key code 36
    delay 0.5
    keystroke "H"
    delay 0.2
    keystroke "o"
    delay 0.1
    keystroke "w"
    delay 0.2
    keystroke " "
    delay 0.1
    keystroke "a"
    delay 0.2
    keystroke "r"
    delay 0.1
    keystroke "e"
    delay 0.2
    keystroke " "
    delay 0.1
    keystroke "y"
    delay 0.2
    keystroke "o"
    delay 0.2
    keystroke "u"
    delay 0.1
    keystroke "?"
    delay 0.2
    key code 36
    delay 1.0
    keystroke "W"
    delay 0.2
    keystroke "h"
    delay 0.2
    keystroke "e"
    delay 0.1
    keystroke "r"
    delay 0.2
    keystroke "e"
    delay 0.2
    keystroke " "
    delay 0.1
    keystroke "d"
    delay 0.2
    keystroke "o"
    delay 0.2
    keystroke " "
    delay 0.2
    keystroke "y"
    delay 0.2
    keystroke "o"
    delay 0.1
    keystroke "u"
    delay 0.2
    keystroke " "
    delay 0.1
    keystroke "w"
    delay 0.2
    keystroke "a"
    delay 0.1
    keystroke "n"
    delay 0.2
    keystroke "t"
    delay 0.2
    keystroke " "
    delay 0.1
    keystroke "t"
    delay 0.3
    keystroke "o"
    delay 0.2
    keystroke " "
    delay 0.1
    keystroke "b"
    delay 0.2
    keystroke "e"
    delay 0.2
    keystroke "g"
    delay 0.1
    keystroke "i"
    delay 0.2
    keystroke "n"
    delay 0.1
    keystroke "?"
end tell

return input
end run

我基本上想要在一次击键(或类似命令)中键入所有单词(至少1行),然后在每个键之间使用下面的随机延迟。有可能吗?

delay (random number from 0.05 to 1.0)

2 个答案:

答案 0 :(得分:0)

解决了它 - http://christianheilmann.com/2014/01/09/autmating-typing-in-screencasts-with-applescript-or-how-to-look-like-a-hollywood-hacker/

您只需要创建一个文件(我使用.txt文件而不是.js文件),指定位置&嘿presto。

以随机文本速度继续我的调整:

set fc to read POSIX file "/test/testdoc.txt" as «class utf8»

set the text item delimiters to (ASCII character 10)
set mylines to text items in fc
repeat with currentline in mylines
    write_string(currentline)
end repeat

on write_string(the_string)
    tell application "System Events"
        tell application "textedit" to activate
        delay 10
        repeat with the_character in the_string
            keystroke the_character
            delay (random number from 0.05 to 0.25)
        end repeat
        key code 36
        key code 123 using command down
    end tell
end write_string

答案 1 :(得分:0)

如果你不介意没有回报,那就用这个:

delay 3 

set new_array to "h, i, ,  t, h, e, r, e, !,  , h, o, w,  , a, r, e,  , y, o, u, ?,  , w, h, e, r, e,  , d, o,  , y, o, u,  , w, a, n, t,  , t, o,  , b, e, g, i, n, ?"
set AppleScript's text item delimiters to ", "
set the list_items to every text item of the new_array
set a to list_items
set b to count of list_items
set c to 0
tell application "System Events"
repeat b times
    set c to c + 1
    delay (random number from 0.05 to 0.3)
    keystroke item c of a
end repeat
end tell

但如果你想要收益,那就用这个:

delay 3
set a to "h, i, ,  t, h, e, r, e, !"
set b to "h, o, w,  , a, r, e,  , y, o, u, ?"
set c to "w, h, e, r, e,  , d, o,  , y, o, u,  , w, a, n, t,  , t, o,  , b, e, g, i, n, ?"
set AppleScript's text item delimiters to ", "
set the list_items to every text item of the a
set d to list_items
set e to count of list_items
set f to 0
set AppleScript's text item delimiters to ", "
set the m to every text item of the b
set g to m
set h to count of m
set i to 0
set AppleScript's text item delimiters to ", "
set the n to every text item of the c
set j to n
set k to count of n
set l to 0
tell application "System Events"
repeat e times
    set f to f + 1
    delay (random number from 0.05 to 0.3)
    keystroke item f of d
end repeat
key code 36
repeat h times
    set i to i + 1
    delay (random number from 0.05 to 0.3)
    keystroke item i of g
end repeat
key code 36
repeat k times
    set l to l + 1
    delay (random number from 0.05 to 0.3)
    keystroke item l of j
end repeat
end tell