1

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
Community
  • 1
  • 1
subdiox
  • 387
  • 2
  • 14
  • Xcode gave me a warning on the following line of code, saying that the pointer types were incompatible. CFStringRef* reasonForActivity= CFSTR("Describe Activity Type"); I fixed the warning by changing the line of code to being the following: CFStringRef reasonForActivity = CFSTR("Describe Activity Type"); Remember that in a CFStringRef, the "Ref" means that it is already declared as a pointer. – Kaydell Aug 28 '13 at 12:07
  • Will you post your AppleScript-ObjectiveC code that doesn't work? – Kaydell Aug 28 '13 at 12:09
  • Sorry, I couldn't convert Objective-C to AppleScriptObjC though I can always do, because I don't know how to write CFSTR("Describe Activity Type") in ASOC. ("IOPMAssertionID assertionID" also) – subdiox Aug 28 '13 at 12:18
  • I will post my wrong ASOC code in EDIT section. – subdiox Aug 28 '13 at 12:19
  • I posted it in EDIT section. – subdiox Aug 28 '13 at 12:29
  • I didn't compile the objective-c code, I couldn't notice it. Thank you. I have learned something today! – subdiox Aug 28 '13 at 15:30

0 Answers0