Shutdown on mac?

hi !
im currently working for an art exhibition for the museum of film in frankfurt. the people there ask for an easy way to switch on/off the installation. For off-switching they have to press ESC, point on the apple icon in the left upper corner, select “ausschalten…” and so on. is there a way to shutdown inside openframeworks app ?

greetings ascorbin

:?

i usually do it like that:
most exhibitions have fixed closing times so you just have to program your mac to switch off automatically after the closing hour. you can do that in the system settings -> energy saver -> schedule or systemeinstellungen -> energie sparen -> zeitplan… on a german system.
but sometimes applications can veto the shutdown then you’ll have to quit the program before or have it quit by itself at a certain time. for my last installations i wrote a shell script to kill my program or restart it in case of premature crash.

best
joerg

I am using the following code-snippet. Essentially you are sending an apple-event to the system process, this is the same, as choosing “shutdown” from the apple-menu.

  
  
  
#include <Carbon/Carbon.h>  
  
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend)  
{  
    AEAddressDesc targetDesc;  
    static const ProcessSerialNumber  
         kPSNOfSystemProcess = { 0, kSystemProcess };  
    AppleEvent eventReply = {typeNull, NULL};  
    AppleEvent appleEventToSend = {typeNull, NULL};  
  
    OSStatus error = noErr;  
  
    error = AECreateDesc(typeProcessSerialNumber,  
        &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess),  
        &targetDesc);  
  
    if (error != noErr)  
    {  
        return(error);  
    }  
  
    error = AECreateAppleEvent(kCoreEventClass, EventToSend,  
                     &targetDesc, kAutoGenerateReturnID,  
                     kAnyTransactionID, &appleEventToSend);  
  
    AEDisposeDesc(&targetDesc);  
  
    if (error != noErr)  
    {  
        return(error);  
    }  
  
    error = AESend(&appleEventToSend, &eventReply, kAENoReply,  
             kAENormalPriority, kAEDefaultTimeout,  
             NULL, NULL);  
  
    AEDisposeDesc(&appleEventToSend);  
  
    if (error != noErr)  
    {  
        return(error);  
    }  
  
    AEDisposeDesc(&eventReply);  
  
    return(error); //if this is noErr then we are successful  
}  
  

To shutdown the computer you do something like this

  
  
OSErr error = SendAppleEventToSystemProcess(kAEShutDown);  
  

I haven’t tried this code with OpenFrameworks, you your mileage my vary.

cheers,
Stephan

not completely sure on mac but you should be able to do:

  
system("shutdown -h")  

if you call it in testApp::exit() it will shutdown the computer whenever you quit the app.

Hey !
Thank you all a lot ! The code from " sth " works quite well. When you put " OSErr error = SendAppleEventToSystemProcess(kAEShutDown); " in testApp::keyPressed it even goes down when you hit ESC. another thing: my application is not focused when i start it as a startup item, so you first have to click on it then you can do keypressing-events. how can i focuse it ?

greetings

ok
for mac i found this:

this sets my cursor at position 100 | 100 and performs a mouseclick, so i have a focus. hm… other solutions?

CGPoint point;
point.x = 100;
point.y = 100;
CGWarpMouseCursorPosition(point);
CGPostMouseEvent(point, FALSE, 1, TRUE); //mouse down
CGPostMouseEvent(point, FALSE, 1, FALSE);//mouse up

greetings

Seems like you have solved most of the problems, but one tip is to use launchd to assure that if your app crashes, it auto restarts. You can use the oensource program Lingo to easy set it up.

Here are my 5 cents to the issue.

We also use mac’s for exhibitions with limited access to the mac computers.

I made this small app that runs and listens to the Notification = com.apple.logoutInitiated

Basically when you pressed the mac power button once it shuts down. you don’t have to choose and click any button in the “are you sure you want to shut down your computer now” window.

link to app:
http://www.maybevideodoes.de/download/Direct-Shutdown.zip

I used sample code ffom here
http://forum.macfr.com/index.php?showtopic=24864
and here
http://developer.apple.com/qa/qa2001/qa1134.html

stephan.

–update–
i just noticed it does not work with mac mini’s and their power button.

i often just run a shell script to shut down a machine (in case there are problems).

sudo -S shutdown now

since shutdown is a protected command, i have to load in the admin password, which isn’t the safest thing but fun for an exhibition computer that nobody has access to.

sudo -S shutdown now < $HOME/linktpasswordinatextfile

i could never figure out how to keep that as a single command.

jeremy

this way works too :hotsprings:

2 Likes