Keep applications from stealing focus when opening in OS X

By default, OS X applications will launch in front of others, and thereby steal window focus. Here are some options for managing this.

One of the frustrating aspects of using a graphical user interface such as OS X is that when you launch a new application, the system assumes that is the application you wish to bring to the forefront. If you launch multiple applications, sometimes they may take a few moments to load and if you decide to switch focus to another program in the meantime then you chance having the program you launched grab your focus and place windows above those of the application you are working in.

These behaviors can be exceptionally frustrating, and while the basic remedy is to just wait for an application to load before switching focus to other tasks, there are a couple of options to launch programs behind others, or keep the foremost window on top of other programs.

Launching programs behind others
The options for freshly launching a program and having it stay behind others may take a little editing of some program resources or establishment of a few scripts, so it may be best to do this only for the programs you regularly use that you would like to keep in the background during launch.

To have a program always open in the background, you will need to edit a property list in the program package and add a couple of lines to the file. To do this, follow these instructions:

  1. Locate the application package file for the program in question.

  2. Right-click and choose "Show Package Contents."

  3. Navigate to the "Contents" directory within the application and open the "Info.plist" file in a text editor.

  4. Add the following lines to the file (the easiest location is to place them immediately after the <dict> tag, before the first <key> tag at the top of the document):

    <key>LSBackgroundOnly</key>
    <string>True</string>

  5. Save and close the file, and close the application package windows

After doing this, the program should now launch behind others at all times, and then only be brought to the front if you click a window for the program. As a word of warning, editing the contents of applications will break their code signature, which is required for some applications to automatically work with system services like the application firewall. Without an intact code signature, the system may always ask for you to allow applications through the firewall when they are launched, which may be a bit of an inconvenience. Additionally, this may not work for all applications, but it not then you can easily remove the added lines to restore full functionality to the program.

The second option for affecting a launching program is to develop a script that will launch it in the background. The basics for this script are to use the "open" terminal command, with the "-g" flag in the following manner (using iCal as an example):

open -g /Applications/iCal.app

As an alternative to providing the full path name to the application, you can use the "-a" flag as well and then only supply the application name, such as the following:

open -ga iCal

With these commands, you can set up a launcher for your programs either using AppleScript or Automator, where the script runs this command as a shell script to target the application of choice. For example, to create a script that will always launch iCal in the background, open Automator and create a new application workflow. Then add the "run shell script" action to the workflow and paste one of the above commands into the action's script field. Then save the workflow and when you run it, iCal should open behind your current application.

Keeping programs above others
Beside specifically targeting a program to open with a certain behavior, you can approach this problem by targeting specific applications that are already open to remain above the windows for other applications. As is the case with the system's Help viewer, this will make an application's window easy to see even if it loses focus when you have the screen filled with other windows.

The easiest way to target windows to remain on top of others is to use a utility like Afloat. Unfortunately, given the changes to the window manager in Lion, the program is not yet compatible with Apple's latest OS, but if you are using Tiger, Leopard, or Snow Leopard, then you should be able to make good use of this utility. Update: a new build of Afloat is available that ensures compatibility with Lion.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    iPhone running slow?

    Here are some quick fixes for some of the most common problem in iOS 7.