# Using applescript to open web browser and go to a site



## blackoutspy (Aug 10, 2003)

I've been messing around with AppleScripts and i thought it would be nice to make a script that would open a window and go to a spacific site for me, but i can't figure it out. So far all i have is:
tell application "Safari"
     activate
     make new window
end tell

This is extreamly poor, i understand, but i can't find the command for new window for the life of me, much less a "goto URL" command=\


----------



## Arden (Aug 11, 2003)

Use the Record option.


----------



## blackoutspy (Aug 11, 2003)

safari doesn't support record=\ and neither does IE


----------



## Arden (Aug 11, 2003)

Oh.  Then drag the little icon next to the URL to the Desktop, and tell Applescript to launch this URL file.


----------



## blackoutspy (Aug 11, 2003)

what would be the code for that?


----------



## Arden (Aug 11, 2003)

Um...

```
tell application "Finder"
  open file "XYZ"
end tell
```
I should think...

Just record your actions as you open the URL file.


----------



## dlloyd (Aug 11, 2003)

```
tell application "Safari"
	activate
	do JavaScript "window.open('http://www.yahoo.com')" in document 1
end tell
```

This will do what you want.

Make sure you retain the http:// and the trailing slash though, otherwise you will get an error page.


----------



## bluehz (Aug 12, 2003)

Here's a little script I use that runs automatically every morning before I wake up. Then when I get my mornig coffee I can just sit down and all my news pages, etc are already open. While not strictly an AppleScript, it might help you out. This is a shell script that is launched by cron at a specified time.

1. Create the text file (aake sure its TEXT not RTF) and enter the following:

#!/bin/sh

#
# functions
#
# opennew is a function to create a new Safari window
opennew () {
osascript <<EOF
tell application "Safari"
        activate
        make new document at beginning of documents
end tell
EOF
sleep 5
}

# open a new Safari window then loads the urls listed below into that new
window.
# note the last url in the list does NOT have a trailing fwd slash
opennew
open "http://url1" \
"http://url2" \
"http://url3" \
"http://url4"

# open another window, load more urls
opennew
open "http://url5" \
"http://url6" \
"http://url7" \
"http://url8"

2. Save the file and change permissions to executable (chmod 755 filename).

3. Setup a cron task to launch the file at a specific time, or you can run it manually


----------



## kalantna (Aug 12, 2003)

what about a script that will prompt the user for the URL they want to open and then open that URL in say IE, Camino, Safari, and Netscape?

Now, that would be a useful script for me when developing websites.


----------



## dlloyd (Aug 12, 2003)

```
display dialog "Open in" buttons {"Cancel", "All Browsers", "Just Safari"} default button 3
copy the result as list to {the button_pressed}

if the button_pressed is equal to "All Browsers" then
	display dialog "Enter the URL to go to:" default answer "http://" buttons {"Choose Local File", "Cancel", "OK"} default button 3
	copy the result as list to {the empty_var, the button_pressed}
	if the button_pressed is equal to "OK" then
		copy the result as list to {the goto_URL}
		tell application "Finder"
			launch application "Safari"
			launch application "Camino"
			launch application "OmniWeb"
			launch application "iCab"
			launch application "Internet Explorer"
			launch application "Netscape"
			launch application "Mozilla"
			wait
		end tell
		tell application "Safari"
			activate
			make new document at beginning of documents
			open location goto_URL
		end tell
		tell application "Camino"
			activate
			make new document at beginning of documents
			open location goto_URL
		end tell
		tell application "OmniWeb"
			activate
			make new document at beginning of documents
			GetURL goto_URL
		end tell
		tell application "iCab"
			Activate
			make new window
			OpenURL goto_URL
		end tell
		tell application "Internet Explorer"
			Activate
			GetURL goto_URL
		end tell
		tell application "Netscape"
			activate
			Get URL goto_URL
		end tell
		tell application "Mozilla"
			activate
			Get URL goto_URL
		end tell
	else if the button_pressed is equal to "Choose Local File" then
		set the local_file to choose file with prompt "Choose a local file to open:"
		tell application "Finder"
			launch application "Safari"
			launch application "Camino"
			launch application "OmniWeb"
			launch application "iCab"
			launch application "Internet Explorer"
			launch application "Netscape"
			launch application "Mozilla"
		end tell
		tell application "Safari"
			activate
			make new document at beginning of documents
			open local_file
		end tell
		tell application "Camino"
			activate
			make new document at beginning of documents
			open local_file
		end tell
		tell application "OmniWeb"
			activate
			make new document at beginning of documents
			open local_file
		end tell
		tell application "iCab"
			Activate
			make new window
			ShowFile local_file
		end tell
		tell application "Internet Explorer"
			Activate
			open local_file
		end tell
		tell application "Netscape"
			activate
			open local_file
		end tell
		tell application "Mozilla"
			activate
			open local_file
		end tell
	end if
else if the button_pressed is equal to "Just Safari" then
	display dialog "Enter the URL to go to:" default answer "http://" buttons {"Choose Local File", "Cancel", "OK"} default button 3
	copy the result as list to {the empty_var, the button_pressed}
	if the button_pressed is equal to "OK" then
		copy the result as list to {the goto_URL}
		tell application "Safari"
			activate
			make new document at beginning of documents
			open location goto_URL
		end tell
	else if the button_pressed is equal to "Choose Local File" then
		set the local_file to choose file with prompt "Choose a local file to open:"
		tell application "Safari"
			activate
			make new document at beginning of documents
			open local_file
		end tell
	end if
end if
```

This is something I wrote in a few hours this morning, you better be grateful, it took me so long 
Really though, I haven't done AppleScript for so long, it was kinda cool to get back to it!
Oh, and I _know_ that all those browser actions work on their own, but the IE, Netscape, and Mozilla ones don't work with all those others in front of them. What you'll need to do is take out the ones you don't need (iCab, Omniweb, etc.) and just leave the others. You can also change the 'Default' browser. I assume you know a little Applescript, so I won't describe how to do it unless you need me to


----------



## Killian 42 (Sep 27, 2008)

*No, No

All you have to do is say:

open location "http://www.youtube.com/watch?v=QFCSXr6qnv4&feature=user"

Or something along those lines.*


----------

