Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

petron

macrumors member
Original poster
May 22, 2009
95
0
Malmo, Sweden
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
 
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.
 
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
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.