Window Object Functions

Function Synopsis

FunctionBrief Description
(tinted functions are static)
active_window() Return a handle to the currently active window.
find_window(str) Find a window based on its title. It should be noted that the search returns the "most likely" window when more than one match is found.
search_for_window(str) Perform a deep search for all windows which match the name given.
wait_exists(str[, timeout]) Wait until a window exists, and then return a handle to it.
normalize_instance() Finds the 'normalized' instance of a window.
find_outer_parent() Finds the outer parent of a window.
is_valid() Boolean check to see if a current window handle is valid.
is_active() Boolean check to see if a current window is the active window.
activate() Activate the window.
maximize([tf]) Maximize the window horizontally and vertically.
maximize_horz([tf]) Make a window full width without modifying its height.
maximize_vert([tf]) Make a window full height without changing its width.
minimize([tf]) Minimize a window to the system tray. A synonym for "iconify".
iconify([tf]) Iconify a window to the system tray. A synonym for "minimize".
full_screen([tf]) Make a window full screen.
restore() Restore a window to its "natural" state.
shade([tf]) Shade a window.
move(x, y[, desk]) Move a window to the coordinates supplied, and potentially to a different virtual desktop.
resize(x, y) Change the inside dimensions of the specified window, not including its borders.
x() Retrieve the window's current x coordinate.
y() Retrieve the window's current y coordinate.
w() Retrieve the window's current width.
h() Retrieve the window's current height.
name() Retreive the window's title/name.
desktop() Retrieve the window's current desktop number.
info() All window info in a tuple (x, y, w, h, name, desktop, id).
wait_active([timeout]) Wait until this window is the active window.

Static Function:

active_window()

Returns a handle to the active window.

Parameters:

None

Return Value:

The currently active window. Will only return None if there are no active windows.

Example(s):

import xaut
win = xaut.window.active_window()
print("The active window is: %s" %
    win.name())

Static Function:

find_window(title)

Attempts to find a window based on title from among the open windows on the computer. Note that this search is not a deep search, so hidden windows will not be found. For example, applications which reside in the system tray will typically not be found.

The search engine uses a regular expression library, so be sure the search term is compatible.

It should be noted that the search returns the "most likely" window when more than one match is found. The criteria is based on two factors:

  • The window's desktop. The absolute difference between the current desktop and each of the window's desktops is calculated. If the values are different, then the lesser of the two is chosen.
  • The window's identity. The window with the lower id value is chosen, since (presumably) the lesser of the two is the older of the two.

Parameters:

ParamReqDescription
str yes The search term.

Return Value:

A single window handle to the most likely window (if it exists), or None if nothing matches at all.

Example(s):

#Get a handle to the konsole application
import xaut
win = xaut.window.find_window( "Shell - Konsole")
print(win.info())

Static Function:

search_for_window(title)

Performs a deep recursive search for windows by name. The most likely use of this function would be to find one of the windows rejected by the sorting criteria used by find_window. This function is several orders of magnitude slower than find - so it should only be called when absolutely necessary.

It is possible to use this function to find windows that are hidden or invisible. Beware: Windows which are hidden do not necessarily behave like "normal" windows. It's a good idea to make sure that what you are trying to do will work properly on a test system before modifying a hidden window. You have been warned.

The search engine uses a regular expression library, so be sure the search term is compatible.

It should be noted that the search pre sorts the array to where the "most likely" window has a lower index when more than one match is found. The criteria is based on two factors:

  • The window's desktop. The absolute difference between the current desktop and each of the window's desktops is calculated. If the values are different, then the lesser of the two is chosen.
  • The window's identity. The window with the lower id value is chosen, since (presumably) the lesser of the two is the older of the two.

Parameters:

ParamReqDescription
name yes The search term

Return Value:

An array of windows which match the search name. The array is sorted from most likely to least likely based on the criteria described above. If there are no matches at all, then you will get an empty array.

