Obtaining applications enlisted in force quit window

59 views
Skip to first unread message

Devarshi

unread,
Jul 5, 2012, 2:16:45 PM7/5/12
to cocoa-unbound
Hi All,

I am trying to retrieve all applications which are enlisted in force
quit window.

When I am using runningAppications method using below line of code:

[[NSWorkspace sharedWorkspace] runningApplications];

It is enlisting many extra applications which are not enlisted in
force quit window.

Can any one suggest me some way to achieve it?

Thanks

Charles Parnot

unread,
Jul 5, 2012, 2:47:21 PM7/5/12
to cocoa-...@googlegroups.com
Not sure if you'll get an exact match, but the first thing I would do is check the `activationPolicy` of each app, and only keep the ones with `NSApplicationActivationPolicyRegular`. This seems to be directly dependent on the LSUIElement flag in the Info.plist, which dictates wether an app appears in the Dock.

@property(readonly) NSApplicationActivationPolicy activationPolicy

charles
--
Charles Parnot
charles...@gmail.com
twitter: @cparnot
http://mekentosj.com


Jim Dovey

unread,
Jul 5, 2012, 2:49:15 PM7/5/12
to cocoa-...@googlegroups.com
You're going to need to use the HIServices framework to achieve what you want to do. Don't worry, it's included by default; just #import <ApplicationServices/ApplicationServices.h>. Then you'll need to get a LaunchServices ProcessSerialNumber for each app and use that to get the information dictionary for those apps. You will want to ignore any apps where the LSUIElement value is non-zero, or the LSBackgroundOnly value is non-zero.

Here's a sample for you:

#import <Cocoa/Cocoa.h>
#import <ApplicationServices/ApplicationServices.h>

@implementation MyAppDelegate

- (void) applicationDidFinishLaunching: (NSNotification *) note
{
NSMutableArray * visibleApps = [NSMutableArray new];
NSMutableArray * hiddenApps = [NSMutableArray new];
NSArray * runningApps = [[NSWorkspace sharedWorkspace] runningApplications];

for ( NSRunningApplication * app in runningApps )
{
ProcessSerialNumber psn;
if ( GetProcessForPID(app.processIdentifier, &psn) != noErr )
{
NSLog(@"Failed to get process serial number for %@!", app.localizedName);
continue;
}

NSDictionary * info = CFBridgingRelease(ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask));
if ( info == nil )
{
NSLog(@"Failed to get process information for %@!", app.localizedName);
continue;
}

if ( [[info objectForKey: @"LSBackgroundOnly"] boolValue] || [[info objectForKey: @"LSUIElement"] boolValue] )
{
// it's not a user-facing app
[hiddenApps addObject: app];
}
else
{
// it's user-facing
[visibleApps addObject: app];
}
}

NSLog(@"User-visible apps: %@", [visibleApps valueForKey: @"localizedName"]);
NSLog(@"Hidden apps: %@", [hiddenApps valueForKey: @"localizedName"]);
}

@end

Jim Dovey

unread,
Jul 5, 2012, 2:50:37 PM7/5/12
to cocoa-...@googlegroups.com
Hah, didn't see that one— that's exactly what you need. You want to filter out any UI element and background-only apps, which is what that flag describes.
Reply all
Reply to author
Forward
0 new messages