Javascript execution in Safari is very slow

The following “do Javascript” command, is excruciatingly slow, requiring at times, up to 13 seconds.

tell application "Safari"
	tell document 1
		if its name is not "Google" then
			open location "https://www.google.com/"
			repeat 10 times
				if its name is "Google" then exit repeat
				delay 0.5
			end repeat
		end if
		if its name is not "Google" then display dialog "Cannot Find Google Page" giving up after 2
		set TargetTextQuoted to quoted form of ("Hello World!")
		do JavaScript "document.getElementsByClassName('lst')[0].value=" & TargetTextQuoted
	end tell
end tell

  1. What method would execute Javascript, in a more productive manner?
  2. Is it possible to write a webkit framework function to evaluate a JavaScript string?
  3. If so, would that function accelerate the speed of executing the command?
  4. If so, how should I write the Javascript function?

When the AppleScript compiler receives incorrect instructions from the user, it, unlike compilers in other languages, tries to guess for itself and fixes everything it can. Although this is reflected in the speed of script execution. In another language, you would just get “This is the end, my friend. Error-r-r!”.

Here, I see 1 problem with your script: 1 do JavaScript is command of Safari.app and not of its document:


tell application "Safari"
	tell document 1
		if its name is not "Google" then
			open location "https://www.google.com/"
			repeat 10 times
				if its name is "Google" then exit repeat
				delay 0.5
			end repeat
		end if
		set itsName to its name
	end tell -- end tell to document here, begin tell to Safari.
	if itsName is "Google" then
		set TargetText to "Hello World!"
		do JavaScript "document.getElementsByClassName('lst')[0].value=" & quoted form of TargetText
	else
		display dialog "Cannot Find Google Page" giving up after 2
	end if
end tell

NOTE: no need JavaScript to google some text:


tell application "Safari"
	activate
	open location "https://www.google.gr/search?q=" & "Hello word!"
	delay 10 -- to see some time at front
end tell

Thank you, KniazidisR for your insights.
Unfortunately, when I run your script, although Applescript runs quickly in less than 0.1 seconds, the script returns

The Google web page also shows no inputted text value in its search field.
Can you explain your script, a bit further, at least to the level that I can replicate it?

I’m sorry, but my second statement is due to my inattention. So I corrected the post. But the script doesn’t work anyway. You said that the speed of your script is problematic for you, so I thought it worked, albeit slowly. But as I checked it does not work at all.

There may be 2 reasons:

  1. you are specifying the wrong element (why do you think that is b[0][/b]?)
  2. many buttons and text fields in Google APIs are protected from robots. For example, they require a mouse click to execute JavaScript code.

Read the note in my post, how to google automatically.

Here is other approach to google some text:


set aText to "Hello word!"

tell application "Safari"
	if (count documents) > 0 then
		search the web in front document for aText
	else
		search the web for aText
	end if
end tell

KniazidisR, Thanks for sharing your two solutions for searching the web. As I understand your comments, you recommend:

  1. Opening a Google find location via a url string
  2. Commanding Safari to search the web

You have significantly simplified the process for me.
My javascript command, as I see it, was to enter text into the search field on Google’s search page.
Is it possible that the 10 to 15 second delay that I was experiencing with my code might have been due to robots on the Google API interfering with that javascript command?

The search field of Google is one combobox. Here is its HTML. You can see that it requires manual actions from user, like paste. And it becomes focused when you put mouse on it before pasting the clipboard or before enter the text. That is, the problem is not delays, but mouse click to focus the combobox.