Example(s):

#Get a handle to all konsole application instances
import xaut
all = xaut.window.search_for_window( "Shell - Konsole")
for win in all:
    print(win.info())

Static Function:

wait_exists(title, [timeout])

Pauses script execution until a window exists and then returns a handle to that window. Note that this function uses find_window internally, which means that it should not have any side effects if used to find a window that is already open. The intention is for this function to be used to wait for a window to open after an action has been taken. For example, you could type the Alt-space key combination, type "firefox", and then wait for firefox to open.

It is an internal implementation detail that the script pauses for approximately .05 seconds per loop. If you supply a value for timeout, then the elapsed time will add at each loop until it is equal or greater than timeout value and None will be returned if the window never opens. It should be noted that testing shows that the loop almost always executes one extra time, so don't count on the timer being highly accurate.

Parameters:

ParamReqDescription
title yes The search term.
timeout no A timout period. Whole numbers are interpreted as seconds.

Return Value:

A handle to the window, or None if the window never opens and a timeout value is supplied.

Example(s):

#Open Konsole, and do a directory listing once it appears
import xaut
import time
kbd = xaut.keyboard()
kbd.type("! ") #That's alt + space
time.sleep(.5)
kbd.type("konsole{Return}")
konsole = xaut.window.wait_exists("Konsole")
time.sleep(1) #So that window has time to initialize
kbd.type("ls{Return}")
#Attempt to open a window which does not exist
import xaut
import time
kbd = xaut.keyboard()
kbd.type("! ") #That's alt + space
time.sleep(.5)
kbd.type("xyz123abc{Return}")
problem = xaut.window.wait_exists("xyz123abc", 2)
if(problem):
    print("Do you really have a program called 'xyz123abc'?")
else:
    print("Could not open 'xyz123abc'")

Function:

normalize_instance()

With recent developments in Window managers, windows have started to exhibit the behavior that asking for the current window produces a handle that does not properly report all its attributes. For example, if you directly ask X11 for the current window, and then ask that instance for it's title, you'll get NULL as your return value.

This function takes whatever instance you pass, walks all the way up to the outer parent, then starts walking back down the children until the instance with pertinent information is found.

This function is called internally from 'active_window()' and 'find_window(str)'. It is NOT called from 'search_for_window(str)' since doing so might throw off the results.

Parameters:

None

Return Value:

The 'normalized' instance of a given window - where 'normalized' means that it should properly report its title, desktop, and etcetera. This instance may or may not be the same window handle as the original window.

Example(s):

import xaut
active = xaut.window.active_window() # Already 'normalized' internally
parent = active.find_outer_parent()
normal = parent.normalize_instance()
print("Active id: %d" % active.id)
print("Parent id: %d" % parent.id)
print("Normal id: %d" % normal.id)

Function:

find_outer_parent()

Some programs have the quirk that the Window handle returned from find_window() is not the same as the Window handle returned from active_window(). Eclipse exhibits this behavior - or at least it did when I was writing this. You can use this function to get the outer window handles of any windows that behave in this fashion if it helps. This function is used internally by wait_active for example.

Parameters:

None

Return Value:

The outer parent of a given window, which may or may not be the same window handle as the original window.

Example(s):

#I'm using Eclipse in this example, since I know it works
import xaut
eclipse = xaut.window.find_window(".*Eclipse")
if(eclipse):
    eclipse.activate()
    eclipse.wait_active()
    active = xaut.window.active_window()
    print("Active id:  %d" % active.id)
    print("Eclipse id: %d" % eclipse.id)

Function:

is_valid()

Returns a non zero value if the window handle object points to a valid window.

It should be noted that although this function can help reduce the risk of race conditions, it is not fool proof. This function is quite fast, but even so it's possible the window could be closed between the check with X11 and value being returned to your script.

Parameters:

None

Return Value:

Non zero if the window is valid, zero otherwise. Note that this can be used as the condition for a loop waiting for a window to close.

