-1

I'm looking for a way to find a specified window dimensions (width & height). So far i've been using the AutoIt functions, by just writing parts of the window/frame name, and it's working just like I want it to. Only problem is that it only works in MS Windows. I need it to work crossplatform (windows & linux).

Since the platforms are so different, It would be necesery to have two scripts, one if the system is windows, and one for Linux. And I would like to not rely on extra programs (like AutoIt). I do not wish it to be "hardcoded" in the script what frame to pick. I need/want it to work like the AutoIt, by spesefying the frames name/or parts of it.

b0bz
  • 2,140
  • 1
  • 27
  • 27
  • 1
    have a look at http://stackoverflow.com/questions/151846/get-other-running-processes-window-sizes-in-python – yurib Jan 25 '12 at 00:05
  • @SLACKY: What do you mean by program or frames dimension? Is it the window that you are interested in (as yurib suggested)? Or, is it the screen resolution that you want to figure out? – gsbabil Jan 25 '12 at 00:48

1 Answers1

1

I figured out how I could do this in windows, thanks to yurib..

import win32con
import win32gui

def inText(haystack, needle, n):
    parts= haystack.split(needle, n+1)
    if len(parts)<=n+1:
        return False
    if len(haystack)-len(parts[-1])-len(needle):
        return True

def isRealWindow(hWnd):
    '''Return True if given window is a real Windows application window.'''
    if not win32gui.IsWindowVisible(hWnd):
        return False
    if win32gui.GetParent(hWnd) != 0:
        return False
    hasNoOwner = win32gui.GetWindow(hWnd, win32con.GW_OWNER) == 0
    lExStyle = win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE)
    if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner)
      or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)):
        if win32gui.GetWindowText(hWnd):
            return True
    return False

def getWindowSizes(text):
    '''Return a list of tuples (handler, (width, height)) for each real window.'''
    def callback(hWnd, extra):
        if not isRealWindow(hWnd):
            return
        title   = win32gui.GetWindowText(hWnd)
        rect    = win32gui.GetWindowRect(hWnd)
        isFrame = inText(title, text, 0)
        if(isFrame):
            windows.append((title, rect[2] - rect[0], rect[3] - rect[1], rect[0],rect[1]))
    windows = []
    win32gui.EnumWindows(callback, windows)
    return windows

def findWindow(text):
    window = getWindowSizes(text)
    name = window[0][0]
    w = window[0][1]
    h = window[0][2]
    x = window[0][3]
    y = window[0][4]

    return name,w,h,x,y

Now i need to know how i can do something like this in Linux.. Any hints?

b0bz
  • 2,140
  • 1
  • 27
  • 27