7
$\begingroup$

Is there any analogue to SystemOpen for closing applications from Mathematica? Documentation says: SystemOpen["file"] is essentially equivalent to double-clicking the file icon in your operating system.

I would like to be able to check if some application is opened and how many windows it has opened, and then to close a window or to quit the whole application.

Basically, I would like to call executable file from Mathematica, have it do some job, clean up afterwards, and come back to Mathematica.

Is this possible to do, at least on MacOSX?

$\endgroup$
1

3 Answers 3

6
$\begingroup$

You can ask MMA to run the traditional ps,grep,kill,killall "unix" shell commands (using for example the RunProcess and Runcommands).

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 psand grep open a Console and type man ps or man grep, or use google.)

To quit an app : killallor 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

$\endgroup$
8
$\begingroup$

There are many ways to do this on a Mac. For example to open Safari:

<< "!open -a Safari"

To open Safari to a particular file or page:

<< "!open -a Safari http://mathematica.stackexchange.com/questions/69247/how-to-close-external-app-from-mathematica"

Now to close Safari:

<< "!osascript -e 'tell application \"Safari\" to quit'"
$\endgroup$
8
$\begingroup$

On Windows this works:

Run["taskkill /im chrome.exe /f"]
Run["taskkill /im WolframPlayerPro.exe /f"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.