Skip to content

Commit

Permalink
Standardize macos window lib with other OS window libs
Browse files Browse the repository at this point in the history
  • Loading branch information
xylix committed Mar 24, 2020
1 parent 4e88cec commit 38591c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
5 changes: 4 additions & 1 deletion aw_watcher_window/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def get_current_window_linux() -> Optional[dict]:

def get_current_window_macos() -> Optional[dict]:
from . import macos
return macos.getInfo()

app = macos.get_current_app()
print ("appname" + macos.get_app_name(app) + ", title" + macos.get_app_title(app))
return {"appname": macos.get_app_name(app), "title": macos.get_app_title(app)}


def get_current_window_windows() -> Optional[dict]:
Expand Down
23 changes: 10 additions & 13 deletions aw_watcher_window/macos.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
from typing import Dict, Optional
from AppKit import NSWorkspace
from AppKit import NSWorkspace, NSRunningApplication
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)

def getInfo() -> Optional[Dict[str, str]]:
app = NSWorkspace.sharedWorkspace().frontmostApplication()
if app:
app_name = app.localizedName()
title = getTitle(app.processIdentifier())
def get_current_app() -> Optional[NSRunningApplication]:
return NSWorkspace.sharedWorkspace().frontmostApplication()

print("appname: " + app_name + ", title: "+ title)
return {"appname": app_name, "title": title}

else:
return None
def get_app_name(app: NSRunningApplication) -> str:
return app.localizedName()

def getTitle(pid: int) -> str:

def get_app_title(app: NSRunningApplication) -> str:
pid = app.processIdentifier()
options = kCGWindowListOptionOnScreenOnly
windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)

for window in windowList:
lookupPid = window['kCGWindowOwnerPID']
if (pid == lookupPid):
return str(window.get('kCGWindowName', 'Non-detected window title'))
if (lookupPid == pid):
return window.get('kCGWindowName', 'Non-detected window title')
return ""

0 comments on commit 38591c5

Please sign in to comment.