forked from gen2brain/dlgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_darwin.go
75 lines (61 loc) · 1.7 KB
/
message_darwin.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
// +build darwin,!linux,!windows,!js
package dlgs
import (
"os/exec"
"strings"
"syscall"
)
// Info displays information dialog.
func Info(title, text string) (bool, error) {
return osaDialog(title, text, "note")
}
// Warning displays warning dialog.
func Warning(title, text string) (bool, error) {
return osaDialog(title, text, "caution")
}
// Error displays error dialog.
func Error(title, text string) (bool, error) {
return osaDialog(title, text, "stop")
}
// Question displays question dialog.
func Question(title, text string, defaultCancel bool) (bool, error) {
osa, err := exec.LookPath("osascript")
if err != nil {
return false, err
}
btn := "Yes"
if defaultCancel {
btn = "No"
}
out, err := exec.Command(osa, "-e", `set T to button returned of (display dialog "`+text+`" with title "`+title+`" buttons {"No", "Yes"} default button "`+btn+`")`).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return ws.ExitStatus() == 0, nil
}
}
ret := false
if strings.TrimSpace(string(out)) == "Yes" {
ret = true
}
return ret, err
}
// osaDialog displays dialog.
func osaDialog(title, text, icon string) (bool, error) {
osa, err := exec.LookPath("osascript")
if err != nil {
return false, err
}
out, err := exec.Command(osa, "-e", `set T to button returned of (display dialog "`+text+`" with title "`+title+`" buttons {"OK"} default button "OK" with icon `+icon+`)`).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return ws.ExitStatus() == 0, nil
}
}
ret := false
if strings.TrimSpace(string(out)) == "OK" {
ret = true
}
return ret, err
}