Please help, filling out a web form with applescript

Hi Everyone,

I am struggling with filling out a form via an applescript. I’ve figured it out mostly, except am having a strange problem… Sometimes when I run the script I end up with no result, a reply of “missing value” whereas other times I run it, it actually works…

I am on OS X Lion and Safari 5.1.2… I am finding a lot of complaints about javascript just not working on Lion properly with Safari — I hope there is a solution, b/c this is really annoying!!

WHAT I AM DOING
taking a filemaker pro record, copying some fields out of it, and filling in a web form (which is based on my desktop, but feeds into Salesforce.com)




tell application "FileMaker Pro Advanced" --collecting the info I need
	do script "findCurrentOnly"
	set pubTitle to cell "Title"
	set contactEmail to cell "Email"
	set theURL to cell "Website"
	set thePhone to cell "Phone"
	--set trueValue to "true"
	
	
	
	tell application "Safari" --filling out the form
		activate
		open location "file:///Users/InNov8/Desktop/NewLEAD.html"
		
		set x to "
		document.forms[0].elements[4].value='" & contactEmail & "' ;
		document.forms[0].elements[5].value='" & pubTitle & "' ;
		document.forms[0].elements[6].value='" & theURL & "' ;
		document.forms[0].elements[7].value='" & thePhone & "' ;
		document.forms[0].elements[12].value='12/04/11';
		"
		
		tell application "Safari" to do JavaScript x in document 1
		
		
	end tell
end tell

Anybody have any ideas on why it sometimes results in a missing value?

If the filemaker pro field is blank it should just come up blank — which seems to work sometimes…

Thanks!

Change

 open location "file:///Users/InNov8/Desktop/NewLEAD.html"
       

and

       tell application "Safari" to do JavaScript x in document 1

to

delay 2
 open location "file:///Users/InNov8/Desktop/NewLEAD.html"
       

and

delay 2
       tell application "Safari" to do JavaScript x in document 1


I’ve had this happen too - it seems that Safari will occasionally ‘beachball’ while processing, and the applescript will keep chugging along, thus missing a step. the delay gives Safari a chance to catch up. You may have to tweak the delay timing…
I set 2 = 2 seconds.

The actual reason for this is that when the browser is completed loading it doesn’t mean it is completely loaded. The HTTP request was successful but images and other external data still needs to be loaded. So you can invoke javascript commands before an form is loaded by AJAX for example.