Opening a URL in Safari When No Window is Open

I have a script that is designed to get Safari to activate, and go to a particular URL that happens to be a php file. Here is a modified version of it…


tell application "Safari"
	activate
	set URL of document 1 to "https://www.mysite.org/dump_set1.php"
end tell

Everything works fine if …

  1. Safari is not open; or
  2. Safari is open, and a browser window is active, either on screen or in the dock (minimized).

It does not work if Safari is open, but no window is active. Instead I get an error message, “Can’t get document 1. Invalid index.” If I’m running this script from within a FMP10 script, I get an additional error message, " Unknown Error: -1719."

What do I need to do to this code to fix this issue? BTW, I do have the home page set to a file on my computer, but this is not a problem, for the script opens that page, and then sets its URL according to the script.

Thanks,
Charlie

Hi,

check for the number of documents


property myURL : "https://www.mysite.org/dump_set1.php"

tell application "Safari"
	activate
	if (count documents) = 0 then
		make new document with properties {URL:myURL}
	else
		set URL of document 1 to myURL
	end if
end tell

Maybe something like this?

tell application "Safari"
	activate
	if (count every document) is 0 then
		set newDoc to (make new document with properties {URL:"http://google.com"})
	else
		set URL of document 1 to "http://google.com"
	end if
end tell

Hope it helps,
ief2

EDIT: Damn, to late :stuck_out_tongue:

Thanks, Stefan. That did the trick.