After using MacOsX Lion for a while, one of the most annoying “features” for me is the saved state, where apps are automatically launched after resetting or shutting down your Mac.
The idea is pretty good, but the annoyance comes when you have to uncheck a little box each and every time you turn off or restart your Mac in order to disable it.
After digging around the defaults configuration, I found the following string:
TALLogoutSavesState = 1
According to my common sense, changing that 1 for a 0 should disable the function. It actually worked, but the configuration was lost after each reboot, meaning that you had to go into terminal and write the “0” into that string every time you wanted to restart or shut down your computer. At the end, I was easier to uncheck the little box.
However, I built a small applescript that will write the 0 for you, and then turn off your Mac, and all this with just one simple click on the icon of the app.
As it came very handy to me, I decided to share it online. So please feel free to download it, report any bugs or comments on this post, and share with your friends!
GUI Scripting Version (Somewhat Cleaner)
Note: To fix the “Access to assistive devices disabled.” error follow these simple steps:
Go into System Preferences -> Universal Access -> Check box at bottom for “Enable Access for assistive Devices”.
After that, manually log out or reboot your system for changes to take effect. (Thanks Mccleodx for pointing this out!)
Download 2.0.2
Change log
2.0.2
– Increased delay time to fix “Can’t get windSow 1…” error.
2.0.1
– Script now compatible with any language.
2.0
– Changed working method. Instead of editing the defaults of “TALLogoutSavesState”, the application now interacts with Finder as if it was the user clicking the checkbox and then clicking the Shut Down option.
1.0
– Initial Release
Shell Script Version (Use if GUI version conflicts with your system)
Download Shell Version
Change log
S
– Uses Shell Scripting instead of GUI Scripting
August 5, 2011 at 3:32 pm
Victor,
Added to your code and have a couple of scripts for you to try and post if you want. Get back to me!
-Dan
August 5, 2011 at 7:16 pm
Dan,
If you like, lets discuss here so everybody can benefit from it.
Feel free to post links to your code in here. After all, the idea of this page is to be as helpful as possible for new developers and people interested in the topic.
August 13, 2011 at 12:11 pm
Thanks for sharing this. However, I am having an issue with it. When I click it, I get the following message box come up: “Access to assistive devices disabled.” I don’t know what that means. Thanks.
August 13, 2011 at 12:31 pm
Hello there,
That error means that you have accessibility disabled for devices. To enable it, follow these simple steps:
Go into System Preferences -> Personal -> Universal Access -> Check box at bottom for “Enable Access for Enabled Devices”.
After that, manually log out or reboot your system for changes to take effect.
August 14, 2011 at 4:32 am
The scripts seems to only work on a english version of mac os. I looked at the script, and from what I understand it references finder’s objects by name. This will fail on localized versions. Is there another way of doing it?
I tried altering the reference to menu item “Restart…” to what it is called in my language, and that worked well, but there are more references that I cannot fix (“invalid index” for window 1 of loginwindow)
August 14, 2011 at 1:37 pm
Hello Ake,
I just uploaded version 2.0.1. This one should work in any language. Please give it a try and let me know. If it works fine, I’ll also update CleanReboot and CleanLogout to include this fix.
Thanks,
Victor
August 14, 2011 at 2:30 pm
Hi, tested 2.0.1, it worked fine! Yay ^__^/
August 14, 2011 at 3:34 pm
Great! I’ll update the other applications to include this fix when I get home!
December 5, 2011 at 12:07 pm
I get the message ‘Can’t get window 1 of class prcs “loginwindow” of application “System Events”. Invalid index.
December 5, 2011 at 12:56 pm
Try the Shell Script version. That one should work for those getting that error.
August 17, 2011 at 1:57 pm
Couldn’t you set TALLogoutSavesState to zero at login with a startup script? It should then hold it until the user reboots or shuts down. Then the user wouldn’t need to use another app to reboot or shut down.
I “upgraded” to Snow Leopard after Lion made my machine essentially useless, so I can’t test it.
August 17, 2011 at 6:23 pm
That will also work. However, version 2.0 of the script ditched the TalLogoutSaveState and now uses GUI Scripting. Many criticize the use of GUI Scripting, but I prefered to use it instead of forcing the change of that defaults value every time the user reboots or shuts down their computer.
When I get some time, I’ll also upload a startup script for those who prefer to change the defaults instead of using GUI scripting.
August 24, 2011 at 2:55 am
Hi, I keep on getting the following error – “can’t get windows 1 of <> “loginwindows” of application “system events” invalid index.
I have re-formated my computer and did a clean install but this has not made any difference – any help would be greatly appreciated as I LOVE the program, not only because of Lion’s shut down problem but also because it makes for such a handy shut down on the desktop
Thanks
Andre
August 24, 2011 at 7:47 am
Hi Victor,
Just did a complete reformat and install again on my mac – made sure Assertive devices are working and still the same long error “cant get window 1 of <>, the extention worked so well – what the heck is my mac’s problem or can you possibly make available some earlier versions – PLEASE
August 24, 2011 at 11:20 am
Hi Andre,
I just downloaded and tried the app on a friend’s computer, and I’m not able to reproduce the error. Let me ask you some questions so I can try to reproduce it and help you out.
Just to double check, did you do this?
– Go into System Preferences -> Personal -> Universal Access -> Check box at bottom for “Enable Access for Enabled Devices”.
Also, in what language is your Mac?
August 25, 2011 at 2:12 am
Hi Victor, Thanks for replying – much appreciated and as far as the software goes even more appreciated, a donation will be on it’s way the minute I can use it again.
I made 100% that all acertive devices are selected (which they are), even reselected them just in case. The computer langauge is English (American).
Just a thought what are the chances that if the software was installed prior to the Mac update it would work but if installed after the newest Lion Updtae the error might occur.
Every time I have tried to install it, it has been from scratch – computer formatted disc swyped and installed from scratch.
Ran it again now and the error received is –
error “System Events got an error: Can’t get window 1 of process “loginwindow”. Invalid index.” number -1719 from window 1 of process “loginwindow”
August 25, 2011 at 3:31 am
Victor, I will make a donation during the day – manage to solve the problem, it seems that if the software is installed prior to Lions 7.01 update it will work LIKE A DREAM. if one updates the computer then installs the software it won’t work.
Why that is I will make your problem but after several formats and clean installs I found the solution.
Thanks a million
Andre
August 25, 2011 at 11:47 am
Great to hear that it worked out for you!
Still, that situation is kind of strange. I’ll look into it to figure out what is causing the error.
Thank you very much. Donations are not needed, but greatly appreciated!
August 26, 2011 at 1:04 am
Hi Victor –
Apologize for the long periods between replies but that is because I am based in South Africa. The program has once again started reproducing the error after the updates were run.
I never bothered running the updates immediately, it seems to be either the upgrade causing the problem or software installed on the computer BUT at the same time all the software I use is pretty much what everybody uses and not a single program that I can imagine affecting the working of the software.
If possible a download link or zipped version of the older versions would be appreciated and tested.
Thanks again for consistent support and comments about the donation, a person willing to assist in the way you have deserves a little gift.
Andre
August 25, 2011 at 7:40 pm
Don’t have a link for the code. Hate to attach it here, but here it goes……..
[Code present in follow-up comment – Victor A.]
— Script created by Daniel Ball with some code from Victor Andreoni from http://www.victorandreoni.com/blog
August 25, 2011 at 7:40 pm
Here is the logoff version:
[Code present on follow-up comment – Victor A.]
— Script created by Daniel Ball with some code from Victor Andreoni from http://www.victorandreoni.com/blog
September 19, 2011 at 12:34 pm
Thanks for the code Dan. Sorry for approving the comment so late, but it was on page 2, and haven’t seen it before!
If you could create a .html file with the code or send me the actual script via email I could uploaded here. That way, It would be cleaner and easier for users to understand and use the code!
September 28, 2011 at 6:19 pm
Forgot to come back and check on updates…..here is a link to download my applescript apps I made.
Thanks,
Dan
http://dl.dropbox.com/u/18068810/Lion_Logoff_Scripts.zip
September 28, 2011 at 6:21 pm
Or here are just the 2.0 versions that are my latest ones. The other post is all the files I was messing with. Versions 1.0, 2.0 and your scripts.
http://dl.dropbox.com/u/18068810/LionSavedStateScripts.zip
August 28, 2011 at 6:24 am
Error message > [Can’t get window 1 of <> “loginwindow” of application “System Events”. Invalid index.] > OK > system reboots
August 29, 2011 at 7:06 pm
Hey guys.
Excuse me for my delay, but I just got to college and things have been a little hectic.
I just uploaded version 2.0.2 of the script, which increases the delay time before attempting to uncheck the box and click the shutdown option.
Please give it a try and let me know if it fixes your issues!
If it doesn’t, I guess I’ll upload a version based on the TallLogOutSaveState default, which should definitively fix this issue.
August 31, 2011 at 11:26 am
Hi Victor,
Thanks for the reply – bad news is that the problem persists and once again installed on a clean format Mac Book Air this time.
Your software is not the only one hit by the sudden failure- I see the App Store suddenly saw a number of updates in software especially my Uninstaller which was suddenly unable to uninstall.
Will you please upload the other mentioned version for us
Thanks
Andre
August 31, 2011 at 12:05 pm
Victor,
ver 2.0.2 still produces an error on my OS X Lion v10.7.1
Can’t get window 1 of <> “loginwindow” of application “System Events”. Invalid index.
Like mudguts, I press OK and the system reboots.
September 6, 2011 at 12:58 pm
Victor! I’m afraid version 2.0.2 of CleanShutDown didn’t fix the problem either. Please keep trying, though.
September 7, 2011 at 1:01 am
Hey guys.
Thanks for the continuous feedback. Since I haven’t been able to reproduce the error, I rely on your comments to know the progress.
As I told you, I began college last week, and am already one week away from mid-terms. As soon as I find some time, I’ll upload the version that uses the shell command instead of GUI scripting.
Thanks for your patience.
Victor
September 17, 2011 at 12:06 pm
Victor –
After formatting the computer 6 times in order to try and replicate the cause of the shit down error I FINALLY found it .
Select SYSTEM PREFERENCE
Then scroll down to NUMBER OF RECENT ITEMS
The minute all three are changed to NONE your software fails
As long as any of the items has a numerical value the CLEANSHUTDOWN software functions as normal.
why this is I will leave in your hands – for now I will leave my values as per Apple default – can’t even say that I know why I change it to 0 but I can use your software again – YEA.
Maybe you wish to publish this solution on your web site
Regards and good luck with your studies
Andre
September 19, 2011 at 9:46 am
Thanks for the info Andre, will look into that. You really are a good beta tester!!
September 22, 2011 at 1:52 pm
Thanks Victor –
It was actually great fun and it still is one mean piece of software. Wishing you ll the best with future development
Andre
September 22, 2011 at 4:02 pm
Thanks Andre!
September 19, 2011 at 9:47 am
Uploaded CleanShutDownS. This should fix the problem of GUI scripting having conflicts with some systems.
CleanRebootS coming this afternoon.
September 27, 2011 at 5:55 pm
The script version works great!
Thanks!
September 20, 2011 at 8:20 am
Hi Victor I have found CleanShutdown and CleanReboot very useful and will continue to use them until Apple sort out the propblem. Would it be possible to change the colour of the icons IE: red for shutdown and green for reboot as when I put them in the dock or the desktop you have to use a light background or it can be difficult to see the icons.
Many thanks for the APP’S
Bob Pritchard
September 20, 2011 at 3:28 pm
Hi Bob.
You can actually change the icons to the ones you like yourself. It’s pretty easy. Let me explain you how:
1) Find the icon that you like online (google,etc) and download it
2) Go to This Page and convert your icon into a .icns format
3) Right-click on the script that you want to modify, and select show package contents.
4) Navigate to /Contents/Resources/
5) Replace the pre-existen icon with the one you created.
If you need any more help please let me know. If you’re having trouble changing the icon, give me a link to the icon you like and I can create a version of the script with that icon for you.
Victor
September 21, 2011 at 2:07 pm
Many thanks Victor I will give it a try ang get back to you
Bob
September 21, 2011 at 3:21 pm
Ok Victor great job worked a treat.
Many thanks for your help and keep up the good work.
Bob
September 21, 2011 at 4:32 pm
Good to hear that everything went fine!
Thanks for the support!
September 23, 2011 at 7:31 pm
Hi Victor
Both CleanShutDownS and CleanRebootS work fine for me. You deserve a donation!
mud
October 22, 2011 at 4:58 pm
Since upgrading to 10.7.2 Cleanshutdown doesnt work. This is such a useful “app” that I’d love to see it updated to work again.
October 22, 2011 at 6:59 pm
Hello Desmond.
The script version of the app should work with any version of Lion released so far. I just added an extra SSD to my MacBook Pro and did a clean download of the app on Lion 10.2. It works just fine. Do you get any kind of error message?
October 24, 2011 at 4:33 pm
Hi Victor,
I’ve just bought a brand new MacBook Air (Mac OS X Lion 10.7) and this ‘reopen windows when logging back in’ is making me very annoyed! I had hoped that your clean shutdown might stop this but it does not seem to be working for me. Is there anything I might be doing wrong? There are no error messages but once I click the icon the computer switches itself off immediately but when I switch back on the problem still exists.
Regards
Loui
October 24, 2011 at 5:51 pm
Hello Loui.
Which version of the script are you using? The S one or the regular one?
Also, keep in mind that Finder is always launched when OS X starts, so it’s normal that Finder windows remain open.
Furthermore, remember that this script prevents app from launching, not from keeping their past state when they are launched (i.e. when closing safari with command Q and then opening it, it will display the previous open page). To avoid this, go to System Preferences, General and uncheck the box “Restore windows when quitting and re-opening apps”.
Please let me know if any of these comments help you fix the problem, or if the script is actually not working as it should.
Victor
October 25, 2011 at 2:56 pm
Hi Victor,
Thanks for your reply!
I downloaded the regular version first and then when I didn’t get the desired result I downloaded the S version. Maybe I have misunderstood what the script it is supposed to do but I will try and explain what my problem is. On my old Mac if I was using Word or Photoshop and I closed the application (using the little cross in the corner) the application would disappear but the little blue light would stay on above the application in the dock. If I switched my computer off and back on later the applications would not relaunch and the little light would have disappeared even if the blue lights had been showing when I switched off. With my new mac if the little lights are showing above the applications in the dock when I switch off my computer, when I switch back on the applications are launched automatically. If I use cmdQ to quit applications the blue light does not remain and the applications do not relaunch when I switch back on. I suppose the difference is that my old mac used to quit all my applications for me on shut down but my new mac keeps them open and relaunches them when I switch back on. Sorry to ramble… maybe I’ll just have to get used to using cmdQ rather than the little cross in the corner!
Regards
Loui
December 5, 2011 at 1:01 pm
This script is supposed to take care of that blue light even if you don’t hit command-Q. Maybe the last update of Lion broke the script. Do you mind telling me your current system version?
Yes, it can be annoying, but hitting command-Q for all your open apps before turning off the computer is a workaround for this “feature”.
Another thing you could do is uncheck the box that appears on the shut-down prompt. By doing so, all apps should terminate and don’t re-launch after rebooting the system. In fact, these scripts simulate the unchecking of that box. But then again, it can be annoying because Lion won’t remember your preference and you will have to uncheck the box every time you shut down the computer.
Sorry for the late reply, college has taken most of my free time!
December 8, 2011 at 4:11 pm
Neat couple of useful apps.
Is it possible to have the icons appear in the Menu Bar, aka Menulets?
Seems to me it would be more intuitive to be able to activate shutdown from there.
I have a 27″ iMac so, of course, I have yards of room.
I am not a programmer, Google search yielded lots of hits but no way to actually do it!
As they say on Top Gear “how hard can it be?”
March 27, 2012 at 1:44 pm
I have to check if it’s possible. Sometimes, menus are only for proprietary code from the original developer.
When I get some time (i.e. college ends :p) I will take a look into it.
Victor
December 13, 2011 at 10:39 am
I downloaded the CleanShutDown and installed. Then pop out a window says: “Can’t get window 1 of “loginwindow” of application “System Events”. Invalid index.” with “Edit” and “OK” checkboxes. And my MacBook Pro shut down automatically. I turned on again, the off, the I turned it on again. Another window pot out: “You haven’t been logged out because the application CleanShutDown failed to quit. To try logging out again, quit CleanShutDown and choose Apple menu > Log Out”
I would like to know what happened to my computer and what the solution is.
March 27, 2012 at 1:53 pm
In order to avoid getting the error related to window 1, download the S version of the script. (S standing for shell).
The error is caused due to changes in the layout of menu options within OS X versions. The scripts without the suffix S use Apple-integrated tools for “clicking” with commands. The scripts that do have the S suffix use shell commands, a no so “clean” way of doing it but definitively more effective.
Regarding your error, a couple of reboots should clean everything up. If still having issues, reset the PRAM of your Mac by holding the option, control, R and P keys at the same time when turning on the computer.
Victor
January 4, 2012 at 1:45 am
thanks, works with Lion latest version
March 27, 2012 at 1:54 pm
Thanks for the feedback.
Victor
January 6, 2012 at 3:47 am
To the author: there’s a new apple menu item “Location” in 10.7.2. I think you should put that into consideration or one will get errors e.g. cleanreboot will put the Mac to sleep; cleanshutdown won’t work.
A quick solution is, if your on 10.7.2 is to edit the main script-increase by one the part “click menu item”
March 27, 2012 at 1:56 pm
Thanks again. Replied to your comment in the CleanReboot post.
Victor
January 7, 2012 at 12:40 pm
Thank you very much for this! It was exactly what I wanted! Cheers!
March 27, 2012 at 1:56 pm
You’re welcome!
Victor
March 12, 2012 at 5:15 am
I Have a problem with Clean Shut Down, which has worked perfectly for a while now (and I even made a small contribution for it, I was so pleased). Now it seems corrupted, and I get the message: Can’t get window 1 of “c;ass prcs” “loginwindow” of application “System Events”.Invalid index”
The result is it keeps shutting down, re-opening. If I Force Close, it shuts down (and re-opens)
I can’t find where to delete it, and start again with a later version.
Please help, urgently – Mac AirBook 10.7.3 going crazy. email, please, [Email Removed so User Doesn’t get Spam – Victor A.]
March 27, 2012 at 2:07 pm
I’m very sorry for the delay in the response. The emails from this mailbox weren’t coming through to my smartphone, and college has kept be so busy that I haven’t checked the site for a while.
To fix the computer rebooting, first try reseting the PRAM. Turn off your Mac, press and hold the option, control R and P keys simultaneously and turn on the computer. Keep holding the keys until the computer reboots again.
If that doesn’t fix it, please let me know. There are many more things to troubleshoot this, but the simple things first.
Regarding the script not working, try downloading the S version of them. Updates to OS X tend to break the UI-based scripts.
Victor
March 17, 2012 at 4:58 am
Hi
This seems like a silly question, but how can I pin your “CleanShutDown.app” to the dock in o/s 10.7 ? Normally I use the mouse with an active App in the Dock to “keep” it there. But your app is (thankfully) gone almost instantly.
(or install it as an applet?)
Great little App. I thought I was the only one infuriated by that little checkbox nuisance.
cheers
RON
March 27, 2012 at 2:19 pm
Locate the folder with the script (the one you got after decompressing the zip file downloaded from the site). Then, click and hold on the icon of the script. Drag the icon down into the dock of your computer, and the adjacent icons will move aside and give some room for the script’s icon. Let go of the click and the icon will remain in the dock.
Let me know if you need any further help!
Victor
April 13, 2012 at 1:53 pm
Everything works fine. Thanks! But I have a silly question. I want to modify your script but I don’t know where is your script? I downloaded the 2.0.2. I see the icon (CleanShutDown), Readme.rtf, Version 2.0.2.rtf, Changelog.rtf.
I suspect your script is in Version 2.0.2.rtf. But when I open the file, I only see the text “Version:2.0.1”. Where is “TALLogoutSavesState = 0” located ?
Many Thanks!
April 13, 2012 at 2:52 pm
The icon is a bundled .app which holds the script, icon, and some other stuff. If you want to access the actual code, right click on the clean shut down icon and select show package contents.
Victor A.
April 13, 2012 at 6:55 pm
It works! Thanks so much!
May 10, 2012 at 5:30 pm
With the 10.7.4 update; clean shutdown is no longer a clean shutdown.
May 12, 2012 at 7:11 pm
That is only true for the GUI version of the script. If you use the shell version (CleanShutDownS), you will still get a clean shut down.
I guess this also means that the app can now be used for two different purposes 😉
December 13, 2012 at 2:51 am
Hi Victor.
Great that you shared this. I have an issue, I am running Cleanshutdown on Mountain Lion, it shuts down automatically then when I reboot, I log in and it immediately shuts down with a few seconds then when I reboot again it is fine.
Why does it shut down the 2nd time ?
Thanks Omar
December 13, 2012 at 3:03 am
Hi Omar.
I have not tried these scripts on Mountain Lion since I have not made the switch yet. However, I’m guessing that it is due to the fact that Mountain Lion remembers if you want to re-launch the open apps on shutdown (whether you check the box in the shutdown dialog or not). Probably, the operating system remembers that you did not want to reopen the apps from the previous time you shut it down, so the “reopen-apps-when-reboot switch” is set to off. Then, the script is flipping the switch when you run it (there is no way for it to know whether it’s on or off), which results in the switch to be set to on. When this happens, since the script was running before shutting down, the script is opened again by the system. As the script runs again immediately after the computer started, it now flips the switch to off again, and it reboots the computer a second time. Now, since the switch is set to off, the script won’t be relaunched and the computer won’t reboot. This is just a guess since, as I told you, I don’t have upgraded(??) to Mountain Lion yet.
However, these scripts are now longer needed. As I said, Mountain Lion will remember whether you want to reopen the running applications or not. Turning off the computer in the intended way and unchecking that box once should be enough for the OS to remember not to launch the apps.
Victor
December 14, 2012 at 1:44 am
Thanks for taking time out to explain in detail, much appreciated. Actually I found your script very useful not for the clean shutdown but I modified it to set the volume to 0, so when it starts again it will not make the sound as it can be loud especially when I power it on in office or at home late in the night.
If and when you upgrade to Mountain Lion, would be keen to read your experience and modified script if at all possible.
Cheers. Omar
March 13, 2013 at 11:36 am
Thank you soo much. I just spent the whole day trying to find this exact solution and it was driving me mad. I was envisionning the cheap way out of auto launching a second application on Turn on to kill the first one to prevent it from auto turning off again ^^.
You’re a Genius! I’ll be following your stuff! Have a great day!
Cheers,
C