-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
WIP: Get macOS window title through python bindings instead of running AppleScript #40
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aac4f88
Update MacOS title logic
ianobermiller 2fa0b3c
Removed .scpt from PyInstaller datas argument
ErikBjare 0ace937
Merge branch 'master' into macos-window-title
xylix 4e88cec
Build on PR
xylix 38591c5
Standardize macos window lib with other OS window libs
xylix 01a5f18
try using a notification observer on macos. still not working
xylix 7ffb3fc
Try a side-effect-ful import approach to initializing macOS event loop
xylix 52852ae
Try to use a higher order function to initialize macos spesific event…
xylix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,17 +1,50 @@ | ||
import subprocess | ||
from subprocess import PIPE | ||
import os | ||
from threading import Thread | ||
from typing import Dict, Optional, NoReturn | ||
from AppKit import NSObject, NSNotification, NSWorkspace, NSRunningApplication, NSWorkspaceDidActivateApplicationNotification | ||
from Quartz import ( | ||
CGWindowListCopyWindowInfo, | ||
kCGWindowListOptionOnScreenOnly, | ||
kCGNullWindowID | ||
) | ||
from PyObjCTools import AppHelper | ||
|
||
|
||
def getInfo() -> str: | ||
cmd = ["osascript", os.path.join(os.path.dirname(os.path.realpath(__file__)), "printAppTitle.scpt")] | ||
p = subprocess.run(cmd, stdout=PIPE) | ||
return str(p.stdout, "utf8").strip() | ||
class Observer(NSObject): | ||
app = NSWorkspace.sharedWorkspace().frontmostApplication() | ||
|
||
def get_front_app(self) -> NSRunningApplication: | ||
return self.app | ||
|
||
def getApp(info) -> str: | ||
return info.split('","')[0][1:] | ||
def handle_(self, noti: NSNotification) -> None: | ||
self._set_front_app() | ||
def _set_front_app(self) -> None: | ||
self.app = NSWorkspace.sharedWorkspace().frontmostApplication() | ||
|
||
observer = Observer.new() | ||
NSWorkspace.sharedWorkspace().notificationCenter().addObserver_selector_name_object_( | ||
observer, | ||
"handle:", | ||
NSWorkspaceDidActivateApplicationNotification, | ||
None) | ||
AppHelper.runConsoleEventLoop() | ||
|
||
def get_current_app() -> NSRunningApplication: | ||
return observer.get_front_app() | ||
|
||
|
||
def get_app_name(app: NSRunningApplication) -> str: | ||
return app.localizedName() | ||
|
||
|
||
def get_app_title(app: NSRunningApplication) -> str: | ||
pid = app.processIdentifier() | ||
options = kCGWindowListOptionOnScreenOnly | ||
windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) | ||
|
||
for window in windowList: | ||
lookupPid = window['kCGWindowOwnerPID'] | ||
if (lookupPid == pid): | ||
return window.get('kCGWindowName', 'Non-detected window title') | ||
|
||
return "Couldn't find title by pid" | ||
|
||
def getTitle(info) -> str: | ||
return info.split('","')[1][:-1] |
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
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to
get_current_window_fn
?