سؤال

أرغب في كتابة تطبيق Menubar بسيط لـ Mac OS X. يريد المستخدم فقط استخدام هذا التطبيق عند فتح Safari. لعدم فوضى Menubar دون داع ، أريد أن أختبئ وإظهار أيقونة Menubar اعتمادًا على ما إذا كان Safari مفتوحًا أم لا.

هل ربما هناك بعض الإخطار الذي يمكن أن يسجله تطبيقي؟ الحل الوحيد الذي يمكنني أن أتخيله هو استطلاع عمليات الجري ومعرفة ما إذا كان Safari قد تم إطلاقه ، لكن هذا لا يبدو أنه وسيلة أنيقة لحل مشكلتي ...

هل كانت مفيدة؟

المحلول

NSWorkspaceDidLaunchApplicationNotification و NSWorkspaceDidTerminateApplicationNotification. (هناك أحداث كربونية مكافئة.)

نصائح أخرى

يستخدم keventappfrontswitched في مدير الأحداث الكربون للحصول على إشعارات عندما يصبح تطبيق آخر نشطًا.

استخدم هذا الرمز: http://cl.ly/2lbb

// usleep(40500);

ProcessNotif * x = [[ProcessNotif new] autorelease];
[x setProcessName: @"Safari"];
[x setTarget: self];
[x setAction: @selector(doStuff)];
[x start];

هذا سوف يدير المحدد -doStuff عندما يركض Safari. إذا حصلت على خطأ ، فك usleep() خط.

حصلت على نفس المشكلة ، ولكن بفضل Jwwalker و Documentation وكتب Google هذا الرمز:

// i need to register on button event, you can do it even in applicationDidFinishLaunching
- (IBAction)Btn_LoginAction:(id)sender {
    ...
    NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
    [center addObserver:self selector:@selector(appLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
    [center addObserver:self selector:@selector(appTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
}

// remember to unregister
- (void)ManageLogout:(NSInteger)aResult {
    ...
    NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
    [center removeObserver:self name:NSWorkspaceDidLaunchApplicationNotification object:nil];
    [center removeObserver:self name:NSWorkspaceDidTerminateApplicationNotification object:nil];
}

- (void)appLaunched:(NSNotification *)note {
    [GTMLogger myLog:kGTMLoggerLevelDebug fmt:@"MainWinDelegate::appLaunched: %@ (%@)\n", [[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]];

    if ( [[[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"app.you.monitor.bundle.identifier"] ) {
        // do stuff
    }
}

- (void)appTerminated:(NSNotification *)note {
    [GTMLogger myLog:kGTMLoggerLevelDebug fmt:@"MainWinDelegate::appTerminated: %@ (%@)\n", [[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]];

    if ( [[[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"app.you.monitor.bundle.identifier"] ) {
        // do stuff
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top