tags:

views:

208

answers:

3

Hi, Really simple little function, but does anyone know how to sleep OS X from Java?

Cheers

+1  A: 

See: Making Mac OS X sleep from the command line

Create a script with the following:

#!/bin/bash
osascript << EOT
tell application "System Events"
     sleep
end
EOT

And use system to exec it.

caskey
Sorry could you give the java code for that?
+2  A: 
System.exec("osascript -e 'tell application \"System Events\" to sleep'");
Jherico
A: 
    
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() );
    }
}

For Some reason while i was doing it it did not work without executing it through bash.

Hamza Yerlikaya