I tried to use Application Scripting Bridge to send my Mac to sleep. The code look like the following:
#import "Finder.h"
FinderApplication *Finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
[Finder sleep];
But it doesn't work. Any ideas why it doesn't work? No compiling errors or warnings, but it doesn't work…
As I posted in this answer, I've been using the following code for over 8 years without issues:
MDRestartShutdownLogout.h:
MDRestartShutdownLogout.m:
Note that the above code is based on the code from Technical Q&A QA1134, but mine is re-worked to use
AESendMessage()
rather thanAESend()
.AESend()
is inHIToolbox.framework
, which is inCarbon.framework
and is therefore unavailable to 64-bit apps. (AESendMessage()
is part of theAE.framework
inCoreServices
).If Scripting Bridge isn't sufficient to do something non-application specific, like shutting down the Mac, then you have the luxury of moving to other frameworks that Applescript (and by extension Scripting Bridge) doesn't have direct access to. For shutting down the Mac, see Core Services: Technical Q&A QA1134: Programmatically causing restart, shutdown and/or logout