Firstly, A Safe and Happy New Year!
I am quite new to Applescript but I have been tinkering with it over my break.
I am trying to understand using Javascript to login to a webpage. I have tinkered with logging in to the Macscripter website but I’m finding difficulties understanding the Javascript tags.
Here is what I have using Javascript to login to the Mascripter webpage.
set loginurl to "http://macscripter.net/login.php"
set mylogin to "Username"
set mypassword to "Password"
tell application "Safari"
make new document at end of documents
set URL of document 1 to loginurl
--This subroutine makes sure the page has finished loading -- it expects the page to finish with the "</html"> tag
my waitforload()
--now that the page has loaded, we use Javascript inside Safari to fill in the form and click the submit button!
do JavaScript "document.forms[login][req_username].value " = mylogin in document 1
do JavaScript "document.forms[login][req_password].value " = mypassword in document 1
do JavaScript "document.forms[login].elements[1].click()" in document 1
end tell
on waitforload()
--check if page has loaded
set loadflag to 0
repeat until loadflag is 1
delay 0.5
tell application "Safari"
set test_html to source of document 1
end tell
try
set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
set loadflag to 1
end if
end try
end repeat
end waitforload
The result I get is:
do JavaScript false in document 1
do JavaScript false in document 1
do JavaScript “document.forms[login].elements[1].click()” in document 1
Obviously, I am incorrect. Where is the error and how to fix it?
If someone can explain to me the correct procedure to identify the document.forms[form name] and the correct syntax I would be most appreciative.
Cheers,
Rob
Model: iMac
AppleScript: 2.3
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)