Kill program Mac OSX terminal can sometimes be necessary

Even though OS X is a pleasure to work with, we have all had a program or process freeze up. It won’t quit by using “Force quit”. What do you do now? Fortunately, this can be solved quite easily. To kill program Mac OS X terminal on Leopard / Snow leopard / Lion do the following commands:

Quitting apps gracefully is typically done through the appropriate application menu by choosing “Quit”, but the Mac GUI is obviously inaccessible from the command line of Mac OS X. So when most command line users are confronted with the need to exit an application, they wind up using the ‘kill’ command to terminate the process. In order to see if the apps are running, lookout for a small dot that’s located under the icons. It can sometimes be hard to see, however you should see the small dot under the icons glowing, which.

Get the ID of the program

Kill the program

This will give you the number of the processes found (if any). Now just close them with this command

So, for example to kill my activity monitor program

Now the offending program will shut down immediately, no matter what it was doing. This is, however, a last resort. You will lose any unsaved changes.

What happens behind the scenes when you kill the program is a bit different from regular operations. Normally, the operating system asks the program nicely to go away. With the above kill command, it simply shuts it down immediately without asking or telling it anything. There you go. Gone.

Has this post got you interested? Would you like to learn how to do more interesting stuff in OS X’s terminal , than just kill a program. Then read these 10 commands. Even more interested? The OS X terminal is built on top of bash, and you can read more about it in this tutorial.

class App

Completely revised - makes sense to read

Using class or instance methods¶

Generally you have the choice between using the class methods (e.g.App.open('application-identifier')) or first create an App instance and usethe instance methods afterwards (e.g. myApp=App('application-identifier')and then later on myApp.open()). There is no recomendation for a preferred usage, butif you plan to act on the same app or window more often,it might be more efficient, to use the instance approach.

For the string application-identifier principally the same rules apply, as if you would use the identifieron a command line. Especially if path specification contains blanks,it must be enclosed in double-quotes.

If the application name is given without a path specification, then it must be found on the system,according to the rules of that system (Windows: is on system path, Mac: is in an /Applications folder, …).

Examples:

How to create an App instance

The basic choice is to just say someApp=App('some-app-identifier') and youhave your app instance, that you can later use together with its methods,without having to specify the string again.

Generally all class methods return an app instance,that you might save in a variable to use it later in your script.

At time of instance creation the process list is scanned for the name of theexecutable using the given text. If this is found, the app instance is initialized with the respectiveinformation (PID, executable, window title of main/frontmost window).So you could directly ask the app instance, wether it is running (isRunning()),has a main window (hasWindow()), get the title of that main window (getWindow())and get the process id (PID) (getPID).

The string representation of an app instance looks like this:
[nPID:executableName(main/frontmostwindowtitle)]giventext

BE AWARE The application-identifier can no longer be (part of) a window title - it must specify an executable.If you want to reference a running application with (part of) a window title, you have to use App.focus().

NOTE Currently the information, wether a window is hidden or minimized, is not availableand it is not possible yet, to bring such a window to front with a compound SikuliX feature.

Open, close an application or focus on it

class App
classmethod App(application)

Usage:someApp=App(application)

Create an App instance, to later use with the instance methods (see above)

The string application must allow the system to locate the application in the system specific mannor.If this is not possible you might try the full path to an application’s executable.

Script

Optionally you might add parameters, that will be given to the application at time of open.

There are 3 options:
  • put the application string in apostrophes and the rest following the second apostroph will be taken as parameter string
  • put `` – `` (space 2 hyphens! space) between the applications name or path (no apostrophes!) and the parameter string.
  • use setUsing() with an already existing application object (created before)
Parameters:application – The name of an application (case-insensitive) or the path to the executable and optionally parameters
Returns:an App object, that can be used with the instance methods
Mac
classmethod open(application)

Usage:App.open(application)

Open the specified application, if it is not yet opened and bring it to front

