Scott Barnes
Scott Barnes

Reputation: 13

Applescript: automate keystroke loop in Safari

I am trying to create a simple bot which will input keystrokes into a safari window. this would be useful for data entry and view-boosting etcetera. I have a brain-dead job which literally requires me to input the same series of keystrokes into a web-form thousands of times a day. I would like to automate this.

This is the script I am using in 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

Right now, the script will activate the window and bring tab one to the front, but then it begins either opening new favorites tabs, else trying to print the code...

I believe the problem is in how I am asking the system to 'press TAB', because I believe the script is actually typing T - A - B, repeatedly.

All I need the script to do is: Press TAB 12 times Press SPACE once Wait for 15 seconds Press SPACE once Wait for 70 seconds Press TAB four times Press SPACE once Wait for 15 seconds Press TAB three times Press SPACE

Repeat about 1 000 times...

Any corrections to my script or suggestions for a new code would be appreciated.

Upvotes: 1

Views: 3258

Answers (2)

jweaks
jweaks

Reputation: 3792

Well, here's some actual code to get you started. keystroke is a command of the app "System Events". You can simplify the script with some repeats, and you'll probably have to add in more 'delay .2' between some of the tabs, etc. depending on your site.

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

To add in repeats, for academic purposes only, really, I wouldn't repeat the tell blocks. I'd do something like:

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

But again, in troubleshooting, it is possible that some spots, but not all, would need more delays, so no real gain for consolidating it.

Upvotes: 1

adayzdone
adayzdone

Reputation: 11238

You should use a handler for repeated commands.

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

Upvotes: 1

Related Questions