Macs in Chemistry

Insanely great science

 

Delete Restore folders for selected applications

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.

As far as I can tell there is no option to switch this off in an application by application basis so the only option seems be to switch this off feature off completely. In the “General” section of the “System Preferences” uncheck the box labelled “Restore windows when quitting and re-opening apps”.

restore

Digging around it appears the folders that contain the restore information are in the user library in a folder called “Saved Application State”. To view the user library in Lion you need to click on the “Go” menu in the “Finder” with the Option key held down. Removing the appropriate application restore folder means that when you subsequently open the application none of the previous documents are opened automatically.

resorefolders

Using an Applescript

Whilst this offers a solution it would be useful to be able to do this without navigating to the appropriate folders, one option is to use an Applescript. There are only a limited number of applications or specific occasions for which I don’t want the restore function to be available so the applescript below deletes the Restore folders for those applications. You will need to replace “username” with your username, and you can simply add any other applications to the list by adding the appropriate Restore folder.

try
  tell application "Finder" to delete ((POSIX file "/Users/username/Library/Saved Application State/com.microsoft.Word.savedState") as alias)
end try
try
  tell application "Finder" to delete ((POSIX file "/Users/username/Library/Saved Application State/com.apple.iWork.Pages.savedState") as alias)
end try

Whilst you could put the applescript anywhere I use the script menu, if you don't have this running go to the Applescript Editor preferences. You should get the dialog shown below, check the box "Show Script menu in menu bar", and the applescript icon should appear in the top right group of menu bar icons.

applescript_prefs-2

Matt over at MacOSX Tips has sent me an alternative script, in this rather than deleting the restore file it is simply enabled or disabled. The script below demonstrates the implementation 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