2012-12-06 48 views
3

是否有可能編寫一個python程序(我將作爲一個守護進程運行,我認爲)可以檢測osx何時進入睡眠狀態以及何時從睡眠狀態恢復?檢測osx睡眠/從睡眠中恢復

道歉,如果聽起來我還沒有研究過 - 我已經遠離了我的舒適區,並且不確定是否需要從python委託給用C編寫的可以報告或不必要的東西。

回答

3

有一些工具可以在IOKit中執行此操作 - 具體地說,在系統休眠之前以及在喚醒之後立即調用註冊爲IORegisterForSystemPower()的回調。

您可能想要查看bb's sleepwatcher daemon,它可以調用您在發生各種事件時指定的命令,包括系統睡眠/喚醒以及各種其他事件(顯示睡眠/喚醒,系統空閒,關機等)。 ..)。

+0

乾杯 - sleepwatcher看起來像一個非常簡單的解決方案:) –

1

Cocoa開發可以監聽NSWorkspaceDidWakeNotification

- (void) receiveSleepNote: (NSNotification*) note 
{ 
    NSLog(@"receiveSleepNote: %@", [note name]); 
} 

- (void) receiveWakeNote: (NSNotification*) note 
{ 
    NSLog(@"receiveWakeNote: %@", [note name]); 
} 

- (void) fileNotifications 
{ 
    //These notifications are filed on NSWorkspace's notification center, not the default 
    // notification center. You will not receive sleep/wake notifications if you file 
    //with the default notification center. 
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
      selector: @selector(receiveSleepNote:) 
      name: NSWorkspaceWillSleepNotification object: NULL]; 

    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
      selector: @selector(receiveWakeNote:) 
      name: NSWorkspaceDidWakeNotification object: NULL]; 
} 

此示例代碼是從蘋果公司的文件在這裏摘錄:Registering and unregistering for sleep and wake notifications