Example(s):

#Get the name of a window if it's valid
#Try this example twice:
# The first time, just let it run.
# The second, close the window during the pause
import xaut
import time
kbd = xaut.keyboard()
kbd.type("!{Tab}")
time.sleep(.5)
win = xaut.window.active_window()
time.sleep(5)
#If window is still valid, print its name
if(win.is_valid()):
    print("Window %s is still valid" %
        win.name())
else:
    print("Window closed while waiting")
#Close a multi-tabbed Konsole window
import xaut
import time
win = xaut.window.find_window("Konsole")
kbd = xaut.keyboard()
if(win):
    win.activate()
    win.wait_active()
    while(win.is_valid()):
        kbd.type("exit{Return}")
        time.sleep(.2)

Function:

is_active()

Returns a boolean indication of whether or not the window is active.

Parameters:

None

Return Value:

True if the window is active, false otherwise.

Example(s):

#Make sure two different windows are open for this
import xaut
import time
kbd = xaut.keyboard()
win = xaut.window.active_window()
print(win.is_active())
kbd.type("!{Tab}")
time.sleep(1)
print(win.is_active())

Function:

activate()

Activates the window pointed to by the window object handle. Note that if the window in question is not on the current desktop, then the display is switched to that desktop (which is to say that the window is not moved).

Parameters:

None.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully activated the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Be sure to use window names which will work
import xaut
import time
def get_window(prompt):
    title = raw_input(prompt)
    win = None
    if(title):
        win = xaut.window.find_window(title)
        if(win == None):
            print("Unable to find %s" %
                title)
    else:
        print("Invalid input")
    return win

win1 = None
while(win1 == None):
    win1 = get_window("Input a search term for the first window: ")

win2 = None
while(win2 == None):
    win2 = get_window("Input a search term for the second window: ")

win1.activate()
time.sleep(3)
win2.activate()
#Switch to a window on another desktop
import xaut
import time
disp = xaut.display()
if(disp.desktop_count() < 2):
    print("Unable to demonstrate, only one desktop")
    exit

win_desk = 1
tst_desk = 2
if(disp.desktop() == 1):
    win_desk = 2
    tst_desk = 1

disp.desktop(win_desk)
win = xaut.window.active_window()
if(win == None):
    print("Unable to demonstrate, no open windows")
    exit

disp.desktop(tst_desk)
print("About to activate %s" %
    win.name())
time.sleep(3)
win.activate()

Function:

maximize([tf])

Maximizes the window both horizontally and vertically if the optional flag supplied is non-zero. Restores from maximization both horizontally and vertically if the optional flag supplied is 0.

Parameters:

ParamReqDescription
tf no Maximizes if the value is non-zero (the default), otherwise removes maximization.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully maximized the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Maximize and then restore the window
import xaut
import time
win = xaut.window.active_window()
win.restore() #In case it's already maximized
win.maximize() #Maximizes (note the lack of a flag)
time.sleep(5)
win.maximize(0) #Restores from maximize
time.sleep(5)
win.maximize(1) #Maximizes (note the flag this time)
time.sleep(5)'win.maximize(0) #Final restoration

Function:

maximize_horz([tf])

Maximizes the window only in the horizontal direction if the optional flag supplied is non-zero. Restores from maximization only in the horizontal direction if the optional flag supplied is 0.

Parameters:

ParamReqDescription
tf no Maximizes if the value is non-zero (the default), otherwise removes maximization.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully maximized the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Maximize and then restore the window
import xaut
import time
win = xaut.window.active_window()
win.restore() #In case its already maximized
win.maximize_horz() #Maximizes (note the lack of a flag)
time.sleep(5)
win.maximize_horz(0) #Restores from maximize
time.sleep(5)
win.maximize_horz(1) #Maximizes (note the flag this time)
time.sleep(5)
win.maximize_horz(0) #Final restoration

