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
/
list_darwin.go
68 lines (58 loc) · 1.76 KB
/
list_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
// +build darwin,!linux,!windows,!js
package dlgs
import (
"fmt"
"os/exec"
"strings"
"syscall"
)
// List displays a list dialog, returning the selected value and a bool for success.
func List(title, text string, items []string) (string, bool, error) {
list := ""
for i, l := range items {
if l == "false" {
return "", false, fmt.Errorf("Cannot use 'false' in items, as it's reserved by osascript's returned value.")
}
list += osaEscapeString(l)
if i != len(items)-1 {
list += ", "
}
}
o, err := osaExecute(`choose from list {` + list + `} with prompt ` + osaEscapeString(text) + ` with title ` + osaEscapeString(title))
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return "", ws.ExitStatus() == 0, nil
}
}
out := strings.TrimSpace(o)
if out == "false" {
return "", false, nil
}
return out, true, err
}
// ListMulti displays a multiple list dialog, returning the selected values and a bool for success.
func ListMulti(title, text string, items []string) ([]string, bool, error) {
list := ""
for i, l := range items {
if l == "false" {
return nil, false, fmt.Errorf("Cannot use 'false' in items, as it's reserved by osascript's returned value.")
}
list += osaEscapeString(l)
if i != len(items)-1 {
list += ", "
}
}
o, err := osaExecute(`choose from list {` + list + `} with multiple selections allowed with prompt ` + osaEscapeString(text) + ` with title ` + osaEscapeString(title))
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return []string{}, ws.ExitStatus() == 0, nil
}
}
out := strings.TrimSpace(o)
if out == "false" {
return nil, false, nil
}
return strings.Split(out, ", "), true, err
}