This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmessage_darwin.go
74 lines (61 loc) · 1.77 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
// +build darwin,!linux,!windows,!js
package dlgs
import (
"os/exec"
"strings"
"syscall"
)
// MessageBox displays message box and ok button without icon.
func MessageBox(title, text string) (bool, error) {
return osaDialog(title, text, "")
}
// 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) {
btn := "Yes"
if defaultCancel {
btn = "No"
}
out, err := osaExecute(`set T to button returned of (display dialog ` + osaEscapeString(text) + ` with title ` + osaEscapeString(title) + ` buttons {"No", "Yes"} default button ` + osaEscapeString(btn) + `)`)
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(out) == "Yes" {
ret = true
}
return ret, err
}
// osaDialog displays dialog.
func osaDialog(title, text, icon string) (bool, error) {
iconScript := ""
if icon != "" {
iconScript = ` with icon ` + icon
}
out, err := osaExecute(`display dialog ` + osaEscapeString(text) + ` with title ` + osaEscapeString(title) + ` buttons {"OK"} default button "OK"` + iconScript)
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(out) == "OK" {
ret = true
}
return ret, err
}