Function:

maximize_vert([tf])

Maximizes the window only in the vertical direction if the optional flag supplied is non-zero. Restores from maximization only in the vertical direction if the optional flag supplied is 0.

Parameters:

ParamReqDescription
tf no Maximizes if the value is non-zero (the default), otherwise removes maximization.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully maximized the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Maximize and then restore the window
import xaut
import time
win = xaut.window.active_window()
win.restore() #In case its already maximized
win.maximize_vert() #Maximizes (note the lack of a flag)
time.sleep(5)
win.maximize_vert(0) #Restores from maximize
time.sleep(5)
win.maximize_vert(1) #Maximizes (note the flag this time)
time.sleep(5)
win.maximize_vert(0) #Final restoration

Function:

minimize([tf])

Minimizes (iconifies) the window if the optional value is non-zero, (the default) otherwise restores the window. Note that "minimize" and "iconify" do exactly the same thing internally. Use whichever is more comfortable. You can even mix and match calls.

Parameters:

ParamReqDescription
tf no Minimizes the window if the value is non-zero (the default). Restores it otherwise.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully minimized the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Minimize then restore a window
import xaut
import time
win = xaut.window.active_window()
win.minimize() #Minimizes (note the lack of a flag)
time.sleep(5)
win.minimize(0) #Restores the window
time.sleep(5)
win.minimize(1) #Minimizes (note the flag this time)
time.sleep(5)
win.minimize(0) #Final restoration

Function:

iconify([tf])

Iconifies (minimizes) the window if the optional value is non-zero, (the default) otherwise restores the window. Note that "minimize" and "iconify" do exactly the same thing internally. Use whichever is more comfortable. You can even mix and match calls.

Parameters:

ParamReqDescription
tf no Iconifies the window if the value is non-zero (the default). Restores it otherwise.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully iconified the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Iconify then restore a window
import xaut
import time
win = xaut.window.active_window()
win.iconify() #Iconifies (note the lack of a flag)
time.sleep(5)
win.iconify(0) #Restores the window
time.sleep(5)
win.iconify(1) #Iconifies (note the flag this time)
time.sleep(5)
win.iconify(0) #Final restoration

Function:

full_screen([tf])

Makes the window "full screen", which typically means "maximize" with no title or status bars. Different windows react differently so test your target window.

Parameters:

ParamReqDescription
tf no Sets the window to "full screen" if the value is non-zero (the default). Restores it otherwise.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully made the window full screen. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Full screen and then restore a window
import xaut
import time
win = xaut.window.active_window()
win.restore() #In case its already full screen
win.full_screen() #Makes the window full screen (note the lack of a flag)
time.sleep(5)
win.full_screen(0) #Restores the window
time.sleep(5)
win.full_screen(1) #Makes the window full screen (note the flag this time)
time.sleep(5)
win.full_screen(0) #Final restoration

Function:

restore()

Restores a window to its natural state. That is to say that it will no longer be minimized/iconified, maximized, full screen, or etcetera. This is a single-step that is equivalent to calling all other state changing functions and supplying them all with a false value. It will even undo multiple flags at once. For example, a window that is first maximized and then iconified will be un-iconified, and then restored to its natural size.

Parameters:

None

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully restored the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

import xaut
import time
win = xaut.window.active_window()
win.restore() #Make sure it starts out normal
win.maximize()
time.sleep(5)
win.restore()
time.sleep(5)

win.maximize_horz()
time.sleep(5)
win.restore()
time.sleep(5)

win.maximize_vert()
time.sleep(5)
win.restore()
time.sleep(5)

win.minimize()
time.sleep(5)
win.restore()
time.sleep(5)

win.iconify()
time.sleep(5)
win.restore()
time.sleep(5)

win.full_screen()
time.sleep(5)
win.restore()
time.sleep(5)

win.shade()
time.sleep(5)
win.restore()
time.sleep(5)

