Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:31:21+00:00 2026-05-27T08:31:21+00:00

I’m trying to identify which OSX application is currently active. I understand that in

  • 0

I’m trying to identify which OSX application is currently active. I understand that in OSX 10.5, this could be done with:

[[NSWorkspace sharedWorkspace] activeApplication]

however, this has been deprecated in 10.6+.

The apple developers documentation states that this should be done through the ‘active’ property of the NSRunningApplication object. I thought one way to approach this might be to get the list of all running applications through

[[NSWorkspace sharedWorkspace] runningApplications]

and then loop through, checking the ‘active’ property of each application. However, the following test code doesn’t behave as I expected: when compiled and run from Terminal.app, only the “terminal” app is ever marked as active, regardless of whether I select a different application.

#import <Foundation/Foundation.h>
#import <AppKit/NSRunningApplication.h>
#import <AppKit/NSWorkspace.h>

int main(int argc, char *argv[]) {
  while(1){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *currApp;
    NSArray *runningApps;
    runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
    for (id currApp in runningApps) {
      if ([currApp isActive])
          NSLog(@"* %@", [currApp localizedName]);
      else
          NSLog(@"  %@", [currApp localizedName]);
    }
    sleep(1);
    [pool release];
  }

  return 0;
}

What am I doing wrong? Have I misunderstood how the “active” property works?

(Also, please feel free to criticise my Objective C code — this is my first attempt at objective C, so I know it’s likely to be hideously ugly to the trained eye! Please forgive me! 🙂 Any suggestions welcome.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:31:21+00:00Added an answer on May 27, 2026 at 8:31 am

    Your problem is that your application is unable to receive any events from the system informing it that the current application has changed, and therefore it never updates the active property on the NSRunningApplication instances. If I use the exact same code, but another app is active when I start running the code, it reports that application instead.

    If instead you change your code to run the main thread’s NSRunLoop and use a 1-second timer, it should work.

    Here’s a quick example:

    #import <Foundation/Foundation.h>
    #import <AppKit/AppKit.h>
    
    @interface Foo : NSObject
    - (void)run;
    @end
    
    @implementation Foo
    - (void)run {
        for (NSRunningApplication *currApp in [[NSWorkspace sharedWorkspace] runningApplications]) {
            if ([currApp isActive]) {
                NSLog(@"* %@", [currApp localizedName]);
            } else {
                NSLog(@"  %@", [currApp localizedName]);
            }
        }
        NSLog(@"---");
    }
    @end
    
    int main(int argc, char *argv[]) {
        NSAutoreleasePool *p = [NSAutoreleasePool new];
    
        Foo *foo = [[Foo new] autorelease];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                          target:foo
                                                        selector:@selector(run)
                                                        userInfo:nil
                                                         repeats:YES];
        [[NSRunLoop mainRunLoop] run];
    
        [p release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.