Skip to content

Commit

Permalink
Added getAllApps() and getAllAppsWindows() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalmat committed Mar 5, 2022
1 parent 5b2aa0f commit 53be721
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ All these functions are available at the moment, in all three platforms (Windows
| getMousePos | activate | |
| getScreenSize | resize / resizeRel | |
| getWorkArea | resizeTo | |
| version | move / moveRel | |
| | moveTo | |
| | alwaysOnTop | |
| getAllApps | move / moveRel | |
| getAllAppsWindows | moveTo | |
| version | alwaysOnTop | |
| | alwaysOnBottom | |
| | lowerWindow | |
| | raiseWindow | |
Expand Down
Binary file modified dist/PyWinCtl-0.0.23-py3-none-any.whl
Binary file not shown.
18 changes: 18 additions & 0 deletions src/pywinctl/_pywinctl_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess
import sys
import time
import timeit
from typing import Union, List

import Xlib.X
Expand Down Expand Up @@ -140,6 +141,23 @@ def findit(hwnd):
return matches


def getAllApps():
"""Returns a list of all active apps."""
return list(getAllAppsWindows().keys())


def getAllAppsWindows():
"""Returns a list of all active apps and their open windows."""
result = {}
for win in getAllWindows():
appName = win.getAppName()
if appName in result.keys():
result[appName].append(win.title)
else:
result[appName] = [win.title]
return result


class LinuxWindow(BaseWindow):

def __init__(self, hWnd: Union[Cursor, Drawable, Pixmap, Resource, Fontable, Window, GC, Colormap, Font]):
Expand Down
48 changes: 43 additions & 5 deletions src/pywinctl/_pywinctl_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import time
import threading
import timeit
from typing import List

import win32api
Expand Down Expand Up @@ -105,6 +106,44 @@ def enum_windows(handle: int, h_list: list):
return []


def _getAllApps(tryToFilter=False):
# https://stackoverflow.com/questions/550653/cross-platform-way-to-get-pids-by-process-name-in-python
WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')
process_list = [(p.Properties_("ProcessID").Value, p.Properties_("Name").Value, p.Properties_("CommandLine").Value) for p in processes]
if tryToFilter:
# Trying to figure out how to identify user-apps (non-system apps). Commandline property seems to partially work
matches = []
for item in process_list:
if item[2]:
matches.append(item)
process_list = matches
return process_list


def getAllApps():
"""Returns a list of all active apps."""
return list(getAllAppsWindows().keys())


def getAllAppsWindows():
"""Returns a list of all active apps and their open windows."""
process_list = _getAllApps(tryToFilter=True)
result = {}
for win in getAllWindows():
pID = win32process.GetWindowThreadProcessId(win.getHandle())
for item in process_list:
appPID = item[0]
appName = item[1]
if appPID == pID[1]:
if appName in result.keys():
result[appName].append(win.title)
else:
result[appName] = [win.title]
break
return result


class Win32Window(BaseWindow):
def __init__(self, hWnd: int):
super().__init__()
Expand Down Expand Up @@ -321,12 +360,11 @@ def getAppName(self) -> int:
processes = WMI.InstancesOf('Win32_Process')
process_list = [(p.Properties_("ProcessID").Value, p.Properties_("Name").Value) for p in processes]
pID = win32process.GetWindowThreadProcessId(self._hWnd)
pID = pID[1]
name = ""
if len(pID) > 1:
pID = pID[1]
for item in process_list:
if item[0] == pID:
name = item[1]
for item in process_list:
if item[0] == pID:
name = item[1]
return name

def getParent(self) -> int:
Expand Down

0 comments on commit 53be721

Please sign in to comment.