我想在AppleScriptObjC中防止睡眠。我发现了这个问题:
What is the correct way to prevent sleep on 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