Safari URL opener

This is for opening URLs in Safari which may or may not be open already. It waits for the tab to load with an optional beep. Some methods of asking a tab for its values before it finishes loading can crash Safari or cause an AppleScript error; this script begins with presumptions of no window, and no tab. It considers the existence of the targeted URL in the tabs to be confirmation of a completed load. I’ve only used it in Safari version 10. Please let me know if it doesn’t work elsewhere!

set myURL to "http://www.theMrScienceShow.com/"

tell application "Safari"
	activate
	set myWNDW to ""
	set myURLsNOTOPEN to URL of tabs of windows as string does not contain myURL
	if myURLsNOTOPEN then
		--- --- --- ---uncomment either of these options... --- --- --- ---
		#open location myURL---open new or with other windows
		set myWNDW to make new document at end of documents with properties {URL:myURL} ---open a new window
		--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
	end if
	set myTAB to missing value
	repeat while myTAB is missing value
		tell (every window whose closeable is true and resizable is true) to set myTAB to first item of (first tab whose URL is myURL)
		beep
	end repeat
	if myWNDW is "" then set myWNDW to first window whose tabs contains myTAB
	return {myWNDW, myTAB, URL of myTAB, source of myTAB}
 end tell

Model: Mac Pro
AppleScript: 2.9
Browser: Safari 602.1.50
Operating System: macOS 10.14

OK, this is neat… a few tweaks and it worked with both Safari AND Google Chrome.

(Good luck with Firefox though.:rolleyes:)

set myURL to "http://www.theMrScienceShow.com/"
return openURL(myURL)
on openURL(myURL)
	 tell application "Google Chrome"------->USE THIS
	#tell application "Safari"--------------------> OR THIS!
		set appNAME to (it as string) --->"Google Chrome","Safari"

		set myWNDW to ""
		set myURLsNOTOPEN to URL of tabs of windows as string does not contain myURL
		if myURLsNOTOPEN then
			--- --- --- ---uncomment either of these options... --- --- --- ---
			open location myURL ---open new or with other windows
			#set myWNDW to make new document at end of documents with properties {URL:myURL} ---open a new window
			--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
		end if
		set myTAB to missing value
		repeat while myTAB is missing value
			tell (every window whose closeable is true and resizable is true) to set myTAB to first item of (first tab whose URL is myURL)
			beep
		end repeat
		if myWNDW is "" then
			repeat with w in (every window whose closeable is true and resizable is true)
				repeat with t in tabs of w
					if ((URL of t) as string) is (myURL) then set myWNDW to w
				end repeat
			end repeat
		end if
		set tabSRC to missing value
		repeat while tabSRC is missing value
			if appNAME is "Safari" then tell application "Safari" to ¬
				tell myWNDW to tell myTAB to set tabSRC to do JavaScript "document.body.innerText" ---SAFARI
			if appNAME is "Google Chrome" then tell application "Google Chrome" to ¬
				tell myWNDW to tell myTAB to set tabSRC to execute javascript "document.body.innerText" ---GOOGLECHROME
			#tell window 1 to tell myTAB to set tabSRC to do JavaScript "document.body.innerHTML"---use this for html
			beep
		end repeat
		return {window id (id of myWNDW), myTAB, URL of myTAB, tabSRC}
	end tell
end openURL