The Notification NSWorkspaceDidLaunchApplicationNotification did not always arrive.

Discussion in 'Mac Programming' started by petron, Dec 14, 2010.

  1. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #1
    Hi folks,
    I have following example of code:

    Code:
    - (void)appHasLaunched:(NSNotification *)notif {
      NSLog(@"Notification arrived");
    }
    
    
    - (id)init {
      self = [super init];
      if (self) {
        NSWorkspace *ws = [NSWorkspace sharedWorkspace];
        NSNotificationCenter *notifyCenter = [ws notificationCenter];
        [notifyCenter addObserver:self
                         selector:@(appHasLaunched:)
                             name:NSWorkspaceDidLaunchApplicationNotification
                           object:nil];
    
        if ([ws launchApplication:@"AnApp.app"]) {
           NSLog(@"Launch Returened True");
        } else {
           NSLog(@"Launch Returned False");
        }		
      }

    What I wanted to achieve with it is to get notification when the "AnApp" has been launched.
    I do get the notification only when the "AnApp" application is not active and I call "launchApplication".

    When the "AnApp is already up and running and my application do try to launch it again the method still returns true but I do not get any notification back.

    Is there any thing that I do wrong ?
    Do I call the launchApplication too early ?

    Any help will be appreciated.

    b.r.
    Petron
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    launchApplication will try to launch the app if it is not already running, and will return YES if the app is running after the call.

    The notification will be called whenever _any_ application is launched.

    So: launchApplication will _not_ notify you if the app is already launched because the call doesn't launch the app. And: When you get the notification, you have to check whether it is about the app you are interested in.

    Check out "runningApplications" to see which apps are already running before you call launchApplication.
     
  3. petron thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #3
    Hi Gnasher,
    Tnx for the reply.

    Your explanation sound logical, but reading the documentation I understood that the notification will be called even when the application is already up and running. I may have interpreted it wrongly.

    I will try to look closer at my implementation.

    B.R.
    Petron
     
  4. petron thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #4
    Based on the info from Gnasher i did changed my implementation to use the
    combination of:
    call to runningApplications and NSWorkspaceDidLaunchApplicationNotification notification.

    The first I do is to check if the application is "running" and if it is not I do launch it manually and wait for the notification to arrive.

    It seems stable and it works perfect with cost of only few extra lines of code.

    Thanks.
    Petron
     

Share This Page