 |
 |
*Proud* - My "killer" applescript
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2000
Location: Huddersfield, UK
Status:
Offline
|
|
Wow! I've just written my first useful applescript, which I've called Search+destroy. Here it is:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> display dialog <font color = red>"Find the name of the application to search for:"</font> default answer <font color = red>"Finder"</font> buttons {<font color = red>"Cancel"</font>, <font color = red>"OK"</font>} default button <font color = blue>2</font>
copy the result as list to {application_name, button_pressed}
tell application <font color = red>"Terminal"</font>
do script with command <font color = red>"ps -auxww | grep '"</font> & application_name & <font color = red>"' | grep -v grep | grep $LOGNAME | awk '{print $<font color = blue>2</font>}' | pbcopy"</font>
delay (<font color = blue>1</font>)
close window <font color = blue>1</font>
end tell
tell application <font color = red>"Finder"</font>
activate
set appnum to the clipboard
display dialog <font color = red>"The PID of the application '"</font> & application_name & <font color = red>"' is: "</font> & appnum buttons {<font color = red>"Kill "</font> & application_name, <font color = red>"Exit"</font>} default button <font color = blue>2</font> with icon note
copy the result as list to {button_pressed}
if the button_pressed is <font color = red>"Kill "</font> & application_name then
tell application <font color = red>"Terminal"</font>
do script with command <font color = red>"kill <font color = blue>-9</font> "</font> & appnum
delay (<font color = blue>1</font>)
close window <font color = blue>1</font>
end tell
end if
end tell
</font>[/code]
So, that's it. Copy it into script editor and compile it - see what you think.
Some of the terminal code was borrowed from Apple's restart menu server script, available from their site.
All in all, I'm very pleased with it (except for the terminal windows opening in the background.
Tell me what you think....
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2000
Location: Minneapolis, MN
Status:
Offline
|
|
I like the style of this script. I've always wondered *why* one would have to find the process ID to kill an app. Why not just by name. I would be careful to type in something like 'e' as an app name. Or any other generic type thing. Like 'Microsoft', hey now that's not a bad idea!?!
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Nov 1999
Status:
Offline
|
|
Careful with this one. I can see two problems:[list=1][*]What happens if you don't have permissions for the app you want to kill? If you can't kill the app, there should be some kind of notice that it couldn't be killed.[*]What about multiple instances of the same app (which will have the same name)? How do you pick which one you want to kill?[/list=a]
|
|
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2000
Location: Minneapolis, MN
Status:
Offline
|
|
I have to agree with Millenium, this script has the potential to do more harm than good if there aren't additional checks and safeguards put in.
Really, now that I think about it, what apps would you want to kill that aren't in the "Force Quit" window? If you don't really want the unix processes running in the background, don't start them up and find the ways to keep them from automatically starting (different depending on the app and its origin).
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Aug 2001
Location: CA
Status:
Offline
|
|
Originally posted by bluedog:
<STRONG>I have to agree with Millenium, this script has the potential to do more harm than good if there aren't additional checks and safeguards put in.</STRONG>
Don't rain on the guy's parade now.
Programming is fun. It is great to make a program that actually works.
Now try some of the Cocoa tutorials Richard -
http://www.cocoadevcentral.com/ - see specifically the "Wrapping UNIX Commands" tutorials.
-Ben
|
|
Dual 800 - GF3 - 1.5GB
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2000
Location: Huddersfield, UK
Status:
Offline
|
|
Thanks for the suggestions (Millennium and bluedog) and support (cheers ben!).
I might try and improve it at a later date, but I'm really busy at the moment and was just pleased to get it working.
As for multiple instances of the same app, I didn't realise that this was possible (in the GUI at least). And yes, what about permissions? I've never had a problem with permissions, personally, so I dunno.
Yes, I'm also aware of the problem of entering a very loose search string into the search dialogue box, but I'm really not familiar enough with Applescript to allow the user to choose which application to kill yet.
...but thanks again for the comments (esp. benh57!  )
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
Originally posted by RichardP:
<STRONG>I'm really not familiar enough with Applescript to allow the user to choose which application to kill yet.</STRONG>
tell application "Finder" to set allProcesses to name of every process
choose from list allProcesses OK button name "Choose" with prompt "Which app would you like to kill?"
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Personally...I wish OS X had come with killall so I could do something like:
killall httpd
instead of:
ps aux | grep httpd
*pull out pid's*
kill <pid>;kill <pid> ....
(Just as an example...)
|
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Aug 2001
Location: CA
Status:
Offline
|
|
Originally posted by Wevah:
<STRONG>Personally...I wish OS X had come with killall so I could do something like:
killall httpd
instead of:
ps aux | grep httpd
*pull out pid's*
kill <pid>;kill <pid> ....</STRONG>
Use xargs.
ps ax | grep -i "httpd" | grep -v grep | cut -c 1-6 | xargs kill
will kill all pids that match httpd.
You could put this into a small shell script called "killall". Replate the "httpd" above with $1 so the first argument will go there and make it executable.
-Ben
[ 11-21-2001: Message edited by: benh57 ]
|
|
Dual 800 - GF3 - 1.5GB
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Why, thank you....now I can avoid compiling psmisc on here...
|
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2001
Location: World capital of drugs and prostitution. Hmmm... SEXTC...
Status:
Offline
|
|
This is what I put into a little script to 'kill all':
kill $1 (ps ax|grep $2|grep -v grep|awk '{print $1}')
In the commandline I'd type 'killall -9 [appname]', with -9 translating to $1 and [appname] to $2.
As for a failsafe, I put it in this form:
#!/usr/bin/sh
if [ $1 = "" ] | [ $2 = "" ]
then
echo "Usage: killpid [kill type] [argument]..."
else
kill $1 $(ps ax | grep $2 | grep -v grep|awk '{print $1}') >/dev/console 2>&1
fi
Mind you, I'm also the only user, so safety isn't a priority. However, if the action was successful, the message 'Killed' will appear, and if you care to, you could see if that's the case and warn if it doesn't.
On the other hand, you could add:
KILLED=$(ps ax | grep $2 | grep -v grep|awk '{print $1}')
and then check whether $KILLED has a value, and if so, warn.
|
|
The one you love and the one who loves you are never the same person.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|