Getting Safari to load URL

I’m able to successfully run the below Applescript on my Mac, but when it runs on other’s Macs, they get this error:

Apple Script Error:
Safari got an error
NSCannotCreateScript CommandError

Can anyone tell me what’s wrong with this script?

tell application “Safari”
set the URL of document 1 to “https://activate.verizon.net/launch/start/?ver=1.0&cdtype=vzone&type=consumer&template=no&customertype=newdsluser
end tell

Basically, I just need a script that will tell Safari to launch (if it’s not started yet), and immediately go to an URL. Is there a better way? I think what might be happening is that Safari needs to be up and running first for this to work on other Macs. I’m not sure of that, however.

There might be a way to do it like how you’re attempting it, but in my experience to get the best behavior out of Safari you have to use JavaScript.

Here is my script for “Creating a new script that will open a URL in Safari” … I keep this attached to a hotkey so when I am at a site that I want to creat a URL hotkey script for, I can just run this script and have a new script pop up in Script Editor:


-- Create Safari URL shortcut

set selectedURL to ""
set gotOne to 0

tell application "Safari"
	try
		set selectedURL to URL of document 1
		set gotOne to 1
	end try
end tell

if (gotOne is 0) then
	set dialogAnswer to (display dialog "Please enter the URL to save:" buttons {"OK", "Cancel"} default button 1 default answer "")
	if button returned of dialogAnswer is "OK" then
		set selectedURL to text returned of dialogAnswer
		set gotOne to 1
	end if
end if

if (gotOne is 1) then
	set newText to "tell application "Safari"
	set targetURL to "" & selectedURL & ""
	try
		set thisDocument to document 1
	on error
		set thisDocument to ""
	end try
	if (thisDocument is "") then
		set thisDocument to make new document at end of documents with properties {URL:targetURL}
	else
		do JavaScript "document.location='" & targetURL & "';" in thisDocument
	end if
end tell"
	
	my NewScriptWithText(newText)
	tell application "Script Editor"
		activate
	end tell
end if

on NewScriptWithText(theText)
	tell application "Script Editor"
		
		make new document at end of documents with properties {contents:theText}
		
	end tell
end NewScriptWithText

Just run this script while you’re at the page in Safari and you should get something that does exactly what you want.

Note it will always attempt to “take over” the frontmost window, which is similar to what your code is doing. This is what makes it handiest for things like hotkeys.

Here is an all Applescrit way of doing it. Unless I’m missing something your trying to do.



tell application "Safari"
	-- open in new tab if already open
	--opens safari if not open
	--If the Url is already open it will go to that tab
	--must specify protocol:// ie "http://" "ftp://"
	open location "http://www.google.com"


	-- Opens in same tab
	--opens safari if not open
	tell document 1
		
		--open location "http://www.google.com/maps"
		
	end tell
end tell

I hope this helps

sorry uncomment the line between the Tell Document 1 block. :frowning:

Thanks to both of you for your suggestions. I think I’m close, but I’m unfortunately getting two windows popping up in Safari instead of one. Is there a way this script can be modified so that Safari launches with just one window, to the URL I specify? (Note, even if I comment out one of the “open locations” below, I still get my homepage loading first, and then the URL:

tell application “Safari”
– open in new tab if already open
–opens safari if not open
–If the Url is already open it will go to that tab
–must specify protocol:// ie “http://” “ftp://”
open location “http://www.google.com

-- Opens in same tab 
--opens safari if not open 
tell document 1
	
	open location "http://www.google.com/maps"
	
end tell

end tell

Safari has annoying behavior when a home page URL is set.

However, if you use my technique, then you’ll still “take over” that window. It will just be a little blip as it loads the home page and then replaces it with your URL. I just tested it.

If you don’t like that behavior then you could always turn off the home page feature of Safari.

Hmmmm, I see what you mean by the Safari Home Page default URL causing issues. The script that your code generates works perfectly, unless the homepage URL in Safari is set to a page that takes more than a second to load. For instance, if my homepage in Safari is set to Google, everything works great. However, try having your homepage set to “www.verizon.com”. Since that pages takes awhile to load, I get an Applescript error and the URL never loads.

Hmmm, is there by chance a unix call that will force Safari to load an URL?

tell application “Safari”
set targetURL to “https://activate.verizon.net/launch/start/?ver=1.0&cdtype=vzone&type=consumer&template=no&customertype=newdsluser
try
set thisDocument to document 1
on error
set thisDocument to “”
end try
if (thisDocument is “”) then
set thisDocument to make new document at end of documents with properties {URL:targetURL}
else
do JavaScript “document.location='” & targetURL & “';” in thisDocument
end if
end tell

perhaps there is a javascript command you could use to stop-loading in whatever the document is. that might make it receptive to a new URL.

It looks like “window.stop()” might do the trick if you could get Safari to run it, but the problem here seems to be that while Safari is in the process of loading that first page, it doesn’t want to allow any javascript to be sent to the document.

yup - - - I found the same thing. I’m stuck, because I can’t go disabling the user’s default homepage either. Even if I could force Safari to load “blank:” or something, and then load my real-webpage that would work. The unix “open” command will open an URL, but that doesn’t force Safari to do it, which is my ultimate goal.

Okay, I see Redsweater, I never noticed the homepage issue I startup blank, so you both gave an Idea. “Open” has other syntax Try this script.

I works for me.

Both lines do different things the first doesn’t wait for do shell script to return
the second does.


do shell script "/usr/bin/open -a safari http://www.macscripter.net/ > /dev/null 2>&1 &" --Asynchon Change dev null to file name if you need to see the results

do shell script "/usr/bin/open -a safari http://www.macscripter.net/ " --Waits for completion


Posted Prematurely, I forgot that open always uses the default browser / application for URL types. that’s why I switched to Launch 1.0

Its a command line open on steroids. it guarentees the application for the URL

http://www.macupdate.com/info.php/id/9369

It’s saved my bacon several times. in shell scripts.

use this command instead be sure to change the Path to launch.


--must have path to launch because sh shell doesn't source ~/.profile
do shell script "/Path/To/launch -i com.apple.Safari  [url=http://www.macscripter.net/]http://www.macscripter.net/"[/url] --Waits for completion