Sample Script - Long Running Task
You have a long running task that needs to be performed. You also want to let your Mac sleep after idle time or inactivity.
The simplest option is to switching off idle time sleep and let the task run without interruption. The downside is it wastes energy and is prone to others turning off the idle Mac without mentioning to you. Just the kind of thing you fear discovering when it is too late.
Another answer is to use the following script. It creates a series of wake and sleep events to ensure your Mac is awake to perform the long task.
The sample usage here schedules a task called Back Up at 1am, lasting at most 150 minutes, on Tuesday and Thursday.
- Wake for Back Up, Start up or wake every Tuesday and Thursday at 1:00 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 1:14 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 1:29 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 1:44 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 1:59 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 2:14 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 2:29 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 2:44 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 2:59 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 3:14 AM
- Coffee for Back Up, Start up or wake every Tuesday and Thursday at 3:29 AM
- Sleep after Back Up, Sleep every Tuesday and Thursday at 3:30 AM
-- Our Mac will idle sleep after fifteen minutes
property kUserInactivityBeforeSleepInSeconds : (60 * 15)
-- BackUp starts at 1am, lasts 150 minutes, every Tuesday and Thursday
scheduleTask("Back Up", (60 * 60), (60 * 150), [Tuesday, Thursday])
-- Nothing to tweak from here on...
on scheduleTask(inTaskUniqueID, inTaskStartsInSecondsFromMidnight, inTaskDurationInSeconds, inDays)
-- Schedule events to wake up and stay awake until the task has completed
-- Schedule the wake event first
tell application "Power Manager Scripting"
-- Create a record of properties to populate our new event
set wakeEvent to {name:"Wake for " & inTaskUniqueID, unique id:inTaskUniqueID & "-wake", action:{type class:start up or wake}, trigger:{type class:daily, seconds from midnight:inTaskStartsInSecondsFromMidnight, days:inDays}, enabled:true}
tell scheduler to add event wakeEvent
-- Make sure the Mac is awake during the entire task
-- How many wake events are needed?
set requiredWakes to round ((inTaskDurationInSeconds / kUserInactivityBeforeSleepInSeconds))
set eventTime to inTaskStartsInSecondsFromMidnight
repeat requiredWakes times
-- Schedule coffee one minute before sleep
set eventTime to eventTime + kUserInactivityBeforeSleepInSeconds - 60
-- Copy the wakeEvent properties and modify
set coffeeEvent to wakeEvent
set name of coffeeEvent to "Coffee for " & inTaskUniqueID
set unique id of coffeeEvent to inTaskUniqueID & "-coffee-" & (eventTime as string)
set seconds from midnight of trigger of coffeeEvent to eventTime
tell scheduler to add event coffeeEvent
-- Stop eventTime from drifting
set eventTime to eventTime + 60
end repeat
-- Schedule the sleep event
-- Copy the wakeEvent properties and modify
set sleepEvent to wakeEvent
set name of sleepEvent to "Sleep after " & inTaskUniqueID
set unique id of sleepEvent to inTaskUniqueID & "-sleep"
set type class of action of sleepEvent to sleep
set seconds from midnight of trigger of sleepEvent to (inTaskStartsInSecondsFromMidnight + inTaskDurationInSeconds)
tell scheduler to add event sleepEvent
end tell
end scheduleTask