2016-05-09 9 views
1

Aus der ganzen API-Dokumentation, die ich finden kann, scheint es, als ob das "vorderste" Fenster von System Events oder der Accessibility API überprüft wird (Beispiel in Python hier, aber das ist das gleiche) in ObjC oder schnellen oder Rubin oder was auch immer):Wie kann ich das Fenster mit dem aktiven Tastaturfokus mithilfe von ScriptingBridge (oder AppleScript) ermitteln?

#!/usr/bin/env python 
from ScriptingBridge import SBApplication 
events = SBApplication.applicationWithBundleIdentifier_(
    "com.apple.systemevents") 
for proc in events.applicationProcesses(): 
    if proc.frontmost(): 
     print(proc.name()) 

der Wert I aus diesem wieder ist die gleiche wie von NSWorkspace.sharedWorkspace().frontmostApplication(). Und es ist normalerweise korrekt. Außer wenn ein Eingabeaufforderungsdialog, insbesondere einer aus dem System, tatsächlich ist, was hat der Tastaturfokus. Zum Beispiel, wenn Messages.app ein Passwort für mein Jabber-Konto haben möchte oder wenn sich mein iCloud-Passwort ändert; Diese Dialoge scheinen aus dem Prozess UserNotificationCenter zu stammen, der sich selbst nicht als vorderste Anwendung meldet, obwohl er definitiv einen Tastaturfokus hat.

+0

Dialogfenster für Benutzername und Passwort gehören zu fragen, um den Prozess 'SecurityAgent' – vadian

+0

Winfo.app - von http://www.irradiatedsoftware.com/labs/ - sagt mir, dass der Dialog, den ich betrachtete, von 'UserNotificationCenter' stammte. – Glyph

Antwort

1

"UserNotificationCenter" und "UserNotificationCenter" sind Hintergrundanwendungen (die NSUIElement Schlüssel 1 in den info.plist).

proc.frontmost() ist immer false auf Prozess, der im Hintergrund ist (kein Menü und nicht im Dock).

Und NSWorkspace.sharedWorkspace().frontmostApplication() funktioniert nicht auf Hintergrundanwendung.


Um die aktive Anwendung zu erhalten, verwenden Sie die activeApplication Methode aus der NSWorkspace Klasse

Hier ist das Apple:

set pyScript to "from AppKit import NSWorkspace 
activeApp = NSWorkspace.sharedWorkspace().activeApplication() 
print activeApp['NSApplicationName'].encode('utf-8') 
print activeApp['NSApplicationProcessIdentifier']" 

set r to do shell script "/usr/bin/python -c " & quoted form of pyScript 
set {localizedAppName, procID} to paragraphs of r -- procID is the unix id 

aktualisieren mit einem nicht veraltet Methode:

set pyScript to "from AppKit import NSWorkspace 
for app in NSWorkspace.sharedWorkspace().runningApplications(): 
     if app.isActive(): 
       print app.localizedName().encode('utf-8') 
       print app.processIdentifier() 
       break" 

set r to do shell script "/usr/bin/python -c " & quoted form of pyScript 
set {localizedAppName, procID} to paragraphs of r -- procID is the unix id 

Um die Frontscheibe aus einem Prozess-ID, verwenden Sie die procID Variable, wie diese:

tell application "System Events" 
    tell (first process whose unix id = procID) 
     log (get properties) -- properties of this process 
     tell window 1 to if exists then log (get properties) -- properties of the front window of this process 
    end tell 
end tell 
+0

'activeApplication' scheint jedoch seit 10.7 veraltet zu sein. – Glyph

+0

Nein, 'activeApplication' ist seit ** OS X v10.11 ** veraltet, arbeitet aber immer noch an' 10.11.4'. Ich füge eine andere Methode (nicht veraltet) in meine Antwort ein. – jackjr300

Verwandte Themen