On Sep 06, 2012, at 05:04, koenig.yvan < email@hidden> wrote: Alas I get an odd behavior.
______________________________________________________________________
Hey Yvan,
Hmm. After fiddling around a bit I got this to work quite reliably from FastScripts, from the Applescript Editor, and from Script Debugger.
If you run it from the Applescript Editor, Script Debugger, or Smile you need to exclude the app from the idList:
set idList to bundle identifier of processes whose visible is true and name is not "Finder" and name is not "Applescript Editor"
It appears to be desirable to keep the Finder from quitting, and timing seems to be an issue.
------------------------------------------------------------------------------------------------ try
tell application "System Events" set visible of processes to true delay 0.5 set idList to bundle identifier of processes whose visible is true and name is not "Finder" end tell
repeat with i in idList tell application id (get contents of i) delay 0.1 quit without saving end tell end repeat
on error e number n set e to "Error: " & e & return & return & "Error Number: " & n beep tell me to set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK" if button returned of dDlg = "Copy" then set the clipboard to e end try ------------------------------------------------------------------------------------------------
A little more fiddling:
------------------------------------------------------------------------------------------------ tell application "System Events" set visible of processes to true delay 0.5 set procList to name of processes whose visible is true and name is not "Finder" end tell set AppleScript's text item delimiters to "|" set procList to "\"" & (procList as text) & "\"" set pidList to do shell script "ps auxc | egrep -i " & procList & " | awk '{ print $2}' | tr '\\n' ' '" set cmd to "kill -15 " & pidList do shell script cmd ------------------------------------------------------------------------------------------------
A little more fiddling:
(This one takes Mountain Lion for pgrep.)
------------------------------------------------------------------------------------------------ tell application "System Events" set visible of processes to true delay 0.5 set procList to name of processes whose visible is true and name is not "Finder" end tell set AppleScript's text item delimiters to "|" set procList to "\"" & (procList as text) & "\""
# See what you're going to kill (comment-out): set pidList to do shell script "pgrep -ifl -d'|' " & procList & " | sed -e 's: \\|$::' | awk '{ gsub(/\\|/, \" \\n\"); print }'"
# Uncomment to kill apps: # set pidList to do shell script "pgrep -if -d'|' " & procList & " | sed -e 's: \\|$::'" # set cmd to "kill " & pidList # do shell script cmd
------------------------------------------------------------------------------------------------
The final result:
(Takes Mountain Lion for pkill.)
------------------------------------------------------------------------------------------------ tell application "System Events" set visible of processes to true delay 0.5 set procList to name of processes whose visible is true and name is not "Finder" end tell set AppleScript's text item delimiters to "|" set procList to "\"" & (procList as text) & "\"" do shell script "pkill -if " & procList ------------------------------------------------------------------------------------------------
I've tried this with my normally running applications ("Terminal|BBEdit|Mail|Script Debugger|Safari") and a few more.
So far it works like a champ.
-- Best Regards, Chris
|