2010-09-12 94 views
0

我想爲Mac OS X編寫一個簡單的菜單欄應用程序。當Safari打開時,用戶只想使用該應用程序。爲了不會不必要地混亂菜單欄,我想根據Safari是否打開來隱藏和顯示菜單欄圖標。Mac OS X - 顯示器應用程序啓動?

是否有可能會通知我的應用可以註冊?唯一的解決方法是我可以想象的是輪詢正在運行的進程,並查看Safari是否啓動,但這似乎不是一種優雅的方式來解決我的問題...

回答

3

NSWorkspaceDidLaunchApplicationNotificationNSWorkspaceDidTerminateApplicationNotification。 (有碳當量活動。)

0

使用此代碼:http://cl.ly/2LbB

// usleep(40500); 

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

Safari瀏覽器中運行時,這將運行選擇-doStuff。如果出現錯誤,請取消usleep()行的註釋。

+0

如何進行'usleep'呼叫幫助的「錯誤」?你指的是什麼樣的「錯誤」? – 2010-09-14 03:08:23

+0

我一直在使用一些開源代碼,並且出現了某種錯誤,並取消註釋該行解決了該問題。 – 2010-09-14 19:59:35

0

得到了同樣的問題,但由於JWWalker,文檔和谷歌寫了這個代碼:

// 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 
    } 
} 
相關問題