You can ask MMA to run the traditional ps
,grep
,kill
,killall
"unix" shell commands (using for example the RunProcess
and Run
commands).
The following is more a "Unix" post than a Mathematica one, but ... :
For Unix, OS X sytems
To "check if some application is opened" :ps
and grep
For example to see if the Chrome browser is running :
myExternalCommand = "ps -acx -o pid -o command | grep -i chrome"
RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]
1709 Google Chrome
1713 Google Chrome Helper
1846 Google Chrome
Helper
We use here the Mathematica command RunProcess
because it is useful not only to execute an external command but also to retrieve the external output of the command.
What does the external command myExternalCommand
: 1/ the shell command ps
lists all the running processes (instances of running programs) on your computer, then the result is passed (piped) to the command grep
which here fetchs and displays only every line containing the word "chrome" (case insensitive here).
From the output you learn here that, yes, the chrome browser is running and that the exact name of the app is actually "Google Chrome", the other "... helper" processes are actually associated interface processes.
If you just want to display the main google chrome process you have to give a more precise pattern to grep like :
myExternalCommand = "ps -acx -o pid -o command | grep -i chrome$"
RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]
1709 Google Chrome
the "chrome$"
pattern means that you look for the word "chrome" at the end of a line ...
(For more info on ps
and grep
open a Console and type man ps
or man grep
, or use google.)
To quit an app : killall
or kill
1/ the killall
shell command must be followed by the plain app name (as displayed by the ps
command) ( !!! WARNING : the following command will really quit Chrome)
myExternalCommand="killall 'Google Chrome'"
RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]
or more simply just use for example Run
because we don't need to retrieve any useful output here :
Run[myExternalCommand]
2/ the kill
shell command must be followed by the app PID (Process ID) number (as displayed by the ps command in the first column, see the output above)
(* WARNING WARNING WARNING WARNING !!!!!*)
(* only run the following \
external command if you know exactly what you are doing, otherwise you could \
quit some other important processes and make damages *)
(*Before running, replace myPIDnumber with the number displayed in \
the output of the ps command above, here I should replace it by 1709 *) \
myExternalcommand = "kill myPIDnumber";
(* Then just the RunProcess or Run command above in 1/*)
killall
and kill
commands both will send the given app (here Chrome) a request to quit but this might not be taken into account by the app if it has some other higher priority tasks running (writing on disk, ...). It is recommended to use this method first but if it does not work you can force the app to quit immediately by adding to the commands the "-9" option, that means : "kill -9 ..." or "killall -9 ..."
For OS X only
To "close a particular window" :
in complement to @MikeHoneychurch great answer about closing an app, I just found that it seems also clearly possible to close only a particular window of a given app, using the same type of apple script. I did not test it but here are the links :
https://superuser.com/questions/526624/how-do-i-close-a-window-from-an-application-passing-the-file-name
https://stackoverflow.com/questions/8798641/close-terminal-window-from-within-shell-script-unix