Script to stop Mountain Lion from reopening apps files after restart

IO believe this topic is already somewhere here but I am unable to find it
Is there a script, a terminal solution, some software which will allow me not to relunch and open all the files as I start my machine?
I tried the General Option but it does nothing to me. I often work with many apps and I have no time to close them all before shutting down
Also it really annoys me to open my computer whit coworkers around able to peep my screen and see what I do
I have nothing to hide but still it annoys me tremendously
Could anyone help? Googling did not help although I tried to follow everything I found
Any help will be a blessing as already I sent this feedback to Apple and they didn’t even reply

Regards to all

Dan

Hi danwan,

I use this:

set my_name to my name
tell application "System Events"
	set visible of every process to true
	delay 0.5
	set proc_list to (name of every process whose visible is true and name is not "Finder" and name is not my_name)
end tell
repeat with this_proc in proc_list
	tell application this_proc to quit saving no
end repeat

I don’t use the part with the ‘quit saving no’, because I like the application to ask to save changes.

Edited: also, you might need to change your System Preferences under the General pane.

Edited: also note that some apps don’t have the same name as their processes. You might need to add some checks for these apps.

Edited: I forgot. When those apps with different process names occur, the system asks you to navigate to the app.

gl,
kel

jI’ve been meaning to fix the script for processes with different names:

set my_name to my name
tell application "System Events"
	set visible of every process to true
	delay 0.5
	set proc_list to (displayed name of every process whose visible is true and name is not "Finder" and name is not my_name)
end tell
repeat with this_proc in proc_list
	tell application this_proc to quit -- saving no
end repeat

I didn’t test it a lot and you can uncomment the ‘saving no’.

gl,
kel

I don’t quit directly but run this script saved as an application.

set p2pane to ((path to library folder from system domain as text) & "PreferencePanes:StartupDisk.prefPane") as alias
# Get four localized strings
set cancelBtn to localized string "CANCEL" in bundle p2pane
set restartBtn to localized string "RESTART" in bundle p2pane
set shutdownBtn to localized string "SHUTDOWN" in bundle p2pane
set prompt to localized string "RESTART?" in bundle p2pane

display dialog prompt buttons {cancelBtn, restartBtn, shutdownBtn} default button 3 cancel button 1
set |éteindre| to (button returned of result) is shutdownBtn

(path to library folder from user domain as text)
tell application "System Events"
	tell folder (result & "Saved Application State")
		delete every folder
		(*
# Attempt to delete the alias com.apple.mail.savedState
# but it doesn't delete it
	set maybe to name of every disk item whose name ends with ".savedState"
	repeat with anAlias in maybe
		try
			delete disk item anAlias
		end try
	end repeat
	*)
	end tell
	if |éteindre| then
		shut down
	else
		restart
	end if
end tell

Dan, to prevent coworkers from peeping your display. I can’t see nothing but another solution than a script :slight_smile:
I can think of more general solutions such as having your Mac not booting straight in your Account (so having the screen stuck at the Session opening dialog window). Then you’d simply open your personal session yourself. You can also have the screensaver launched after a few time and your password required straight afterwards. You can still manually delete the folder named “Saved Application State” at the end of the day. What else? Set the shutdown schedule so your Mac shuts down at the set time every day or whenever you wish. Most of these don’t require a script. Yet you can script a lot. HTH.

Model: PowerMac
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)

Save this script as an application and run it when you want to quit or restart.

In the late one, I forgot that shutdown or restart automatically saved the applications states.
This time, the script quit every open application (minus Finder and the script itself)
Then it delete the Saved Applications State datas to get rid of what was just saved or what was saved before
and at last, shutdown or restart according to your choice.

set myName to my name

tell application "System Events"
	set visible of every process to true
	--delay 0.5
	set theAppsNames to (displayed name of every process whose visible is true and name is not "Finder" and name is not myName)
end tell

repeat with anApp in theAppsNames
	tell application anApp to quit
