rentzsch.com: tales from the red shed

AppleScripting Safari Forms

AppleScript
Did you know you can use AppleScript to fill out web forms via Safari? Here's a little script that automates the process of using Google to search my site for libelous accusations against you:

tell application "System Events" to set fullUserName to full name of current user
tell application "Safari"
  open location "http://www.google.com/advanced_search"
  delay 3
  do JavaScript "document.forms[0].elements[6].value = \"" & fullUserName & " eats worms\"" in document 1
  do JavaScript "document.forms[0].elements[8].value = \"libelous\"" in document 1
  do JavaScript "document.forms[0].elements[17].value = \"rentzsch.com\"" in document 1
end tell

Finding the right form/element indexes is a little tedious. I'm sure there's a nice high-level way to discover this, but I caved and brute-forced it with this script:

tell application "Safari"
  repeat with formIndex from 0 to 10
    repeat with elementIndex from 0 to 100
      do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].value = \"" & formIndex & "." & elementIndex & "\"" in document 1
    end repeat
  end repeat
end tell

I haven't bothered to figure out radio buttons and popups and the like. If you have, clue me in -- it may be handy to know one day.

Update: Jon Nathan dwarfs my puny script with his AutoFill applescript.

Sunday, September 12, 2004
12:00 AM