CleanShutDown

July 26, 2011

Mac Os X

CleanShutDown

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

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

Download Shell Version

Change log

S

- Uses Shell Scripting instead of GUI Scripting

, , , , , , , , , , , , ,

76 Responses to “CleanShutDown”

  1. Dan Says:

    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

    • Victor A. Says:

      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.

  2. RogueMonk Says:

    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.

    • Victor A. Says:

      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.

  3. Ake Says:

    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)

  4. Guru Bob Says:

    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.

    • Victor A. Says:

      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.

  5. Andre Says:

    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

  6. Andre Says:

    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

    • Victor A. Says:

      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?

      • Andre Says:

        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”

      • Andre Says:

        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

        • Victor A. Says:

          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!

          • Andre Says:

            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

  7. Dan Says:

    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

  8. Dan Says:

    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

  9. mudguts Says:

    Error message > [Can't get window 1 of <> "loginwindow" of application "System Events". Invalid index.] > OK > system reboots

  10. Victor A. Says:

    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.

    • Andre Says:

      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

    • stevsmit Says:

      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.

    • mudguts Says:

      Victor! I’m afraid version 2.0.2 of CleanShutDown didn’t fix the problem either. Please keep trying, though.

  11. Victor A. Says:

    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

  12. Andre Says:

    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

  13. Victor A. Says:

    Uploaded CleanShutDownS. This should fix the problem of GUI scripting having conflicts with some systems.

    CleanRebootS coming this afternoon.

  14. Bob Says:

    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

    • Victor A. Says:

      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

  15. mudguts Says:

    Hi Victor
    Both CleanShutDownS and CleanRebootS work fine for me. You deserve a donation!

    mud

  16. Desmond Foulger Says:

    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.

    • Victor A. Says:

      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?

  17. loui Says:

    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

    • Victor A. Says:

      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

  18. Loui Says:

    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

    • Victor A. Says:

      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!

  19. David Morton Says:

    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?”

    • Victor A. Says:

      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

  20. Jay Fang Says:

    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.

    • Victor A. Says:

      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

  21. paocer Says:

    thanks, works with Lion latest version

  22. Kiki Says:

    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”

  23. R Bryson Says:

    Thank you very much for this! It was exactly what I wanted! Cheers!

  24. Constantine Says:

    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.]

    • Victor A. Says:

      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

  25. Ron Says:

    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

    • Victor A. Says:

      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

  26. Z Says:

    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!

  27. BillN Says:

    With the 10.7.4 update; clean shutdown is no longer a clean shutdown.

    • Victor A. Says:

      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 ;)

  28. Omar Says:

    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

    • Victor A. Says:

      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

      • Omar Says:

        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

  29. Cyberschnaps Says:

    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

Trackbacks/Pingbacks

  1. CleanReboot | Victor Andreoni - July 28, 2011

    [...] up with CleanShutDown, CleanReboot will reboot your system in just one click while disabling the save state [...]

  2. CleanLogOut | Victor Andreoni - July 28, 2011

    [...] another application with the same goal as CleanShutDown and CleanReboot: provide a one-click solution to log out from your account without saving [...]

  3. CleanShutDown | Victor Andreoni | The MacTrack - iPhone, iPad, iPod Touch and Mac News - August 3, 2011

    [...] via CleanShutDown | Victor Andreoni. [...]

  4. Turn off "Reopen windows when logging back in" | Windows - Linux - Mac OSX Guides - August 13, 2011

    [...] for those who’s waiting for the fix in the next update, can use Victor Andreoni’s AppleScript to shutdown the computer without having to uncheck the box. It works for me. Related [...]

  5. Back to (Snow)Leopard - November 1, 2011

    [...] Finalmente una feature di cui arrivo a capire l’utilità; peccato che i miei non chiudano mai nessuna applicazione e che queste si riaprano al prossimo riavvio dal momento che, nello spegnere, trovano già spuntata la casellina che le riapre e regolarmente si scordano di de-spuntarla. Se anche voi volete togliervi il pensiero della suddetta casellina, potete usare questo semplice Applescript di Victor Andreoni, scaricabile da qui. [...]

Leave a Reply

You must be logged in to post a comment.