本当に単純な小さな機能ですが、Java から OS X をスリープさせる方法を知っている人はいますか?
乾杯
System.exec("osascript -e 'tell application \"System Events\" to sleep'");
次のスクリプトを作成します。
#!/bin/bash
osascript << EOT
tell application "System Events"
sleep
end
EOT
そして、システムを使用して実行します。
public void gotoSleep(){
try{
logger.finer("Zzz...");
if (preferences.getOS().equals("OSX") == true ){
Process p = Runtime.getRuntime().exec
("/bin/bash");
String command = "osascript -e 'tell application \"System Events\"' "
+ " -e \"sleep\" -e 'end tell'";
OutputStream stdin = p.getOutputStream();
stdin.write( command.getBytes() );
stdin.flush();
stdin.close();
}
}catch( Exception e ) {
logger.warning( e.toString() );
}
}
何らかの理由で、bashを介して実行しないと機能しませんでした。