Search code examples
cocoamacosbackground-processnsrunningapplication

How to hide window of UIAgent process with cocoa


I have an UIAgent application with one window. I want to hide/show it from another application.How do I do it with cocoa? Seems like hide/unhide methods of NSRunningApplication doesn't affect UIAgent processes.

Thanks in advance


Solution

  • I solved it with NSDistributionNotifications. In the UIAgent application I add an observer to a @"QuitProcessNotification" (any other name):

    [[NSDistributedNotificationCenter defaultCenter]
                                 addObserver:self selector:@selector(quit:) 
                                 name:@"QuitProcessNotification" 
                                 object:@"com.MyCompany.MyApp" 
                                 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
    

    The callback looks like that:

    - (void) quit:(NSNotification *) notification
    {
        [NSApp terminate:nil];
    }
    

    In the main application: Sending notification:

    [[NSDistributedNotificationCenter defaultCenter]
                         postNotificationName:@"QuitProcessNotification" 
                         object:@"com.MyCompany.MyApp"
                         userInfo: nil /* no dictionary */
                         deliverImmediately: YES];
    

    Be sure, that the object parameter is indeed your sender application's bundle identifier.