end repeat
# Now the applications are cleanly closed
# We may delete the Saved Application State datas
set p2AS to (path to library folder from user domain as text) & "Saved Application State:"
tell application "System Events" to tell folder result  # EDITED
	delete every folder
	(*
# Attempt to delete the alias com.apple.mail.savedState
# but it doesn't delete it
   set maybe to name of every disk item whose name ends with ".savedState"
   repeat with anAlias in maybe
       try
           delete disk item anAlias
       end try
   end repeat
   *)
end tell # System Events .

set p2pane to ((path to library folder from system domain as text) & "PreferencePanes:StartupDisk.prefPane") as alias
# Get four localized strings
set cancelBtn to localized string "CANCEL" in bundle p2pane
set restartBtn to localized string "RESTART" in bundle p2pane
set shutdownBtn to localized string "SHUTDOWN" in bundle p2pane
set prompt to localized string "RESTART?" in bundle p2pane

display dialog prompt buttons {cancelBtn, restartBtn, shutdownBtn} default button 3 cancel button 1
set |éteindre| to (button returned of result) is shutdownBtn
# Now we may trigger shutdown or restart cleanly, open documents were saved above
tell application "System Events"
	if |éteindre| then
		shut down
	else
		restart
	end if
end tell # System Events

KOENIG Yvan (VALLAURIS, France) jeudi 5 septembre 2013 11:37:26

Hi Yvan,

there is a convenient “shortcut” to /System/Library/PreferencePanes


set p2pane to ((path to system preferences as text) & "StartupDisk.prefPane") as alias

Hello.

If peeping from coworkers is a problem, then you can buy a filter to put in front of your display, that renders your screen unreadable from a few feets away. Great for open office landscapes. :slight_smile:

It’s a good idea to make time to close applications properly, especially those with open documents.

If emptying the “Saved Application State” folder actually works for the purpose in hand, an alternative approach may be simply to empty it once and then lock it. Not being able to create the “.savedState” folders in the first place doesn’t appear to have any negative effects on running applications.

Thanks Nigel.
Unfortunately my occupation is teaching film production, and also producing documentaries or short films. Therefore not onlyI need to use more than one application at the time - Often linked through scripts (mostly suggested, or even build to help from this community)-, but I might have one or two machines on. it happens that I need to move away quickly and suddenly, as something “crucial, or reported as crucial” happens.
As a professor this “Lion and post Lion” idiotic feature does not bother me so much, just as many others which changed my working ways, apps made for me and so on (I come all the way from the Still working Apple ][, back a few decades ago. But during Production time is simply impossible, especially because I cannot count on adequate support as money in this business multiplies by the thousands …
But of course you are right.
So thank you for taking care of Macscripter so well and for everyone.
Dan

Thanks everyone for helping me
I will try everything as suggested and keep you posted …

Regards to everyone

Thanks Ivan however I had to change enable the part:

set maybe to name of every disk item whose name ends with “.savedState”

and perviously modify:

set p2AS to (path to library folder from user domain as text) & “Saved Application State:”
tell application “System Events” to tell folder (result & “Saved Application State”)

to
set p2AS to (path to library folder from user domain as text) & “Saved Application State:”
tell application “System Events” to tell folder (result)

else I kept getting this error:

System Events got an error: Can’t get folder “Mountain Lion:Users:danwan:Library:Saved Application State:Saved Application State”.

FInally I get to your final dialogue and the script seems to work very well
I only wonder (being a perennial incompetent if these changes may create some side effects to the whole script

Thanks again

Dan

No problem

I made a change in an instruction and didn’t made the required counterpart.

I will edit my message. ( DONE )

I delete the contents of the Saved Applications State folder since the delivery of Lion with no odd effects.

KOENIG Yvan (VALLAURIS, France) jeudi 5 septembre 2013 19:30:44

Thanks Stephan

I missed this one.

KOENIG Yvan (VALLAURIS, France) jeudi 5 septembre 2013 19:36:34