Open Safari web page, scroll down & highlight text on said page.

Escaping any quotes and/or backslashes, basically.

Hello Nigel

I asked because when I tried to search the string defined by:
set theTEXT to “as "The World’s Greatest Rock and Roll Band".”
the script didn’t find it while it’s really available at the beginning of the page.
Same negative result with :
set theTEXT to "as " & quote & “The World’s Greatest Rock and Roll Band” & quote & “.”
or
set theTEXT to quoted form of "as " & quote & “The World’s Greatest Rock and Roll Band” & quote & “.”

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 29 décembre 2016 16:13:36

Hi Yvan.

Yes. That’s the kind of thing to which I was referring. Since my script uses escaped double-quotes to quote the parameter in the string for the JavaScript command, any double-quotes in the parameter have to be double-escaped:

set theTEXT to "\\\"The World's Greatest Rock and Roll Band\\\""

tell application "Safari"
	-- Blah blah blah
	do JavaScript "window.find(\"" & theTEXT & "\")" in document 1
end tell

If I’d used single-quotes to quote the parameter, the parameter’s double-quotes would only need to be single-escaped and the single-quotes and apostrophes would need to be escaped:

set theTEXT to "\"The World\\'s Greatest Rock and Roll Band\""

tell application "Safari"
	-- Blah blah blah
	do JavaScript "window.find('" & theTEXT & "')" in document 1
end tell

Thanks Nigel.
I apologizes, it works but it’s too cumbersome for me.

I will stay with :

set theURL to "https://en.wikipedia.org/wiki/The_Rolling_Stones"
set theTEXT to "Mick and I thought these songs were really puerile"
set theTEXT to "as " & quote & "The World's Greatest Rock and Roll Band" & quote & "."
# or this easy to build alternate version
--set theTEXT to "as \"The World's Greatest Rock and Roll Band\"."
(*
set theURL to "https://fr.wikipedia.org/wiki/Le_Corbusier"
set theTEXT to "La modestie du commandeur influença probablement le choix définitif"
*)
tell application "Safari"
	activate
	--end tell # ACTIVE in original code
	
	open location theURL
	
	--tell application "Safari" # ACTIVE in original code
	delay 1
	repeat until (do JavaScript "document.readyState" in document 1) is "complete"
		delay 1
	end repeat
end tell

tell application "System Events" to tell process "Safari"
	set frontmost to true # ADDED
	delay 0.2 # REQUIRED when there is a single tell application "Safari", no need with the originals two tell instructions
	keystroke "f" using command down
	tell window 1
		-- Wait until the "Find" text field appears.
		repeat until (text field 1 of group 2 of UI element 2 of splitter group 1 exists)
			delay 0.2
		end repeat
		set theTarget to text field 1 of group 2 of UI element 2 of splitter group 1
		set value of theTarget to theTEXT
	end tell
	delay 0.5 -- Wait until the found text is selected and comes into view.
	keystroke "." using {command down}
end tell

I like it because the events log displays exactly what it inserts :

set value of text field 1 of group 2 of tab group 1 of splitter group 1 of window "The Rolling Stones - Wikipedia" of application process "Safari" to "as \"The World's Greatest Rock and Roll Band\"."

which exactly what the alternate version does.

It’s clear, deciphering Enigma wasn’t a task for me. :frowning:

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 29 décembre 2016 17:45:41

Just thought I’d add that since I’ve completed some latest macOS Sierra upgrades with newer Safari versions, I’ve found that this part doesn’t work very well for waiting for the web page to load.


        tell application "Safari"
		activate
		delay 1
		repeat
			if (do JavaScript "document.readyState" in document 1) is "complete" then exit repeat
			delay 1 -- wait a second before checking again
		end repeat
		delay 1
	end tell

So, as of Sierra 10.12.3 I’ve been adding a dialog to many of my scripts that depend upon Safari loading a web page first. This is an example that works with Launchbar after I enter a search term.


on handle_string(theString)
	
	tell application "Safari"
		
                activate		
		open location "https://samplewebsite.com/forum/"
				
	end tell
	
	-- For some reason by keeping the following javascript it keeps dialog active so I can hit enter key
	
	tell application "Safari"
		activate
		delay 1
		repeat
			if (do JavaScript "document.readyState" in document 1) is "complete" then exit repeat
			delay 1 -- wait a second before checking again
		end repeat
		delay 1
	end tell
	
	-- Again, for some reason keeping the above javascript keeps dialog active below so I can hit enter key instead of having to click on the dialog with the mouse first to activate it	
	
        display dialog "Press Enter When Page Loads..." with title "Continue" buttons {"Continue"} default button 1
	if button returned of result is "Continue" then
	end if
	
	
	tell application "Safari"
		
		do JavaScript "document.getElementById('SearchQuery').value ='" & theString & "';" in document 1
		delay 1
		do JavaScript "document.getElementsByClassName('search Tooltip')[0].click()" in document 1
	end tell
	
		
end handle_string

This way when I launch a script from LaunchBar or somewhere else, I can simply hit the Enter key once the website loads. It’s been working great. I guess I’d prefer this to happen automatically, though.

Any input on this would be appreciated. I’m not sure of any other way to keep the AppleScript dialog active while Safari loads the page when I’m using LaunchBar.