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?
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)
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)
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}’”)
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.
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”
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.