差分

ナビゲーションに移動 検索に移動

Xamarin.Mac/スリープからの復帰を検出する

2,296 バイト追加, 2016年5月18日 (水) 02:22
ページの作成:「== 概要 == Mac OS Xスリープからの復帰時にスクリプトを実行したい。 Windowsではタスクスケジューラーで出来る...」
== 概要 ==
[[Mac OS X]]の[[スリープ]]からの復帰時に[[スクリプト]]を実行したい。
[[Windows]]では[[タスクスケジューラー]]で出来るが、[[Mac OS X]]には標準で無さそうなので自作するしかなさそうだ。

== 実装 ==
スリープ状態はNotificationCenterから取得することができる。

「コンピューターのスリープ」と「ディスプレイのスリープ」が別通知なので注意すること。この例では「コンピューターのスリープ」のみ対応しているが、現実的は両方を記述することになると思う。
{| class="wikitable"
|-
| コンピューターのスリープ突入 || NSWorkspace.WillSleepNotification
|-
| コンピューターのスリープ復帰 || NSWorkspace.DidWakeNotification
|-
| ディスプレイのスリープ突入 || NSWorkspace.ScreensDidSleepNotification
|-
| ディスプレイのスリープ復帰 || NSWorkspace.ScreensDidWakeNotification
|}

<source lang="csharp">
using System;
using AppKit;
using Foundation;

namespace NotificationSleepWake
{
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public AppDelegate()
{
}

NSObject _wake;

public override void DidFinishLaunching(NSNotification notification)
{
// Insert code here to initialize your application
if (_wake == null)
{
_wake = NSWorkspace.SharedWorkspace.NotificationCenter.AddObserver(
NSWorkspace.DidWakeNotification,
(notify) =>
{
Console.WriteLine("wake");
});
}
}

public override void WillTerminate(NSNotification notification)
{
if (_wake != null)
{
NSWorkspace.SharedWorkspace.NotificationCenter.RemoveObserver(_wake);
}
}
}
}
</source>

== 関連項目 ==
* [[Mac OS X/日時を強制的にアップデートする]]
* [[Xamarin.Mac/システムステータスバーにアイコンを表示する]]
* [[Xamarin.Mac/Dockアイコンを非表示にする]]

== 参考文献 ==
{{reflist}}

{{stub}}

[[category: Xamarin.Mac]]
匿名利用者

案内メニュー