-
Notifications
You must be signed in to change notification settings - Fork 0
/
donotdisturbHandler.go
84 lines (63 loc) · 2.13 KB
/
donotdisturbHandler.go
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
package main
import (
"fmt"
"os/exec"
"time"
)
func restartNotificationCenter() error {
cmd := exec.Command("bash", "-c", "killall NotificationCenter")
_, err := cmd.Output()
if err != nil {
return err
}
return nil
}
// TurnDoNotDisturbOn turns on Do Not Disturb on Mac OS X
func TurnDoNotDisturbOn(reschan chan string, errchan chan error) func() {
return func() {
// Enable Do Not Disturb Mode
cmd := exec.Command("bash", "-c", "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true")
_, err := cmd.CombinedOutput()
if err != nil {
errchan <- fmt.Errorf("Could not turn Do Not Disturb on: %v", err)
return
}
// Set Time forDo Not Disturb Mode
t := time.Now()
tfmt := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d +0000", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
cmd = exec.Command("bash", "-c", fmt.Sprintf("defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date '%s'", tfmt))
_, err = cmd.CombinedOutput()
if err != nil {
errchan <- fmt.Errorf("Could not set time for turning Do Not Disturb on: %v", err)
return
}
// Restart Notification Center
err = restartNotificationCenter()
if err != nil {
errchan <- fmt.Errorf("Could not restart Notification Center: %v", err)
return
}
reschan <- "Successfully turned Do Not Disturb On"
return
}
}
// TurnDoNotDisturbOff turns off Do Not Disturb on Mac OS X
func TurnDoNotDisturbOff(reschan chan string, errchan chan error) func() {
return func() {
// Disable Do Not Disturb Mode
cmd := exec.Command("bash", "-c", "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false")
_, err := cmd.CombinedOutput()
if err != nil {
errchan <- fmt.Errorf("Could not turn Do Not Disturb off: %v", err)
return
}
// Restart Notification Center
err = restartNotificationCenter()
if err != nil {
errchan <- fmt.Errorf("Could not restart Notification Center: %v", err)
return
}
reschan <- "Successfully turned Do Not Disturb Off"
return
}
}