Force Quit an App using AS

Hi there
I am having problems with an app that will not automatically quit when the OS is shutting down, or the user logs out, thus halting the process. The app needs to be manually quit first. Selecting Quit from the App’s menu works, but the App is not reacting to a “QUIT” calll from AS. For example

tell application “TextEdit”
QUIT
end tell

will quit TextEdit, but not my App… ok, so what I want to do is to send a “Force Quit” to my App, using AS. Can this be done?

Can AS simulate a keyboard input? Because if it can, then it should pperhaps be possible to do something like:

tell application App
activate
sendKey(ALT-SHIFT-OPT-ESC) ← howeever that syntax should be?
end tell

(ALT-SHIFT-OPT-ESC force quits the frontmost appp without showing the dialog…)

another way could be to use UNIX commands: Anyone have a suggestion how to do that?

Mikkel Wøldike
Copenhagen

I found this script somewhere (it might be this forum)

set app_name to “Application Name”
set the_pid to (do shell script “ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk ‘{print $1}’”)
if the_pid is not “” then do shell script ("kill -9 " & the_pid)

This script works well for me :wink:

Thanks a lot!
It works for me as well, although i have a slightly different version.
I didn’t know the awk command, and its a great tool to have !
My command have a slightly different reading thougn, but it works.

Here’s mine:

set app_name to “Application Name”
set the_pid to (do shell script ("ps -auxwww | grep " & app_name & " | grep -v grep | awk ‘{print $2}’ "))
if the_pid is not “” then do shell script ("kill -9 " & the_pid)

I am trying to use this to force quit Windows Media Player and it’s not working ” what am I doing wrong?

set app_name to “Windows Media Player”
set the_pid to (do shell script ("ps -auxwww | grep " & app_name & " | grep -v grep | awk ‘{print $2}’ "))
if the_pid is not “” then do shell script ("kill -9 " & the_pid)

Thanks!

Never mind – no spaces between WindowsMediaPlayer

I believe that the spaces in the application name are giving you trouble. If you notice above, the ‘quoted form of’ is used to escape the spaces in the application name.

set the_pid to (do shell script “ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk ‘{print $1}’”)

Andy

As apoint of interest, awk can do the pattern matching as well as grep, and your Windows Media killer can be simplified to:

do shell script “kill -9 ps -awwx | awk '/[W]indows Media/ {print $1}'

Notice that the characters before the ps and after the second single quote are backticks located at the far upper right of the keyboard.

Andy

Oh cool! That is even better! I am using Audio Hijack to record several WMP streams during the day, and sometimes certain streams throw up and give me the message “Could not connect to server”. AH cannot quit the current stream to start the next one with that message, so I have to program this force-quit AS to run in between broadcasts.

Little problem

The script only works if the actual Window Media Application is open. When WMP is already quit, the script gives an error message that it couldn’t execute and doesn’t go away

But most of the time WMP quits successfully – the script is used really only as a precaution (for those times when Windows Player doesn’t get quit by Audio Hijack because of the “Can’t connect to stream message”

So I need this script to say “Execute this command only if Windows Media is still running”

What do I need to re-write/add?

enclose it in a try block, something like this:

set app_name to "Windows Media Player"
try
    set the_pid to (do shell script ("ps -auxwww | grep " & app_name & " | grep -v grep | awk '{print $2}' "))
    if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end try

or however your script looks by now, just enclose it in the try block

You get that error because the command exited with a non zero output, meaning that nothing was matched. You can run the shell command in the background (this makes the applescript continue without waiting for the unix command to return a value) and redirect the shell output to the ‘bit bucket’

If you append this cryptic string:

/dev/null 2>&1 &

to the end of your do shell script, it will ignore any errors and exit gracefully.

do shell script “kill -9 ps -awwx | awk '/[W]indows Media/ {print $1}' > /dev/null 2>&1 &”

Cheerrs

Andy

Browser: Safari 412
Operating System: Mac OS X (10.4)

Interestingly, the AS dictionary of WMP (vers. 9.0.0) contains no quit command in the Standard Suite. (Most applications do.)

However, in spite of that, this vanilla version works here:

tell application "System Events" to if "WindowsMediaPlayer" is in name of processes then tell application "Windows Media Player" to quit

(Note also that the name of the process contains no spaces, while the name of the application does…) :confused: