From e2a4ed6d23503ce5888efc2afa513c324a4d8f67 Mon Sep 17 00:00:00 2001 From: Simon Bein Date: Sat, 29 Sep 2018 13:20:03 +0200 Subject: [PATCH] Add do not disturb mode (#6) * Add Method for enabling Do Not Disturb Mode * Add Method for disabling Do Not Disturb Mode --- .circleci/config.yml | 2 +- donotdisturbHandler.go | 75 ++++++++++++++++++++++++++++++++++++++++++ main.go | 13 ++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 donotdisturbHandler.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 1360e49..b05eae2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -22,7 +22,7 @@ jobs: environment: TEST_RES_DIR: /tmp/test-results ARTIFACTS_DIR: /tmp/artifacts - APP_VERSION: v0.2.0 + APP_VERSION: v0.3.0 steps: - checkout - run: diff --git a/donotdisturbHandler.go b/donotdisturbHandler.go new file mode 100644 index 0000000..fdb4f54 --- /dev/null +++ b/donotdisturbHandler.go @@ -0,0 +1,75 @@ +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() error { + + // 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 { + return err + } + + // 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 { + return err + } + + // Restart Notification Center + err = restartNotificationCenter() + + if err != nil { + return err + } + + return nil +} + +// TurnDoNotDisturbOff turns off Do Not Disturb on Mac OS X +func TurnDoNotDisturbOff() error { + + // 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 { + return err + } + + // Restart Notification Center + err = restartNotificationCenter() + + if err != nil { + return err + } + + return nil +} diff --git a/main.go b/main.go index 17520f4..8031fb9 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,19 @@ func main() { os.Exit(0) } + // Handle Notification Center + switch desAction { + case "on": + err = TurnDoNotDisturbOn() + case "off": + err = TurnDoNotDisturbOff() + } + + if err != nil { + fmt.Printf("Could not change Do Not Disturb State: %v\n", err) + } else { + fmt.Println("Successfully changed Do Not Disturb State") + } // Execute action reschan, errchan := make(chan string), make(chan error)