Mac OS X Tip: How to bring a single Safari window to the foreground with a keyboard shortcut

Posted by Pierre Igot in: Macintosh
April 7th, 2005 • 12:21 am

A while ago, I wrote about my futile attempts to find a way to bring a single Safari window to the foreground with a keyboard shortcut without bringing all other Safari windows to the foreground at the same time.

It is of course very easy to bring a single Safari window to the foreground by clicking on it with the mouse. Contrary to what would happen in the classic Mac OS, in Mac OS X if you have an application open in the background with several document windows (like Safari with several web pages in separate windows), when you click on one of the windows only that windows comes to the foreground. The other windows currently open in the same application stay where they are relative to other windows.

But how do you achieve this if your hands are on the keyboard and you want to use a keyboard shortcut?

In my original post, I explained that Mac OS X’s keyboard application switcher (the command-Tab shortcut) brings all of an application’s windows to the foreground, so it’s no good for this purpose.

I tried to design an AppleScript script to achieve this, with GUI scripting, but it didn’t work.

Now it turns out that there are at least two solutions for this. One of them is the donationware utility Witch. It is a preference pane that provides a “window switcher” similar to Mac OS X’s application switcher. It lets you see all currently open windows in each application, and when you select a specific window, it only brings that window to the foreground, without bringing the other windows in the same application to the foreground as well.

Witch is highly customizable and works well in my experience. The only drawback is that the list of available windows can easily become quite long, and browsing up and down the list with the cursor keys will then require multiple key strokes to select the window you want.

Thanks for an e-mail from a Betalogue reader, I have found another solution. It turns out that, for some reason, if you type the following instruction in a Terminal window:

open -a Safari

Mac OS X only brings to the foreground the foremost window in Safari.

And such a Terminal command can easily be incorporated into an AppleScript script:

do shell script "open -a Safari"

Launch Script Editor, insert this single line of code, save the script in a specific location, and then use a utility such as DragThing to assign a system-wide keyboard shortcut to the script. Et voilà!

You now have a keyboard shortcut that brings the foremost Safari window to the foreground without bringing any other Safari window to the foreground at the same time.

Of course, if you want to bring a specific Safari window to the foreground, you need to make sure that it is the foremost window in Safari at the time you use the shortcut. But this is not a problem for me, because that’s typically the situation that I am in. (In other words, I am in Microsoft Word, and I want to bring the foremost Safari window to the foreground without bringing the other Safari windows to the foreground, because they will hide the other windows that I have open in Word — and I always have several Safari windows open at the same time, and I almost always have more than one Word document window open at the same time.)

After I’ve use the web page in the Safari window, I can then just press command-Tab to return to Microsoft Word. This will bring all of Microsoft Word’s windows to the foreground, but that’s not a problem for me, because they are already in the foreground in my situation.

So now I can easily switch between the Word document windows on which I am working and my web-based terminology database and back without lifting my hands from the keyboard. Excellent!

Maybe this will be useful to someone else. I do wonder why the “open -a Safari” command only brings the foremost Safari window to the foreground. There is certainly nothing in the description of the open command that would explain this. But it just happens to work that way — in Mac OS X 10.3 at least.


Comments are closed.

Leave a Reply

Comments are closed.