Skip to content

Commit

Permalink
Build on PR
Browse files Browse the repository at this point in the history
  • Loading branch information
xylix committed Mar 22, 2020
1 parent 0ace937 commit 4e88cec
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions aw_watcher_window/macos.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
from typing import Optional
from typing import Dict, Optional
from AppKit import NSWorkspace
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)

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

app = NSWorkspace.sharedWorkspace().activeApplication()
print("appname: " + app_name + ", title: "+ title)
return {"appname": app_name, "title": title}

if app:
pid = app['NSApplicationProcessIdentifier']
else:
return None

def getTitle(pid: int) -> str:
options = kCGWindowListOptionOnScreenOnly
windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)

options = kCGWindowListOptionOnScreenOnly
window_list = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
for window in window_list:
if pid == window['kCGWindowOwnerPID']:
# We could use app['NSApplicationName'], but this value is more
# accurate and matches other methods (like applescript)
app_name = window['kCGWindowOwnerName']
title = window.get('kCGWindowName', u'')
break
for window in windowList:
lookupPid = window['kCGWindowOwnerPID']
if (pid == lookupPid):
return str(window.get('kCGWindowName', 'Non-detected window title'))
return ""

return {"appname": app_name, "title": title}

0 comments on commit 4e88cec

Please sign in to comment.