2009-11-18 7 views
2

J'ai le code suivant pour détecter la fenêtre actuelle. Comment puis-je obtenir 1) nom interne de l'application, 2) emplacement, 3) éditeur et 4) description de la fenêtre/application?Comment obtenir les détails d'une application en utilisant Objective-C?

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

//Get info about the currently active application. 
NSWorkspace* workspace   = [NSWorkspace sharedWorkspace]; 
NSDictionary* currentAppInfo  = [workspace activeApplication]; 

//Get the PSN of the current application. 
UInt32 lowLong     = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue]; 
UInt32 highLong     = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue]; 
ProcessSerialNumber currentAppPSN = {highLong,lowLong}; 

//Grab window information from the window server. 
CFArrayRef windowList    = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID); 
ProcessSerialNumber myPSN   = {kNoProcess, kNoProcess}; 

//Loop through the windows, the window list is ordered from front to back. 
for (NSMutableDictionary* entry in (NSArray*) windowList) 
{ 
    int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue]; 
    GetProcessForPID(pid, &myPSN); 

    //If the process of the current window in the list matches our process, get the front window number. 
    if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN) 
    { 
     NSNumber *windowNumber = [entry objectForKey:(id)kCGWindowNumber]; 
     windowNumber = [entry objectForKey:(id)kCGWindowNumber]; 
     NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName]; 
     NSLog(@"Capture the window: %@ with window ID: %@.",applicationName,windowNumber); 
     return applicationName; 

     //Break because we only want the front window. 
     break; 
    } 
} 
CFRelease(windowList); 
[pool release]; 

Répondre

1

Vous devez utiliser la fonction ProcessInformationCopyDictionary de l'API Process Manager. Donnez-lui &myPSN et kProcessDictionaryIncludeAllInformationMask comme arguments et vous obtiendrez l'information que vous recherchez.

0

Je cherchais quelque chose en rapport avec ce sujet. J'ai besoin d'un WindowRef de la fenêtre ou partie de la fenêtre à un certain endroit (position de la souris) et il doit être sur toutes les fenêtres de toutes les applications en cours d'exécution ...

Je l'ai essayé avec Carbon ('Cos my App est entièrement écrit en C++) mais Ive a trouvé que certaines fonctions de carbone Indifférent travail correctement (MacFindWindow, FindWindow, HIWindowFindAtLocation, FindWindowOfClass, HIWindowGetCGWindowID ...)

Peut-être que je suis fait mal, est difficile voir croire que ces fonctions Carbon ne fonctionneront plus dans les architectures 64 bits ...

Donc, en rapport avec votre question j'ai trouvé le même code et j'ai essayé mais ce n'est pas ce dont j'ai besoin, j'espère aide yo de toute façon et je continuerai à chercher et à essayer jusqu'à ce que je l'obtienne (si l'O.S peut le faire, tout le monde le devrait).

//if the process of the current window in the list matches our process, get the front window number 
    if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN) 
    { 
     NSNumber* windowNumber = [entry objectForKey:(id)kCGWindowNumber]; 
     NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName]; 
     NSLog(@"The current app is %@ and the window number of its front window is %@.",applicationName,windowNumber); 
     CGRect bounds; 
     CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[entry objectForKey:(id)kCGWindowBounds], &bounds); 
     NSLog(@"WINDOW RECT BOUNDS; (x,y,width, height) = (%d,%d, %d, %d)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); 


     break; 
    } 

Aussi, suivez ce lien, je vais vous aider. Je suis sûr:

http://code.google.com/p/blazingstars/source/browse/trunk/PokerHK/HKLowLevel.m?r=70

Questions connexes