Question

I want to get the applications from the cmd+tab menu in OS X. The best way I got now is to associate this with a AppleScript call with the following:

NSDictionary *errorDict;
NSAppleEventDescriptor *returnValue;
NSString *appleScriptText = @"tell application \"System Events\" to get name of (processes where background only is false)";
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:appleScriptText];

Then loop through the stuff coming back from it and to match it to the [[NSWorkspace sharedWorkspace] runningApplications] but this seems a little bit too weird to get this task done.

So my question here: is there a way that is not so quirky like this one?

I am really tense for the answers.

Was it helpful?

Solution

Given that you're already familiar with -[NSWorkspace runningApplications], why don't you just iterate over those and check which ones meet your criteria? The background only property corresponds to NSRunningApplication's activationPolicy property being something other than NSApplicationActivationPolicyRegular.

So, something like (not tested):

NSArray* apps = [[NSWorkspace sharedWorkspace] runningApplications];
NSIndexSet* indexes = [apps indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
    return [obj activationPolicy] == NSApplicationActivationPolicyRegular;
}];
NSArray* names = [[apps objectsAtIndexes:indexes] valueForKey:@"localizedName"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top