Parameters:application – the same as for App(application
Returns:an App object, that can be used with the instance methods
Close

This method is functionally equivalent to openApp().

classmethod focus(title[, index])

Usage:App.focus(title[,index])

Switch the input focus to a running application having a front-most window with a matching title.

Parameters:
  • title – The name of an application (case-insensitive) or (part of) a window title (case-sensitive).
  • index – optional number (counting from 0) telling which one you want to have in the row of possible matches.
Returns:

an App object, that can be used with the instance methods

If no index is given, it is taken as 0. If the index is not applicable (not enough matches)the returned App object is invalid and not useable.

So you might loop through possible matches by counting the index up from 0 until you get an invalid

If you specify the exact window title of an open window, you will get exactlythis one. But if you specify some text, that is found in more than one openwindow title, you will get the first in the row of all these windows.So if you want exactly one specific window, you eitherneed to know the exact window title or at least some part of the title text,that makes this window unique in the current context (e.g. save a document witha specific name, before accessing it’s window).

macOS and Linux not yet possible, to identify a running app by part of the title of it’s frontmost window.If you want to use a window title to match the application, use App.focus() before,to get a valid App object.

This method is functionally equivalent to switchApp().

classmethod close(application)

Usage:App.close(application)

Mac Script Check If App Running And Close It

It closes the running application matching the given string. It does nothingif no running application matches. If you want to use a window title to match the application, use App.focus() before,to get a valid App object.

Parameters:application – The name of an application (case-insensitive)

This method is functionally equivalent to closeApp().

setUsing(parametertext)

Usage:appName=someApp.setUsing('parm1xparm2yparm3z')where App instance someApp was created before.

Parameters:parametertext – a string, that is given to the application at startup (when using open() ) as if you would start the app from a commandline.
setWorkDir([workingFolder])
WINDOWS ONLY

Usage:appName=someApp.setWorkDir('pathtotheworkingdirectory')where App instance someApp was created before.

param workingFolder:
the absolute path, which gets the working directory. The location of the app exec-file is used if omitted.
open([waitTime])

Usage:someApp.open()where App instance someApp was created before.

Open this application.

Parameters:waitTime – optional: seconds as integer, that should be waited for the app to get running
Returns:the app instance, which will be invalid, if open failed.
focus()

Usage:someApp.focus() where App instance someApp was created before.

Switch the input focus to this application.

Returns:the app instance, which will be invalid, if open failed.
close([waitTime])

Usage:someApp.close() where App instance someApp was created before.

Close this application.

Parameters:waitTime – optional: seconds as integer, that should be waited for the app to no longer being running
Returns:the app instance, which should be invalid (not running) afterwards.
closeByKey([waitTime])

Usage:someApp.closeByKey() where App instance someApp was created before.

Close this application by bringing it to front first (focus()) an then issue the systemspecifickeybord shortcut to close an application. This might help in situations where the normal close() leads tooddities at a later restart of the application.

Parameters:waitTime – optional: seconds as integer, that should be waited for the app to no longer being running

Getting information about a running application

isValid()

Usage:ifnotsomeApp.isValid():someApp.open()where App instance someApp was created before.

Returns:True if the app has a process ID, False otherwise

Be aware This simply checks wether the app object has a valid process ID. Hence it returns instantly. But thereis no guarantee, that the app is still running. If you want to be sure, you have to use isRunning(0), whichevaluates the state of the app, but might take some 100 millisecs, depending on your system’s state.

isRunning([waitTime])

Usage:ifnotsomeApp.isRunning():someApp.open() where App instance someApp was created before.

Parameters:waitTime – optional: seconds as integer, that should be waited for the app to get running
Returns:True if the app is running (has a process ID), False otherwise

Be aware Until the wait time is elapsed, the state of the application is checked every second.If you use just isRunning(), the state check is done twice, waiting 1 second in between.Hence this might take up to 2 seconds.If you want, that only one check is done, use isRunning(0).

hasWindow()

Usage:ifnotsomeApp.hasWindow():openNewWindow()#someprivatefunctionwhere App instance someApp was created before.

Returns:True if the app is running and has a main window registered, False otherwise
getTitle()

Usage:title=someApp.getTitle()where App instance someApp was created before.

Returns:the title of the frontmost window of this application, might be an empty string
getPID()

Usage:pid=someApp.getPID()where App instance someApp was created before.

Returns:the process ID as number if app is running, -1 otherwise
getName()

Usage:appName=someApp.getName()where App instance someApp was created before.

Returns:the short name of the app as it is shown in the process list
classmethod pause(waitTime)

Usage:App.pause(someTime) (convenience function)

Just do nothing for the given amount of time in seconds (integer or float).

Dealing with Application windows¶

classmethod focusedWindow()

Usage:App.focusedWindow()

Identify the currently focused or the frontmost window and switch to it.Sikuli does not tell you, to which application this window belongs.

Returns:a Region object representing the window or None ifthere is no such window.

On Mac, when starting a script, Sikuli hides its window and startsprocessing the script. In this moment, no window has focus. Thus, it isnecessary to first click somewhere or use App.focus() to focus on awindow. In this case, this method may return None.

On Windows, this method always returns a region. When there is no windowopened on the desktop, the region may refer to a special window such as thetask bar or an icon in the system tray.

Example:

window([n])

Usage 1:App(application).window([n]) an App instance is created on the fly.

Usage 2:someApp.window([n]) where App instance someApp was created before.

Mac Script Check If App Running And Close It Is Hacked

Get the region corresponding to the n-th window of this application (Mac) ora series of windows with the matching title (Windows/Linux).

Parameters:n – 0 or a positive integer number. If ommitted, 0 is taken asdefault.
Returns:the region on the screen occupied by the window, if such windowexists and None if otherwise.

Below is an example that tries to open a Firefox browser window and switchesto the address field (Windows):

Afterwards, it focuses on the Firefox application, uses the window() method toobtain the region of the frontmost window, applies some operationswithin the region, and finally closes the window:

Below is another example that highlights all the windows of anapplication by looping through them (Mac):

getTitle([n])

Usage: see App.window([n])

Returns:the title of the frontmost/nth window of this application, might be an empty string

General aspects, hints and tipps¶

  • Be aware, that especially the window handling feature is experimentaland under further development.
  • Especially on Windows be aware, that there might be many matchingwindows and windows, that might not be visible at all. Currently thewindow() function has no feature to identify a special window besidesreturning the region. So you might need some additional checks to besure you are acting on the right window.
  • Windows/Linux: The close() function currently kills the application,without closing it’s windows before. This is an abnormal termination andmight be recognized by your application at the next start (e.g. Firefoxusually tries to reload the pages).
  • Even if the windows are hidden/minimized, their region that they have inthe visible state is returned. Currently there is no Sikuli feature, todecide wether the given window(n) is visible or not or if it iscurrently the frontmost window. The only guarentee: window()/window(0)is the topmost window of an application (Mac) or a series of matchingwindows (Windows/Linux).
  • Currently there are no methods available to act on such a window(resize, bring to front, get the window title, …).

Some tips:

  • Check the position of a window’s returned region: some apps hide therewindows by giving them “outside” coordinates (e.g. negative)
  • Check the size of a window’s returned region: normally your app windowswill occupy major parts of the screen, so a window’s returned region ofe.g. 150x30 might be some invisible stuff or an overlay on the real appwindow (e.g. the “search in history” input field on the Safari Top-Sitespage, which is reported as windows(0))
  • If you have more than one application window, try to position them atdifferent coordinates, so you can decide which one you act on in themoment.
  • It is sometimes possible to use the OCR text extraction featureRegion.text() to obtain the window title.

Some technical information on the implementation¶

The following information on how the features are implemented might help to understand problematic situations orto make suggestions on enhancements.

Windows

Mac Script Check If App Running And Close It Is Working

  • Applications are opened by internally running an appropriate startcommand.
  • The state of an application is evaluated by using the appropriate output of a tasklistcommand.
  • focus() and the windowrelated features are still implemented at the native level (WinUtil.dll). Same goesfor finding an application by part of its frontmost window’s title.

Mac

Mac Script Check If App Running And Close It Is Open

  • Applications are opened by internally running an appropriate opencommand.
  • The state of an application is evaluated by using the output of an appropriate AppleScript snippet, internally runusing macOS’s osascriptcommand.
  • focus() and the windowrelated features are still implemented at the native level (MacUtil.dll).

Mac Script Check If App Running And Close It Is Going

Linux

Mac Script Check If App Running And Close Itunes

The features are implemented by either running an appropriate shell command or by using appropriate features ofthe packages xdotool and wmctrl, which must be provided by the user.