Skip to content

Commit

Permalink
Fix waitForWindow() on Mac and Windows.
Browse files Browse the repository at this point in the history
Avoid exception when getWindow() or getWindowTitle() is called while window isn't open yet and waiting for window.
On Mac, return None instead of raising exception when PID window isn't found
  • Loading branch information
ulodha committed Sep 2, 2018
1 parent 24a0d8b commit c88bd7f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lackey/App.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ def getWindow(self):
Returns an empty string if no match could be found.
"""
if self.getPID() != -1:
if not self.hasWindow():
return ""
return PlatformManager.getWindowTitle(PlatformManager.getWindowByPID(self.getPID()))
else:
return ""
return None
def getName(self):
""" Returns the short name of the app as shown in the process list """
return PlatformManager.getProcessName(self.getPID())
Expand All @@ -213,7 +215,7 @@ def waitForWindow(self, seconds=5):
timeout = time.time() + seconds
while True:
window_region = self.window()
if window_region is not None or time.time() < timeout:
if window_region is not None or time.time() > timeout:
break
time.sleep(0.5)
return window_region
Expand All @@ -224,6 +226,8 @@ def window(self, windowNum=0):
"""
if self._pid == -1:
return None
if not self.hasWindow():
return None
x,y,w,h = PlatformManager.getWindowRect(PlatformManager.getWindowByPID(self._pid, windowNum))
return Region(x,y,w,h).clipRegionToScreen()

Expand Down
4 changes: 2 additions & 2 deletions lackey/PlatformManagerDarwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def getWindowByPID(self, pid, order=0):
return w["kCGWindowNumber"]
else:
order -= 1
raise OSError("Could not find window for PID {} at index {}".format(pid, order))
return None
def getWindowRect(self, hwnd):
""" Returns a rect (x,y,w,h) for the specified window's area """
for w in self._get_window_list():
Expand All @@ -328,7 +328,7 @@ def getWindowRect(self, hwnd):
width = w["kCGWindowBounds"]["Width"]
height = w["kCGWindowBounds"]["Height"]
return (x, y, width, height)
raise OSError("Unrecognized window number {}".format(hwnd))
return None
def focusWindow(self, hwnd):
""" Brings specified window to the front """
Debug.log(3, "Focusing window: " + str(hwnd))
Expand Down

0 comments on commit c88bd7f

Please sign in to comment.