Skip navigation

Force Logout without "Do you want to save?" Prompt

1069 Views 5 Replies Latest reply: Aug 4, 2012 10:36 AM by permix RSS
permix Calculating status...
Currently Being Moderated
Aug 2, 2012 7:57 AM

Hi,

 

I am new to Applescript, and have been trying to learn as much as I can.  Most of it makes sense, but I'm having a few issues with a particular task.  I've been asked to come up with a way to essentially log out a user after 30 minutes of inactivity.  That part's not really an issue, but when it comes to actually logging the user out, I run into a problem if the user has any open, unsaved documents opened.

 

This forces the "Do you want to save changes?" dialog box to appear, which - if left unanswered - ends up causing the log out process to timeout.  I suppose what I'm wondering is if there's any way to force close all programs/open documents/etc. with no prompt to save and log off.  Other sites and forums mentioned using Unix commands, but I'm not that familiar with them (and most "do shell script ..." and some variation of "killall" still brought up the same user prompt about saving).

 

I totally understand the reason behind prompting users about that, but I'm just trying to figure out if this is possible.  Any hints, tips, or advice really would be appreciated.

 

Thanks!

Mac Pro, Mac OS X (10.7.4)
  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Aug 2, 2012 10:07 AM (in response to permix)

    Your problem here is that it may depend on the applications in use.

     

    All applications should understand the 'quit' event, so you can tell them to quit. Most of them should also accept the optional 'saving' parameter to tell them whether or not to save open documents, but there's no guarantee of that.

     

    In addition, recent applications that support versions and resume shouldn't prompt at all - they should save their current state and just restore that state next time they're launched.

     

    So, since there are multiple cases that require different actions, your first problem is going to be categorizing applications so that you know what actions to take. Given that you likely can't predict all the applications that will be running, that's going to be hard to do.

     

    I might be over-thinking it though, so let's simplify:

     

    First, it's easy to get a list of running apps:

     

    tell application "System Events"

              set runningApps to every application process whose background only is false

    end tell

     

    From here it's easy to iterate through them, first trying the 'safe' option of not saving changes. Here I'm using a 'try' block to catch errors (e.g. the application doesn't understand the 'saving' parameter), at which point I can re-try the quit without the parameter:

     

    repeat with eachApp in runningApps

              try

                        tell application eachApp to quit saving no

              on error

                        try

                                  tell application eachApp to quit

                        end try

              end try

    end repeat

     

    So, in essence, this will iterate through the runningApps. It first tries to tell the application to quit with the 'saving no' option. If that fails it then re-tries without the 'saving' option.

     

    This should be enough to quit most apps, after which you can issue the log out command.

     

    If you have applications that support versions and restore you could put them in an exceptions list, knowing that when they're told to quit they'll save state, so you don't need to interact with them

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Aug 2, 2012 10:10 AM (in response to Camelot)

    Now that I think of that, you'd probably want to exclude certain apps (like the Finder), since telling the Finder to quit is generally a bad idea.

     

    Here's a revised script that doesn't interact with the Finder (because it shouldn't quit) or TextEdit (because it will save/restore state automatically. Extend the list as appropriate.

     

    set excludedApps to {"Finder", "TextEdit"}

     

    tell application "System Events"

              set runningApps to every application process whose background only is false

    end tell

     

    repeat with eachApp in runningApps

              if eachApp is not in excludedApps then

                        try

                                  tell application eachApp to quit saving no

                        on error

                                  try

                                            tell application eachApp to quit

                                  end try

                        end try

              end if

    end repeat

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Aug 2, 2012 12:37 PM (in response to permix)

    Oh, my bad... should have tested my script a little better.

     

    The problem is one of context - to System Events, an application is far more than just a name, but that's what I'm checking against the list, and that's why the script complains - it's comparing apples and oranges.

     

    The fix is simple, though - all we really want is the name of the running processes, so change the line:

     

              set runningApps to every application process whose background only is false


    to:

     

              set runningApps to name of every application process whose background only is false


    Now it'll just get the process names, and the rest of the script should work.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.