1

我想防止 AppleScriptObjC 中的睡眠。我发现了这个问题:

在 OS X 上防止睡眠的正确方法是什么?

我可以用 Objective-C 防止睡眠,但我不能用 AppleScriptObjC 做到这一点。我该怎么做?

格雷厄姆·米尔恩的例子:

#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. 
}

编辑

我将他的代码翻译成 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
4

0 回答 0