zenity/file_darwin.go

153 lines
3 KiB
Go
Raw Normal View History

2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 05:29:31 -05:00
import (
"bytes"
"html/template"
"io"
"os/exec"
"strings"
)
2020-01-02 20:44:23 -05:00
func SelectFile(options ...FileOption) (string, error) {
opts := fileoptsParse(options)
2019-12-11 05:29:31 -05:00
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
2020-01-02 20:44:23 -05:00
Operation: "chooseFile",
Prompt: opts.title,
Location: opts.filename,
Type: appleFilters(opts.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
}
2020-01-02 20:44:23 -05:00
func SelectFileMutiple(options ...FileOption) ([]string, error) {
opts := fileoptsParse(options)
2019-12-11 05:29:31 -05:00
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
2020-01-02 20:44:23 -05:00
Operation: "chooseFile",
Multiple: true,
Prompt: opts.title,
Location: opts.filename,
Type: appleFilters(opts.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
}
2020-01-02 20:44:23 -05:00
func SelectFileSave(options ...FileOption) (string, error) {
opts := fileoptsParse(options)
2019-12-11 05:29:31 -05:00
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
2020-01-02 20:44:23 -05:00
Operation: "chooseFileName",
Prompt: opts.title,
Location: opts.filename,
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
}
2020-01-02 20:44:23 -05:00
func SelectDirectory(options ...FileOption) (string, error) {
opts := fileoptsParse(options)
2019-12-11 05:29:31 -05:00
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
2020-01-02 20:44:23 -05:00
Operation: "chooseFolder",
Prompt: opts.title,
Location: opts.filename,
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
}
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 {
2020-01-02 20:44:23 -05:00
Operation string
Prompt string
Location string
Type []string
Multiple bool
2019-12-11 05:29:31 -05:00
}
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 = {};
2020-01-02 20:44:23 -05:00
opts.withPrompt = {{.Prompt}};
2019-12-11 05:29:31 -05:00
opts.multipleSelectionsAllowed = {{.Multiple}};
2020-01-02 20:44:23 -05:00
{{if .Location}}
opts.defaultLocation = {{.Location}};
2019-12-11 05:29:31 -05:00
{{end}}
2020-01-02 20:44:23 -05:00
{{if .Type}}
opts.ofType = {{.Type}};
2019-12-11 05:29:31 -05:00
{{end}}
2019-12-13 21:16:06 -05:00
var res;
2019-12-11 19:22:28 -05:00
try {
2019-12-13 21:16:06 -05:00
res = app[{{.Operation}}](opts);
2019-12-11 19:22:28 -05:00
} catch (e) {
if (e.errorNumber !== -128) throw e;
}
2019-12-13 21:16:06 -05:00
if (Array.isArray(res)) {
res.join('\0');
} else if (res != null) {
res.toString();
2019-12-11 19:22:28 -05:00
} else {
void 0;
2019-12-11 05:29:31 -05:00
}
</script>`))