Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript to quit or force quit hanging programmes.

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 itemi of the process_list

if this_process is not in white_list then

tell applicationthis_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

Posted on Feb 27, 2012 4:11 AM

Reply
13 replies

Feb 27, 2012 6:07 AM in response to fane_j

fane_j wrote:


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.

thanks, this would not work as Applications that hang will cancel it.


Shell scripts is an option but a bit hard or?

Feb 27, 2012 6:26 AM in response to ChangeAgent

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.


Feb 27, 2012 8:56 AM in response to ChangeAgent

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.

Feb 27, 2012 3:16 PM in response to ChangeAgent

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.

Feb 27, 2012 3:54 PM in response to ChangeAgent

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

applescript to quit or force quit hanging programmes.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.