How to manage file-in-use errors for OS X applications upgrades
While there are several approaches to managing "in-use" errors in OS X, you can often make use of the Trash to temporarily store these files so as not to disturb your workflow.
Many applications in OS X are self-contained packages that you install by dragging and dropping to your Applications folder. Since all of the program's required resources are within this package, when you need to update or upgrade the software, you can download the update and drag it to your Applications folder, replacing the current version you have installed.
This method of managing programs is very straightforward and usually works without error; however, at times you might run into problems when upgrading where the system gives an warning claiming that a component of the application is in use.
Recently I ran into such a situation when version 4.0 of the free and popular text editor TextWrangler was released last week. After downloading the update and mounting the disk image, I dragged it to the Applications folder, but when confirming to replace the existing version of TextWrangler, the system issued a warning that stated, "The operation can't be completed because the item 'TextWranglerTextDoc.icns' is in use."
The TextWranglerTextDoc.icns file is a small icon image used by OS X to change the Finder image of relevant documents that are handled by this program. This allows you to quickly identify from the icon that TextWrangler will be used to handle that specific file, as opposed to others like TextEdit, Pages, or Word which might also be able to handle the file.
As with most files, any program or system service that access this file within TextWrangler should only apply a temporary lock to it while it is opened for reading, and once its contents are in memory, then the file's lock should be released so it can be used or modified elsewhere. However, if an error occurs that prevents the file lock from being released, then it may be maintained until the process holding the lock on the file is quit.
If an "in-use" error such as this happens and prevents you from deleting a file, modifying it, or replacing it, then you can quickly look up which process is using the file by way of the Terminal's "lsof" command (this stands for "list open files"). Unfortunately there are many system files that are intended to be opened and not released, so running this command in the Terminal by itself will list all open files on the system and make locating the one in question difficult to do. However, you can narrow the results to the single entry of where the file in question is, by filtering it with the "grep" command, as follows:
lsof | grep TextWranglerTextDoc.icns
This will output a single line that shows information about the process which has a lock on the file, such as its name, process ID, and username under which it is running, and using this information you can determine how to best solve the problem.
While in many cases you can simply restart the system to guarantee that the system will clear the open files, if multiple users are logged on or if you are in the middle of work. then you might prefer to avoid doing this.
With regards to the TextWrangler installation error, you can see the program using the file is SystemUIS (or the System UI Server), which is a background service running as the user "tkessler" and is used to display and managing many user interface elements.
In this case the program is running under the current user account, but is a background process instead of being a standard application like TextEdit, iCal, or Disk Utility. With standard applications you could try quitting the program to clear the open file lock, but being a background program makes arbitrarily quitting it more risky, as it could destabilize the user account. It turns out in many cases background processes such as the SystemUIServer will automatically respawn if forced to quit Activity Monitor; however, without knowing this the safest bet if you are trying to preserve your current workflow is to assume the arbitrary quitting of any background process might destabilize the system.
Because of this, if you run into a situation where you cannot overwrite a file because it is in use, then the next best route is to make use of the OS X Trash or other folders on the system to quickly work around the problem. Even though the file in question is in use, you can still move it to a new location on the drive without error and thereby free up its old location for placement of your new files. Therefore, in the case of TextWrangler's upgrade, applying this workaround was simply a matter of moving the old version of the program to the trash, and then copying the new version to the Applications folder.
In doing this, the older version of the program in the Trash will not run or otherwise be accessed, even though it also cannot be deleted because of the existing file locks on components of the program. However, meanwhile the new version of the program in the /Applications folder can be launched and used as needed.
By using the OS X Trash in this way, you will be able to continue your workflow and then delete the old version of the program when you are next able to restart or log out and log back into your system, since these actions will quit the programs and services that are holding the lock on the files and prevent them from being deleted.