Skip navigation

applescript to quit or force quit hanging programmes.

8531 Views 13 Replies Latest reply: Feb 27, 2012 11:41 PM by ChangeAgent RSS
ChangeAgent Level 1 Level 1 (140 points)
Currently Being Moderated
Feb 27, 2012 4:11 AM

I have a Mac Mini that I use as a home server. I have the basic energy saver scheduler turned on, but sometimes a application is hanging and as such preventing a shut down. 

 

I want to schedule a force quit of every application that is hanging.  I would have this run from cron a few minutes before the shut down.  Or even have cron shut down at that point.

 

I thought I start gently by Quitting all open applications with this.

 

 

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try

          tell application "Finder"

                    set process_list to the name of every process whose visible is true

          end tell

          repeat with i from 1 to (number of items in process_list)

                    set this_process to item i of the process_list

                    if this_process is not in white_list then

                              tell application this_process

                                        quit

                              end tell

                    end if

          end repeat

 

 

How can I add a bit that would kill any other applications so that when the energy saving panel comes to shutting down, it can.

MBP, MM, MBP - 10.6 + Windooz XP on a hard partition
  • fane_j Level 4 Level 4 (3,655 points)

    Rather than this method, have you tried simply,

     

    on run

              tell application "System Events" to shut down

    end run

     

    I don't know if this is cancellable by apps. If it is, then shutdown from a do shell script should shut down the system, no ifs, ands, or buts.

  • fane_j Level 4 Level 4 (3,655 points)

    ChangeAgent wrote:

     

    Shell scripts is an option but a bit hard or?

    Not a shell script, but AppleScript's do shell script command. Like this

     

    on run

      do shell script "shutdown -h now"

    end run

     

    I can't test it, 'cos I can't shut down now. If the account in which you're running it doesn't have admin privs, you might need to do (with due caution about hard-coding your name and pass)

     

    on run

      do shell script "shutdown -h now" user name "your_user_name" password "your_password" with administrator privileges

    end run

     

    All the details about 'do shell script' are in Apple's TN 2065.

     


  • R C-R Level 6 Level 6 (13,830 points)

    I suggest trying enclosing the "quit" statements inside "try" clauses with "on error" clauses to trap the failed quits & prevent them from halting your 'gentle' quit section. You could also include statements to compile a list of the apps that won't quit & maybe try to handle them separately after the 'gentle' section is done. You also may be able to do some housekeeping like writing out files to the disk before the forced shutdown to minimize the undesirable effects. There should be shell commands to do this but you will have to get help with that from someone else.

     

    I'm not sure this will work but the 'try' construction is something you should check out for anything that might hang or halt your scripts.

  • BobHarris Level 6 Level 6 (12,505 points)

    "shutdown -h now" stops for  no one, assuming you can run it with root privs

  • R C-R Level 6 Level 6 (13,830 points)

    Which is why he says it is unhealthy.

  • fane_j Level 4 Level 4 (3,655 points)

    ChangeAgent wrote:

     

    I am quite sure it will hang on a hanging Application

    If it does, I'll eat my hat.

    I do not want to do a kill all.  unhealthy.

    You'll have to elaborate on that.

     

    The alternative is to get a list of all running processes from System Events, and tell each one to quit, in a try statement, as suggested above. Then repeat the procedure, this time force-quitting (ie, killing) those which did not quit the first time. There are all sorts of issues you'll have to deal with. In the end, nothing is as robust as shutdown -- it's the reason shutdown was created in the first place.

  • rccharles Level 5 Level 5 (5,150 points)

    Hence me looking for a way to shut down hanging applications first and gently.  And I do not want to do a kill all.  unhealthy.

    Well, what are you going to do if the app doesn't respond to quit?  If the app doesn't repond to quit, it is messed up and your going to have to force quit the app.

     

    here is the terminal command to force quit an app. Change Finder to you app name.

    killall "Finder"

     

    There are a bunch of other signals you can send to the app.  Here is the list:

    mac $ killall -l

    HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP

    TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH INFO USR1 USR2

    mac $

    I haven't tried any of them.

     

    So your applescript doesn't hang up look at the timeout statement page 210 of

    AppleScript Language Guide pdf download the pdf file

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.