Function:

shade([tf])

For those that don't know, "shading" is where the window rolls up into the bar so the title bar is all that is visible. The title bar is not moved, nor is the window resized.

Parameters:

ParamReqDescription
tf no Shades the window if the value is non-zero (the default). Restores it otherwise.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully shaded the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Shades and then restores a window
import xaut
import time
win = xaut.window.active_window()
win.restore() #In case its already shaded
win.shade() #Shades (note the lack of a flag)
time.sleep(5)
win.shade(0) #Restores the window
time.sleep(5)
win.shade(1) #Shades the window (note the flag this time)
time.sleep(5)
win.shade(0) #Final restoration

Function:

move(x, y[, desk])

Moves the window to the target coordinates, and optionally to the supplied desktop. Note that while the "x" and "y" parameters are required, you can supply a value less than zero to indicate that you do not want that particular value to change. For example, win.move(-1, 10) means "leave the window's x coordinate as it is, but move it to 10 pixels down from the top".

The fact that any value less than zero indicates "do not change" means that it is impossible to move a window off the screen. Which means that it's possible to manually move a window to a coordinate that is impossible with xaut. At least without cobbling together some sort of mouse click and drag code using the mouse object.

Another point of interest is the value for desktop. All values are "1" indexed, rather than "0" indexed. The value "0" is special, and places the window on all desktops. Any value less than zero means "leave the window where it is", and is exactly the same as not supplying a value at all.

Parameters:

ParamReqDescription
x yes The target x coordinate.
y yes The target y coordinate.
desk no The desktop to send the window to. Note that "0" puts the window on all desktops.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully moved the window. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Move a window 10 pixels to the right, and then back again
import xaut
import time
win = xaut.window.active_window()
currX = win.x()
win.move(currX + 10, -1)
time.sleep(5)
win.move(currX, -1)
#Move a window down 10 pixels, and then back again
import xaut
import time
win = xaut.window.active_window()
currY = win.y()
win.move(-1, currY + 10)
time.sleep(5)
win.move(-1, currY)
#Move a window down and right simultaneously, and then back again
import xaut
import time
win = xaut.window.active_window()
currX = win.x()
currY = win.y()
win.move(currX + 10, currY + 10)
time.sleep(5)
win.move(currX, currY)
#Move a window from desktop 1 to desktop 2 and back
import xaut
import time
disp = xaut.display()
disp.desktop(1)
win = xaut.active_window()
win.move(-1, -1, 2)
disp.desktop(2)
time.sleep(5)
win.move(-1, -1, 1)
disp.desktop(1)
#Put a window on all desktops, and then put it back
import xaut
import time

time.sleep(5)
disp = xaut.display()
count = disp.desktop_count()
desk = disp.desktop()
win = xaut.window.active_window()
win.move(-1, -1, 0)
for i in range(1, count + 1):
    disp.desktop(i)
    time.sleep(2)

win.move(-1, -1, desk)
time.sleep(2)
win.activate()

Function:

resize(w, h)

Change the dimensions of the specified window. Note that a window may refuse to resize, or it may refuse to resize to the desired size. Also, although both w and h parameters are required, you can supply zero as either parameter to indicate that you do not want that particular value to change. For example, win.resize(0, 450) means leave the window's width as-is and make the window height 450 pixels.

Parameters:

ParamReqDescription
w yes The desired width of the window.
h yes The desired height of the window.

Return Value:

Non-zero value if call succeeds, zero otherwise.

Be cautious, "succeeds" in this context doesn't necessarily mean that your application successfully altered the winodw size. It means that X11 did not report any errors or throw any exceptions.

Example(s):

#Resize a window to 640 X 480
import xaut
win = xaut.window.active_window()
print("%s is currently %d X %d pixels" %
    (win.name(), win.w(), win.h()))
win.resize(640, 480)
print("%s is now %d X %d pixels" %
    (win.name(), win.w(), win.h()))
