Disable restored windows when re-opening specific apps

There’s a great tip over on the Macs in Chemistry blog on how to stop Lion from restoring your previous windows when re-opening specific apps. In System Preferences there’s a checkbox for turning this on and off globally, but using a handy little AppleScript you can disable it on a per-app basis.

One of the new features in Lion is the ability of applications to resume activity at the point an application was quit. This means that when you reopen an application it will open and display all the documents you were editing in the state when you quit the application. This is obviously very useful but there are times when you may not want confidential documents automatically opened and displayed when you open an application.

Here’s an example of the script for Safari:


set statePath to POSIX path of (path to home folder) & "Library/Saved Application State/com.apple.Safari.savedState"

set quotedPath to quoted form of statePath
try
   do shell script "test -w " & quotedPath
    set current to "enabled"
on error
   set current to "disabled"
end try

if current is "enabled" then
   tell application "Finder" to delete every item of ((POSIX file statePath) as alias)
    do shell script "chmod u-w " & quotedPath
    display dialog "Disabled window restore for Safari" buttons "OK" default button 1
else
   do shell script "chmod u+w " & quotedPath
    display dialog "Enabled window restore for Safari" buttons "OK" default button 1
end if


Head over to macinchem.org for a quick explanation of how to add it to your scripts menu to give you an easy place to access it.