Script Implementation for Disabling Re-Opening Previous Login

Ok guys,

I'm just getting back to this amongst several other projects, but I thought I'd re-address it. I'm creating the script to disable windows from the previous login under 10.7. In order to do this it seems I need to create the same script for applications that launch and create the associated plist.

Example:

sudo defaults write com.apple.Safari NSQuitAlwaysKeepsWindows -bool false

I don't know of another way around this, but to create the same script multiple times for the additional applications into a bash script.

Heres my example of the script in totally that I've created:

#!/bin/bash
# Disable Native Mac Applications from ReOpening from previous login

sudo defaults write com.apple.Safari NSQuitAlwaysKeepsWindows -bool false
sudo defaults write org.mozilla.firefox NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.google.Chrome NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.QuickTimePlayerX NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.Preview NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.TextEdit NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.Mail NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.iTunes NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.iPhoto NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.TimeMachine NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.Preview NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.iCal NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.appstore NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.iWork NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.apple.AddressBook NSQuitAlwaysKeepsWindows -bool false

# Disable Third-party Mac Applications from ReOpening from previous login
sudo defaults write com.microsoft.Outlook NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.microsoft.Powerpoint NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.microsoft.Word NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.microsoft.Excel NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.microsoft.Messenger NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.microsoft.rdc NSQuitAlwaysKeepsWindows -bool false
sudo defaults write com.adobe.Reader NSQuitAlwaysKeepsWindows -bool false

# Will exit with status of last command.

exit

When I run the script, I get the following error:

sudo: no tty present and no askpass program specified

I'd like to package all this in a bash script or perl script and perform a single shot in terminal.

It means that sudo needs to ask for a password, and can't, because your program isn't run from a terminal.

Think globally instead of writing your preference key to each application domain. Use either the user's GlobalPreferences or the System's GlobalPreferences.

This would be best achieved without the GUI running.

Thanks, xbin. The only GlobalPreferences I'm finding might possibly be an "MCX" file within the user template. Is there another place I should search?

---------- Post updated at 11:42 AM ---------- Previous update was at 11:40 AM ----------

Would the following work, Corona688?

$ADMINS ALL = NOPASSWD: LOCATE

I suggest that you read the defaults manual. Focus on the four paragraphs under the DESCRIPTION heading. The file- /Library/Preferences/.GlobalPreferences is modified when you write to the NSGlobalDomain. So,

defaults write NSGlobalDomain NSQuitAlwaysKeepsWindows -bool 'false' 

would write the preference key "NSQuitAlwaysKeepsWindows" and the value to /Library/Preferences/.GlobalPreferences

Note: I did not test this. Use at your own risk. It would be advisable to understand how to remove the preference key should any problems arise.

How are you deploying this scripted solution?
Typically, you do not enter "sudo" in a script, as it will require someone enter the password of an administrator. If I'm creating a script like this, I leave off the "sudo" part and just have the basic commands in it.
If I run the script in Terminal.app, I will then use the "sudo" command to run the script, thereby granting my admin access to all the commands in the script. So, script named "defaultchanger.sh" would be run by issuing the command "sudo defaultchanger.sh"

If you are deploying the script remotely, then the deployment solution will likely have a mechanism that will allow it to be run with elevated privileges. Again. No "sudo" in the script would be required.

Good luck! :slight_smile:

Look into Loginhooks ... That will run as root and you do not need the sudo command. It runs at login, and the variable $1 will expand to the user name of the current user, but the script is always run as root. You add a loginhook by a defaults write command to loginwindow.plist I believe, check it out on Google.