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.

Line  
1
-- Our Mac will idle sleep after fifteen minutes
2
property kUserInactivityBeforeSleepInSeconds : (60 * 15)
3
4
-- BackUp starts at 1am, lasts 150 minutes, every Tuesday and Thursday
5
scheduleTask("Back Up", (60 * 60), (60 * 150), [Tuesday, Thursday])
6
7
-- Nothing to tweak from here on...
8
9
on scheduleTask(inTaskUniqueID, inTaskStartsInSecondsFromMidnight, inTaskDurationInSeconds, inDays)
10
    
11
    -- Schedule events to wake up and stay awake until the task has completed
12
    
13
    -- Schedule the wake event first
14
    tell application "Power Manager Scripting"
15
        
16
        -- Create a record of properties to populate our new event
17
        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}
18
        tell scheduler to add event wakeEvent
19
        
20
        -- Make sure the Mac is awake during the entire task
21
        
22
        -- How many wake events are needed?
23
        set requiredWakes to round ((inTaskDurationInSeconds / kUserInactivityBeforeSleepInSeconds))
24
        set eventTime to inTaskStartsInSecondsFromMidnight
25
        repeat requiredWakes times
26
            
27
            -- Schedule coffee one minute before sleep
28
            set eventTime to eventTime + kUserInactivityBeforeSleepInSeconds - 60
29
            
30
            -- Copy the wakeEvent properties and modify
31
            set coffeeEvent to wakeEvent
32
            set name of coffeeEvent to "Coffee for " & inTaskUniqueID
33
            set unique id of coffeeEvent to inTaskUniqueID & "-coffee-" & (eventTime as string)
34
            set seconds from midnight of trigger of coffeeEvent to eventTime
35
            
36
            tell scheduler to add event coffeeEvent
37
            
38
            -- Stop eventTime from drifting
39
            set eventTime to eventTime + 60
40
            
41
        end repeat
42
        
43
        -- Schedule the sleep event
44
        
45
        -- Copy the wakeEvent properties and modify
46
        set sleepEvent to wakeEvent
47
        set name of sleepEvent to "Sleep after " & inTaskUniqueID
48
        set unique id of sleepEvent to inTaskUniqueID & "-sleep"
49
        set type class of action of sleepEvent to sleep
50
        set seconds from midnight of trigger of sleepEvent to (inTaskStartsInSecondsFromMidnight + inTaskDurationInSeconds)
51
        
52
        tell scheduler to add event sleepEvent
53
        
54
    end tell
55
    
56
end scheduleTask

Open in Script Editor