JavaScript Question

Is there a way to use the “do javascript” command to fill in web forms using webview? I know you can do this with Safari, but I don’t know if the webview feature in Studio has all of the same features that Safari has.

I tried doing this for an ip address webpage and it didn’t work. The problem is, I don’t know what to call my webview program. It’s never referred to as “Safari” in my script, so I already knew before running, my codes at the bottom “Tell application “Safari”” wouldn’t work, but I couldn’t think of anything else to do. Any suggestions?

Here’s my script:

on clicked theObject
	
	GetURL()
end clicked

on end editing theObject
	GetURL()
end end editing


on GetURL()
	
	tell window "main"
		
		set ipAddy to (the contents of text field "ipAddy")
		set theURL to "http://www.ip-adress.com" as text
		try
			set newString to (call method "stringByAddingPercentEscapesUsingEncoding:" of theURL with parameter 30)
			set URLWithString to call method "URLWithString:" of class "NSURL" with parameter newString
			set requestWithURL to call method "requestWithURL:" of class "NSURLRequest" with parameter URLWithString
			
			set mainFrame to call method "mainFrame" of object (view "browser")
			
			call method "loadRequest:" of mainFrame with parameter requestWithURL
		on error errmsg
			log errmsg
		end try
	end tell
end GetURL
tell application "Safari"
	do JavaScript "document.forms[0].login.value = \"" & "MYEMAIL@gmail.com\"" in document 1
	do JavaScript "document.forms[0].password.value = \"" & " MYPASSWORD\"" in document 1
	do JavaScript "document.forms[0].submit()" in document 1
end tell

Hi George,

again there’s no direct AppleScript access afaik. You can execute the JavaScripts like this:

set scriptObject to (call method “windowScriptObject” of object (view “browser”))
call method “evaluateWebScript:” of scriptObject with parameter (“document.forms[0].login.value = "” & “MYEMAIL@gmail.com"”)

(don’t forget t make sure the page is fully loaded before executing the JS :wink: …)

Hope that helps.

D.

Thank you, Dominik. That worked.