Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I posted in <a href="https://stackoverflow.com/questions/6103962/perform-certain-system-events-mac-os-x/6105338#6105338">this answer</a>, I've been using the following code for over 8 years without issues:</p> <p>MDRestartShutdownLogout.h:</p> <pre><code>#import &lt;CoreServices/CoreServices.h&gt; /* * kAERestart will cause system to restart * kAEShutDown will cause system to shutdown * kAEReallyLogout will cause system to logout * kAESleep will cause system to sleep */ extern OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSend); </code></pre> <p>MDRestartShutdownLogout.m:</p> <pre><code>#import "MDRestartShutdownLogout.h" OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSendID) { AEAddressDesc targetDesc; static const ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess }; AppleEvent eventReply = {typeNull, NULL}; AppleEvent eventToSend = {typeNull, NULL}; OSStatus status = AECreateDesc(typeProcessSerialNumber, &amp;kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &amp;targetDesc); if (status != noErr) return status; status = AECreateAppleEvent(kCoreEventClass, eventToSendID, &amp;targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &amp;eventToSend); AEDisposeDesc(&amp;targetDesc); if (status != noErr) return status; status = AESendMessage(&amp;eventToSend, &amp;eventReply, kAENormalPriority, kAEDefaultTimeout); AEDisposeDesc(&amp;eventToSend); if (status != noErr) return status; AEDisposeDesc(&amp;eventReply); return status; } </code></pre> <p>Note that the above code is based on the code from <a href="http://developer.apple.com/library/mac/#qa/qa1134/_index.html" rel="nofollow noreferrer">Technical Q&amp;A QA1134</a>, but mine is re-worked to use <code>AESendMessage()</code> rather than <code>AESend()</code>. <code>AESend()</code> is in <code>HIToolbox.framework</code>, which is in <code>Carbon.framework</code> and is therefore unavailable to 64-bit apps. (<code>AESendMessage()</code> is part of the <code>AE.framework</code> in <code>CoreServices</code>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload