Skip to content

Commit

Permalink
Add do not disturb mode (#6)
Browse files Browse the repository at this point in the history
* Add Method for enabling Do Not Disturb Mode

* Add Method for disabling Do Not Disturb Mode
  • Loading branch information
SimonTheLeg committed Sep 29, 2018
1 parent 8e4bb5b commit e2a4ed6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -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:
Expand Down
75 changes: 75 additions & 0 deletions 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
}
13 changes: 13 additions & 0 deletions main.go
Expand Up @@ -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)

Expand Down

0 comments on commit e2a4ed6

Please sign in to comment.