Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When calling TransformProcessType(), the app menu doesn't show up

If you call TransformProcessType() like this :

ProcessSerialNumber psn = { 0, kCurrentProcess }; 
(void) TransformProcessType(&psn, kProcessTransformToForegroundApplication);

Then the cocoa app menu doesn't show up unless you call this early enough in your app (eg. in applicationWillFinishLaunching).

like image 290
Benjamin Ortuzar Avatar asked Sep 29 '11 11:09

Benjamin Ortuzar


2 Answers

I asked Apple for help and they helped me very well. Quote :

The reason why the menu bar isn't show when you call TransformProcessType is that your app is already the active app (that is, [[NSRunningApplication currentApplication] isActive] returns YES) and the menu bar for an app is shown when the app is activated

This is their workaround :

- (void)transformStep1 {
    for (NSRunningApplication * app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"]) {
        [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
        break;
    }
    [self performSelector:@selector(transformStep2) withObject:nil afterDelay:0.1];
}

- (void)transformStep2
{
    ProcessSerialNumber psn = { 0, kCurrentProcess }; 
    (void) TransformProcessType(&psn, kProcessTransformToForegroundApplication);

    [self performSelector:@selector(transformStep3) withObject:nil afterDelay:0.1];
}

- (void)transformStep3
{
    [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
like image 167
Benjamin Ortuzar Avatar answered Nov 13 '22 12:11

Benjamin Ortuzar


Here is how I made it working.

BOOL MakeAppForeground()
{
    BOOL bSuccess = TranformAppToState(kProcessTransformToForegroundApplication);

    if(bSuccess)
    {
         bSuccess = (SetSystemUIMode(kUIModeNormal, 0) == 0);
        [NSApp activateIgnoringOtherApps:YES];
    }

    return bSuccess;
}

BOOL MakeAppBackground()
{
    return TranformAppToState(kProcessTransformToBackgroundApplication);
}

BOOL TranformAppToState(ProcessApplicationTransformState newState)
{
    ProcessSerialNumber psn = { 0, kCurrentProcess };
    OSStatus transformStatus = TransformProcessType(&psn, newState);

    if((transformStatus != 0))
    {
        NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:transformStatus userInfo:nil];
        NSLog(@"TranformAppToState: Unable to transform App state. Error - %@",error);
    }

    return (transformStatus == 0);
}
like image 23
Omkar Avatar answered Nov 13 '22 13:11

Omkar