-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
19 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |