Skip to content

Instantly share code, notes, and snippets.

@gebv
Forked from ljos/frontmost_window.py
Created October 20, 2015 15:44

Revisions

  1. @ljos ljos revised this gist Oct 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion frontmost_window.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
    # Copyright @ Bjarte Johansen 2012
    # License: http://ljos.mit-license.org/

    from AppKit import NSApplication, NSApp, NSWorkspace
    from Foundation import NSObject, NSLog
  2. @ljos ljos created this gist Jul 3, 2012.
    32 changes: 32 additions & 0 deletions frontmost_window.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
    # Copyright @ Bjarte Johansen 2012

    from AppKit import NSApplication, NSApp, NSWorkspace
    from Foundation import NSObject, NSLog
    from PyObjCTools import AppHelper
    from Quartz import kCGWindowListOptionOnScreenOnly, kCGNullWindowID, CGWindowListCopyWindowInfo

    class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, notification):
    workspace = NSWorkspace.sharedWorkspace()
    activeApps = workspace.runningApplications()
    for app in activeApps:
    if app.isActive():
    options = kCGWindowListOptionOnScreenOnly
    windowList = CGWindowListCopyWindowInfo(options,
    kCGNullWindowID)
    for window in windowList:
    if window['kCGWindowOwnerName'] == app.localizedName():
    NSLog('%@', window)
    break
    break
    AppHelper.stopEventLoop()

    def main():
    NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)
    AppHelper.runEventLoop()

    if __name__ == '__main__':
    main()