#Change only width
import xaut
win = xaut.window.active_window()
print("%s is currently %d X %d pixels" %
    (win.name(), win.w(), win.h()))
win.resize(640, 0)
print("%s is now %d X %d pixels" %
    (win.name(), win.w(), win.h()))
#Change only height
import xaut
win = xaut.window.active_window()
print("%s is currently %d X %d pixels" %
    (win.name(), win.w(), win.h()))
win.resize(0, 480)
print("%s is now %d X %d pixels" %
    (win.name(), win.w(), win.h()))

Function:

x()

Retrieve the window's current x coordinate of its upper left corner.

Parameters:

None

Return Value:

The x coordinate of the window.

Example(s):

#Get the x coordinate of the currently active window
import xaut
win = xaut.window.active_window()
print("Upper left corner of %s is %d pixels over" %
    (win.name(), win.x()))

Function:

y()

Retrieve the window's current y coordinate of its upper left corner.

Parameters:

None

Return Value:

TODO

Example(s):

#Get the y coordinate of the currently active window
import xaut
win = xaut.window.active_window()
print("Upper left corner of %s is %d pixels down" %
    (win.name(), win.y()))

Function:

w()

Retrieve the window's current width.

Parameters:

None

Return Value:

The window's current width in pixels.

Example(s):

#Get the width of the currently active window
import xaut
win = xaut.window.active_window()
print("Window %s is %d pixels wide" %
    (win.name(), win.w()))

Function:

h()

Retrieve the window's current height.

Parameters:

None

Return Value:

The window's current height.

Example(s):

#Get the height of the currently active window
import xaut
win = xaut.window.active_window()
print("Window %s is %d pixels tall" %
    (win.name(), win.h()))

Function:

name()

Retreive the window's title/name. It should be noted that most windowed applications consist of multiple "windows" glued together to form a cohesive whole. It's possible that the name returned by this function will not match what you see in the title bar. Experiment with your windows to see what they return.

Parameters:

None

Return Value:

The window's title/name.

Example(s):

#Get the title of the currently active window
import xaut
win = xaut.window.active_window()
print("The active window is called %s" %
    (win.name()))

Function:

desktop()

Retrieve the window's current desktop number.

Parameters:

None

Return Value:

The window's current desktop number.

Example(s):

#Get the window's current desktop number
import xaut
win = xaut.window.active_window()
print("Window %s is on desktop %d" %
    (win.name(), win.desktop()))

Function:

info()

All window info in a tuple (x, y, w, h, name, desktop, id). This probably has more use as a debugging tool than anything else, seeing as how each item is available individually. I typically use this to look at all window information while I'm testing stuff.

Parameters:

None

Return Value:

A tuple of window information in the order, x, y, w, h, name, desktop, window id.

Example(s):

#Get all info about the current window
import xaut
win = xaut.window.active_window()
print(win.info())

Function:

wait_active([timeout])

Wait until this window is the active window.

It is an internal implementation detail that the script pauses for approximately .05 seconds per loop. If you supply a value for timeout, then the elapsed time will add at each loop until it is equal or greater than timeout value and False will be returned if the window does not become active during that time period. It should be noted that testing shows that the loop almost always executes one extra time, so don't count on the timer being highly accurate.

Parameters:

ParamReqDescription
timeout no A timout period. Whole numbers are interpreted as seconds.

Return Value:

True if the window activates.

Example(s):

#Be sure to have at least two windows open for this test
import xaut
import time
kbd = xaut.keyboard()
win = xaut.window.active_window()
win.iconify()
time.sleep(2)
win.activate()
win.wait_active()
#Attempt to wait for an invalid window to demonstrate the timeout
import xaut
win = xaut.window(5) #Almost certainly invalid
found = win.wait_active(5) #Wait five seconds for the invalid window
if(found):
    print("The window was found?!?")
else:
    print("Couldn't open window")