I need some help creating a applescript . I’m trying created a script to fill out web form such like this http://www.asp101.com/samples/checkbox.asp. The webform is only checkboxes.
I will really appreciate if anybody can help me with this or if you can point me to where I can get information about how to do it I will appreciate.
I would search around for GUI scripting articles, but to tell you the truth its not pretty. Using UI Brower from the PreFab group to click the very first box this works…
activate application "Safari"
tell application "System Events"
tell process "Safari"
click checkbox 1 of group 36 of UI element 1 of scroll area 1 of group 4 of window "ASP 101 - Checkbox Sample"
end tell
end tell
To click the other checkboxes just change the checkbox number… the sad thing though is this may or may not be the case on every webpage.
in your example the first checkbox can be set with
tell application "Safari"
do JavaScript "document.forms[3].elements[0].checked=true" in document 1
end tell
but don’t ask me how to determine a certain element, I’ve done it trial&error with
Script Debugger’s single step function and this snippet
tell application "Safari"
repeat with formIndex from 0 to 10
repeat with elementIndex from 0 to 10
do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].checked=true" in document 1
end repeat
end repeat
end tell
Edit: You can determine the form element by name with
set checkBoxName to "default"
tell application "Safari"
set maxForm to do JavaScript "document.forms.length" in document 1
repeat with formIndex from 0 to maxForm as integer
set maxElements to do JavaScript ("document.forms[" & formIndex as string) & "].elements.length" in document 1
repeat with elementIndex from 0 to maxElements
if (do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].name" in document 1) is checkBoxName then
display dialog "FormIndex: " & formIndex & " / ElementIndex: " & elementIndex
do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].checked=true" in document 1
end if
end repeat
end repeat
end tell
1.-how you load the url on applescript at the moment you open Safari?
2.-using Tell you open the application but how you close the application with applescript?
3.-applescript has some kind of for loops?
1. this is a way tp open a page with a subroutine, which waits until the page is reliably loaded
tell application "Safari"
activate
open location "http://www.asp101.com/samples/checkbox.asp"
if page_loaded(20) is false then display dialog "Couldn't load page" buttons {"Cancel"} default button 1
-- do something
end tell
on page_loaded(timeout_value)
delay 2
repeat with i from 1 to timeout_value
tell application "Safari"
if (do JavaScript "document.readyState" in document 1) is "complete" then
return true
else if i is timeout_value then
return false
else
delay 1
end if
end tell
end repeat
return false
end page_loaded
set checkBoxName to "default"
tell application "Safari"
set maxForm to do JavaScript "document.forms.length" in document 1
repeat with formIndex from 0 to maxForm as integer
set maxElements to do JavaScript ("document.forms[" & formIndex as string) & "].elements.length" in document 1
repeat with elementIndex from 0 to maxElements
if (do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].name" in document 1) is checkBoxName then
display dialog "FormIndex: " & formIndex & " / ElementIndex: " & elementIndex
do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].checked=true" in document 1
end if
end repeat
end repeat
end tell
where applescript is clicking “submit” or how can I click submit?