zenity/dialog/dialog_darwin.go

145 lines
2.9 KiB
Go
Raw Normal View History

2019-12-11 05:29:31 -05:00
package dialog
import (
"bytes"
"html/template"
"io"
"os/exec"
"strings"
)
func OpenFile(title, defaultPath string, filters []FileFilter) (string, error) {
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
Operation: "chooseFile",
Title: title,
DefaultPath: defaultPath,
2019-12-11 06:29:32 -05:00
Filter: appleFilters(filters),
2019-12-11 05:29:31 -05:00
})
out, err := cmd.Output()
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
func OpenFiles(title, defaultPath string, filters []FileFilter) ([]string, error) {
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
Operation: "chooseFile",
Multiple: true,
Title: title,
DefaultPath: defaultPath,
2019-12-11 06:29:32 -05:00
Filter: appleFilters(filters),
2019-12-11 05:29:31 -05:00
})
out, err := cmd.Output()
if err != nil {
return nil, err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
2019-12-11 19:22:28 -05:00
if len(out) == 0 {
return nil, nil
}
2019-12-11 05:29:31 -05:00
return strings.Split(string(out), "\x00"), nil
}
2019-12-11 06:29:32 -05:00
func SaveFile(title, defaultPath string, confirmOverwrite bool, filters []FileFilter) (string, error) {
2019-12-11 05:29:31 -05:00
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
Operation: "chooseFileName",
Title: title,
DefaultPath: defaultPath,
})
out, err := cmd.Output()
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
func PickFolder(title, defaultPath string) (string, error) {
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
Operation: "chooseFolder",
Title: title,
DefaultPath: defaultPath,
})
out, err := cmd.Output()
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
2019-12-11 06:29:32 -05:00
func appleFilters(filters []FileFilter) []string {
2019-12-11 05:29:31 -05:00
var filter []string
for _, f := range filters {
for _, e := range f.Exts {
filter = append(filter, strings.TrimPrefix(e, "."))
}
}
return filter
}
type scriptData struct {
Operation string
Title string
DefaultPath string
Filter []string
Multiple bool
}
func scriptExpand(data scriptData) io.Reader {
var buf bytes.Buffer
err := script.Execute(&buf, data)
if err != nil {
panic(err)
}
var slice = buf.Bytes()
return bytes.NewReader(slice[len("<script>") : len(slice)-len("</script>")])
}
var script = template.Must(template.New("").Parse(`<script>
var app = Application.currentApplication();
app.includeStandardAdditions = true;
2019-12-11 19:22:28 -05:00
app.activate();
2019-12-11 05:29:31 -05:00
var opts = {};
opts.withPrompt = {{.Title}};
opts.multipleSelectionsAllowed = {{.Multiple}};
{{if .DefaultPath}}
2019-12-11 19:22:28 -05:00
opts.defaultLocation = {{.DefaultPath}};
2019-12-11 05:29:31 -05:00
{{end}}
{{if .Filter}}
2019-12-11 19:22:28 -05:00
opts.ofType = {{.Filter}};
2019-12-11 05:29:31 -05:00
{{end}}
2019-12-11 19:22:28 -05:00
var ret;
try {
ret = app[{{.Operation}}](opts);
} catch (e) {
if (e.errorNumber !== -128) throw e;
}
2019-12-11 05:29:31 -05:00
if (Array.isArray(ret)) {
ret.join('\0');
2019-12-11 19:22:28 -05:00
} else if (ret != null) {
2019-12-11 05:29:31 -05:00
ret.toString();
2019-12-11 19:22:28 -05:00
} else {
void 0;
2019-12-11 05:29:31 -05:00
}
</script>`))