-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbefore_sleep_v3.0.applescript
86 lines (60 loc) · 3.08 KB
/
before_sleep_v3.0.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- The path of the Project.
set project_path to "/Users/" & (do shell script "whoami") & "/Projects/mySafeSleep/"
-- LOG, prepare new entries.
do shell script "echo \"############### BEFORE SLEEP #################\" >> " & project_path & "my_safe_sleep.log"
-- First, delete all scheduled wakes.
do shell script "sudo " & project_path & "pmset_10.12 schedule cancelall"
-- Getting current hibernatemode.
set tmp_var to do shell script "pmset_10.12 -g | grep hibernatemode"
tell tmp_var to set hibernatemode to second word of paragraph 1
-- LOG
do shell script "echo \"$(date) -- Found hibernatemode on " & hibernatemode & ".\" >> " & project_path & "my_safe_sleep.log"
-- Check for _hibernate, if true, then hibernate.
set _hibernate to do shell script "cat " & project_path & "config_files/_hibernate.conf"
if _hibernate is "true" then
-- Reset _hibernate.
do shell script "echo false > " & project_path & "config_files/_hibernate.conf"
if hibernatemode is not equal to "29" then
do shell script "sudo " & project_path & "pmset_10.10 -a hibernatemode 29"
-- LOG
do shell script "echo \"$(date) -- Changed hibernatemode to 29, because of _hibernate.\" >> " & project_path & "my_safe_sleep.log"
end if
-- LOG
do shell script "echo \"$(date) -- Going to hibernate.\" >> " & project_path & "my_safe_sleep.log"
-- Exit
return 0
end if
-- Getting current charge.
set Cap to do shell script "pmset_10.12 -g batt"
tell Cap to set chargeLeft to third word of paragraph 2
-- LOG
do shell script "echo \"$(date) -- Current charge is: " & chargeLeft & ".\" >> " & project_path & "my_safe_sleep.log"
-- If the battery is too low, I'll go directly to hibernate, instead of sleeping.
if chargeLeft - 0 < 8 then
-- LOG
do shell script "echo \"$(date) -- Charge is < 8.\" >> " & project_path & "my_safe_sleep.log"
if hibernatemode is not equal to "29" then
do shell script "sudo " & project_path & "pmset_10.10 -a hibernatemode 29"
-- LOG
do shell script "echo \"$(date) -- Changed hibernatemode to 29, battery too low.\" >> " & project_path & "my_safe_sleep.log"
end if
-- LOG
do shell script "echo \"$(date) -- Going to hibernate.\" >> " & project_path & "my_safe_sleep.log"
else
-- LOG
do shell script "echo \"$(date) -- Charge is >= 8.\" >> " & project_path & "my_safe_sleep.log"
if hibernatemode is not equal to "0" then
do shell script "sudo " & project_path & "pmset_10.12 -a hibernatemode 0"
-- LOG
do shell script "echo \"$(date) -- Changed hibernatemode to 0.\" >> " & project_path & "my_safe_sleep.log"
end if
-- Scheduling next wake.
set nextWakeTime to do shell script "date -v +3H \"+%m/%d/%Y% %H:%M:%S\""
do shell script "sudo " & project_path & "pmset_10.12 schedule wake \"" & nextWakeTime & "\""
-- LOG
do shell script "echo \"$(date) -- Scheduled wake: " & nextWakeTime & ".\" >> " & project_path & "my_safe_sleep.log"
-- Next time, just hibernate.
do shell script "echo true > " & project_path & "config_files/_hibernate.conf"
-- LOG
do shell script "echo \"$(date) -- Changed _hibernate to true, going to sleep.\" >> " & project_path & "my_safe_sleep.log"
end if