2009-01-26 17 views
15

olarak edinin. Daha önce sorulan bir question başvurulan, geçerli etkin belgenin başlığını nasıl alacağımı öğrenmek istiyorum.Geçerli etkin pencerenin/belgenin başlığını Mac OS X

Yukarıdaki soruya verilen cevaplarda yer alan betiği denedim. Bu çalışır, ancak bana sadece uygulamanın adını verir. Örneğin, şu soruyu yazıyorum: Komut dosyasını açtığımda bana uygulamanın adını veriyor, yani "Firefox". Bu oldukça düzgün, ama gerçekten yardımcı olmuyor. Mevcut etkin belgemin başlığını yakalamak isterim. Resme bakın.

Firefox title http://img.skitch.com/20090126-nq2egknhjr928d1s74i9xixckf.jpg

Ben Leopard kullanıyorum, böylece hiçbir geriye dönük uyumluluk gerekli. Ayrıca NSWorkspace sınıfına erişmek için Python Appkit'i kullanıyorum, ancak bana Objective-C kodunu söylerseniz, Python'a çeviriyi anlayabilirim.


Tamam, çok tatmin edici olmayan bir çözümüm var, bu yüzden Koen Bok'un cevabını işaretlemiyorum. En azından henüz değil.

tell application "System Events" 
set frontApp to name of first application process whose frontmost is true 
end tell 
tell application frontApp 
if the (count of windows) is not 0 then 
    set window_name to name of front window 
end if 
end tell 

Komut dosyası olarak kaydedin ve kabuktan osascript ile çağırın.

+0

Elmalar için teşekkür ederiz. Bunu python ile yapmanın bir yolunu buldun mu? – Amjith

cevap

2

Objective-C, kısa cevap olarak biraz Kakao kullanarak ve: Ama AppleScript yüzden

Bu biraz yardımcı olabilir :-) sorgulayıcı bir egzersiz bırakın bana sihirli Carbon Accessibility API olup:

// Get the process ID of the frontmost application. 
NSRunningApplication* app = [[NSWorkspace sharedWorkspace] 
           frontmostApplication]; 
pid_t pid = [app processIdentifier]; 

// See if we have accessibility permissions, and if not, prompt the user to 
// visit System Preferences. 
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES}; 
Boolean appHasPermission = AXIsProcessTrustedWithOptions(
          (__bridge CFDictionaryRef)options); 
if (!appHasPermission) { 
    return; // we don't have accessibility permissions 

// Get the accessibility element corresponding to the frontmost application. 
AXUIElementRef appElem = AXUIElementCreateApplication(pid); 
if (!appElem) { 
    return; 
} 

// Get the accessibility element corresponding to the frontmost window 
// of the frontmost application. 
AXUIElementRef window = NULL; 
if (AXUIElementCopyAttributeValue(appElem, 
     kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) { 
    CFRelease(appElem); 
    return; 
} 

// Finally, get the title of the frontmost window. 
CFStringRef title = NULL; 
AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute, 
        (CFTypeRef*)&title); 

// At this point, we don't need window and appElem anymore. 
CFRelease(window); 
CFRelease(appElem); 

if (result != kAXErrorSuccess) { 
    // Failed to get the window title. 
    return; 
} 

// Success! Now, do something with the title, e.g. copy it somewhere. 

// Once we're done with the title, release it. 
CFRelease(title); 

Alternatif olarak, this StackOverflow answer de ima edildiği gibi, CGWindow API kullanımı daha basit olabilir.