Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modernize the code #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# mac_list_windows_pids
# `mac_list_windows_pids`
Mac OS X: list all windows title and their owner process ids

##Usage:
## Usage:

Tested on OS X Yosemite 10.10.5, Python 2.7
Tested on macOS Monterey 12.2, Python 3.10.0

*Note* if you run into error `No module named Quartz`, then run `pip install pyobjc-framework-Quartz` to install it.
*Note* if you run into error `No module named Quartz`, then run `pip install -r requirements.txt` to install it.

$ python lswin.py

Expand Down Expand Up @@ -39,7 +39,7 @@ Tested on OS X Yosemite 10.10.5, Python 2.7
... ...


##Known problems
## Known problems

Processes created by Safari browser will be displayed as same PID as main Safari process.

Expand Down
72 changes: 51 additions & 21 deletions lswin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,59 @@

import Quartz

#wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID)
wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID)
# wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID)
wl = Quartz.CGWindowListCopyWindowInfo(
Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID
)

wl = sorted(wl, key=lambda k: k.valueForKey_('kCGWindowOwnerPID'))
wl = sorted(wl, key=lambda k: k.valueForKey_("kCGWindowOwnerPID"))

#print wl
# print wl

print 'PID'.rjust(7) + ' ' + 'WinID'.rjust(5) + ' ' + 'x,y,w,h'.ljust(21) + ' ' + '\t[Title] SubTitle'
print '-'.rjust(7,'-') + ' ' + '-'.rjust(5,'-') + ' ' + '-'.ljust(21,'-') + ' ' + '\t-------------------------------------------'
print(
"PID".rjust(7)
+ " "
+ "WinID".rjust(5)
+ " "
+ "x,y,w,h".ljust(21)
+ " "
+ "\t[Title] SubTitle"
)
print(
"-".rjust(7, "-")
+ " "
+ "-".rjust(5, "-")
+ " "
+ "-".ljust(21, "-")
+ " "
+ "\t-------------------------------------------"
)

for v in wl:
print ( \
str(v.valueForKey_('kCGWindowOwnerPID') or '?').rjust(7) + \
' ' + str(v.valueForKey_('kCGWindowNumber') or '?').rjust(5) + \
' {' + ('' if v.valueForKey_('kCGWindowBounds') is None else \
( \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('X'))) + ',' + \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Y'))) + ',' + \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Width'))) + ',' + \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Height'))) \
) \
).ljust(21) + \
'}' + \
'\t[' + ((v.valueForKey_('kCGWindowOwnerName') or '') + ']') + \
('' if v.valueForKey_('kCGWindowName') is None else (' ' + v.valueForKey_('kCGWindowName') or '')) \
).encode('utf8')
print(
str(v.valueForKey_("kCGWindowOwnerPID") or "?").rjust(7)
+ " "
+ str(v.valueForKey_("kCGWindowNumber") or "?").rjust(5)
+ " {"
+ (
""
if v.valueForKey_("kCGWindowBounds") is None
else (
str(int(v.valueForKey_("kCGWindowBounds").valueForKey_("X")))
+ ","
+ str(int(v.valueForKey_("kCGWindowBounds").valueForKey_("Y")))
+ ","
+ str(int(v.valueForKey_("kCGWindowBounds").valueForKey_("Width")))
+ ","
+ str(int(v.valueForKey_("kCGWindowBounds").valueForKey_("Height")))
)
).ljust(21)
+ "}"
+ "\t["
+ ((v.valueForKey_("kCGWindowOwnerName") or "") + "]")
+ (
""
if v.valueForKey_("kCGWindowName") is None
else (" " + v.valueForKey_("kCGWindowName") or "")
)
)
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyobjc-framework-Quartz
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# pip-compile requirements.in
#

pyobjc-core==8.1
# via pyobjc-framework-quartz
pyobjc-framework-cocoa==8.1
# via pyobjc-framework-quartz
pyobjc-framework-quartz==8.1
# via -r requirements.in