Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Open new Safari window with an AppleScript application Web Browsers
The Safari dock icon behavior has always annoyed me. If you have no windows open and click on it, it will open a new Safari window. Great. However, if you have other windows open or minimized to the dock, it will just restore the last window you had open, which I find annoying. My solution: AppleScript.

You can use the System Events feature of OS X to activate any menu options. For this to work, you must have "Enable access for assistive devices" enabled on the Universal Access system preference pane.

Create a new AppleScript program in Script Editor with the following code, and save it as an application:

tell application "Safari" to activate
tell application "System Events"
  tell process "Safari"
    click menu item "New Window" of menu "File" of menu bar 1
  end tell
end tell
Now, find some spiffy icon to assign to your new application and drag it to the dock. I put this application right above the real Safari application, and assigned it a different icon. When you click on this new program, it will open a new Safari window. One quirk is if Safari isn't already running, it will launch two windows since Safari loads one on startup. I've only tested this on 10.3.
    •    
  • Currently 3.14 / 5
  You rated: 2 / 5 (7 votes cast)
 
[50,871 views]  

Open new Safari window with an AppleScript application | 10 comments | Create New Account
Click here to return to the 'Open new Safari window with an AppleScript application' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Open new Safari window with an AppleScript application
Authored by: joeholmes on Dec 10, '03 11:26:54AM
This is really great, but I'd love to add one more wrinkle to the script.

What I hate is switching to Safari and then having to open a new window before I can type any URL etc. What I want is a script that will create a new Safari window only if no window is open.

I'm sure a simple "if" statement will do the trick, but I can't find a way to tell Applescript to detect a window in Safari.

Anyone?

-=-Joe

[ Reply to This | # ]

Open new Safari window with an AppleScript application
Authored by: ChaChi on Dec 10, '03 11:50:23AM

Try this:

tell application "Safari"
if windows is {} then reopen
end tell

Hope this helps...



[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: joeholmes on Dec 10, '03 10:34:25PM
Perfect ...thanks! I've set it to trigger with Keyboard Maestro, so now my F-key of choice switches to Safari and makes sure at least one window is open.

[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: larkost on Dec 10, '03 12:01:58PM
Oohhh.. fun AppleScript for a change!

set aWindowIsOpen to false
tell application "Safari"
	repeat with thisWindow in windows
		if (not miniaturized of thisWindow) then
			set aWindowIsOpen to true
		end if
	end repeat
	
	if (aWindowIsOpen) then
		activate
	else
		make new document with properties {URL:"http://www.macosxhints.com"}
	end if
end tell
Clicking on this script should do what you want.

[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: larkost on Dec 10, '03 11:50:07AM
Rather than using UI scripting, why not just rely on the AppleEvents that Safari already supports? Here is a quick example that will do what you want:

tell application "Safari"
	make new document with properties {URL:"http://www.macosxhints.com"}
end tell


[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: Laqua on Dec 27, '04 03:15:41PM

For some reason this does not work for me. It opens a new window, but leaves the url to whatever the default page is in prefs.

---
Thanks
Laqua



[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: Laqua on Dec 27, '04 03:26:01PM

I did find a solution though, here is what I wound up using.

tell application "Safari"
make new document
set URL of front document to "http://www.bright.net"}
end tell

The reason I did this is because Applescript always seems to put the newly created window in front of all others.

---
Thanks
Laqua



[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: mjmtaiwan on Mar 15, '05 08:40:36PM

This does not work.

[code]
tell application "Safari"
make new document
set URL of front document to {"http://www.bright.net"}
end tell
[/code]

It gives the error: NSUnknownKeyScriptError

---
Old Time Radio from the 30s, 40s and 50s



[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: wookitus on Dec 10, '03 04:04:52PM

This hint gave me the idea that I could have a Safari window open and then click the zoom button twice for me so I don't have to do that every time I open a new window. I have trouble reading Safari's default font sizes so I always have to zoom my windows (setting a minimum font size doesn't do much in Safari). I figure I could do this by using the script from above and then just adding a line to access the "Make Text Bigger" option in the menu. Is there a more efficient way of doing this though where I wouldn't need the assistive devices option checked in the prefs pane?



[ Reply to This | # ]
Open new Safari window with an AppleScript application
Authored by: cooker47 on May 17, '10 03:37:45PM

I am using Safari Version 4.0.5 (4531.22.7) on a late 2005 Mac Mini PPC G4.

I was revisiting this 2003 topic because there are times when I just want to have a new empty Safari window with minimum clicks.

I know how to get a new empty tab using Safari Preferences and Command+T, but I try to avoid using tabs because they seem to drive a lot of overhead (Safari CPU consumption), and having Safari Preferences open new Windows with the same page is useful to keep.

I can still open a new empty tab and then "Move Tab to New Window" but that takes three clicks and I still have to copy and paste the URL I want or else do a search.

This code works great for me:

tell application "Safari"
    make new document with properties {URL:""}
end tell

I saved the script from Script Editor as an application in $HOME/Library/Scripts/Applications/Safari. Then - after using "/Applications/Applescript/AppleScript Utility.app" to enable the "Show Script Menu in menu bar" - I can run the script from the menu bar when Safari is active.

Now the operation takes only two clicks, so this is quite useful.

I also created a web page to collect a few examples of launching Applescript from a link. See here.



[ Reply to This | # ]