Prevent sleep in AppleScriptObjC

309 Views Asked by At

I want to prevent sleep in AppleScriptObjC. I found this question:

What is the correct way to prevent sleep on OS X?

I can prevent sleep with Objective-C, but I can't do it with AppleScriptObjC. How do I do it?

Graham Miln's example:

#import <IOKit/pwr_mgt/IOPMLib.h>

// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep

//reasonForActivity is a descriptive string used by the system whenever it needs 
//  to tell the user why the system is not sleeping. For example, 
//  "Mail Compacting Mailboxes" would be a useful string.

//  NOTE: IOPMAssertionCreateWithName limits the string to 128 characters. 
CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");

IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, 
                                kIOPMAssertionLevelOn, reasonForActivity, &assertionID); 
if (success == kIOReturnSuccess)
{

    //Add the work you need to do without 
    //  the system sleeping here.

    success = IOPMAssertionRelease(assertionID);
    //The system will be able to sleep again. 
}

EDIT

I translated his code into ASOC.

tell class "CFStringRef" of current application
    set reasonForActivity to CFSTR("Describe Activity Type")
end tell
tell class "IOPMAssertionID" of current application
    set assertionID
end tell
tell class "IOReturn" of current application
    set success to IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, 
                                kIOPMAssertionLevelOn, reasonForActivity, assertionID)
end tell
if success = kIOReturnSuccess then
    -- Add the work you need to do without the system sleeping here.
    set success to IOPMAssertionRelease(assertionID)
    -- The system will be able to sleep again.
end if
0

There are 0 best solutions below