package zenity
import (
"bytes"
"html/template"
"io"
"os/exec"
"strings"
)
func SelectFile(title, defaultPath string, filters []FileFilter) (string, error) {
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{
Operation: "chooseFile",
Title: title,
DefaultPath: defaultPath,
Filter: appleFilters(filters),
})
out, err := cmd.Output()
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
func SelectFileMutiple(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,
Filter: appleFilters(filters),
})
out, err := cmd.Output()
if err != nil {
return nil, err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
if len(out) == 0 {
return nil, nil
}
return strings.Split(string(out), "\x00"), nil
}
func SelectFileSave(title, defaultPath string, confirmOverwrite bool, filters []FileFilter) (string, error) {
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 SelectDirectory(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
}
func appleFilters(filters []FileFilter) []string {
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("")])
}
var script = template.Must(template.New("").Parse(``))