Applescript:自动化Safari中的击键循环

时间:2014-12-16 18:15:40

标签: safari applescript keystroke

我正在尝试创建一个简单的机器人,它将键击输入到一个safari窗口中。这对数据输入和视图增强等有用。我有一个脑力不足的工作,字面上要求我每天数千次将相同的一系列击键输入到网络表格中。我想自动化这个。

这是我在Apple Script中使用的脚本


activate application "Safari"
tell front window of application "Safari" to set current tab to tab 1
repeat 1000 times
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke " "
    delay (15)
    tell application "Safari" to keystroke "SPACE"
    delay (70)
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "SPACE"
    delay (15)
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "TAB"
    tell application "Safari" to keystroke "SPACE"
end repeat

现在,脚本将激活窗口并将标签一个带到前面,但随后开始打开新的收藏夹标签,否则尝试打印代码......

我认为问题在于我如何要求系统“按TAB”,因为我认为该脚本实际上是在反复输入T-A-B。

我需要的脚本是:    按Tab键12次    按SPACE一次    等待15秒    按SPACE一次    等待70秒    按Tab键四次    按SPACE一次    等待15秒    按Tab键三次    按SPACE

重复约1 000次......

对我的脚本的任何更正或对新代码的建议都将不胜感激。

2 个答案:

答案 0 :(得分:1)

嗯,这是一些让你入门的实际代码。 keystroke是应用程序“系统事件”的命令。您可以使用一些重复来简化脚本,并且您可能需要在某些选项卡之间添加更多“延迟.2”,具体取决于您的网站。

tell application "Safari" to activate
delay 1
tell front window of application "Safari" to set current tab to tab 1
tell application "System Events"
    tell process "Safari"
        repeat 10 times
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke " "
            delay (15)
            keystroke " "
            delay (70)
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke " "
            delay (15)
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke " "
        end repeat
    end tell
end tell

添加重复,仅用于学术目的,实际上,我不会重复使用tell块。我会做类似的事情:

tell application "System Events"
    tell process "Safari"
        repeat 1000 times
            repeat 12 times
                keystroke tab
            end repeat
            keystroke " "
            delay (15)
            keystroke " "
            delay (70)
            repeat 4 times
                keystroke tab
            end repeat
            keystroke " "
            delay (15)
            repeat 3 times
                keystroke tab
            end repeat
            keystroke " "
        end repeat
    end tell
end tell

但同样,在排除故障时,有些地方(但不是全部)可能需要更多延迟,因此无法合并它。

答案 1 :(得分:1)

您应该使用处理程序来重复命令。

tell application "Safari"
    activate
    tell front window to set current tab to tab 1
end tell

repeat 10 times
    typeIt(tab, 12)
    typeIt(space, 1)
    delay 15
    typeIt(space, 1)
    delay 70
    typeIt(tab, 4)
    typeIt(space, 1)
    delay 15
    typeIt(tab, 3)
    typeIt(space, 1)
end repeat

on typeIt(stroke, n)
    tell application "System Events"
        tell process "Safari"
            repeat n times
                keystroke stroke
                delay 0.2
            end repeat
        end tell
    end tell
end typeIt