-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfile_darwin.go
42 lines (35 loc) · 1.14 KB
/
file_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
package image_picker
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
func fileDialog(title string, fileType string) (string, error) {
osascript, err := exec.LookPath("osascript")
if err != nil {
return "", err
}
var filters string
switch fileType {
case "image":
filters = `"PNG", "public.png", "JPEG", "jpg", "public.jpeg"`
case "video":
filters = `"MOV","mov","MP4","mp4","AVI","avi","MKV","mkv"`
default:
return "", errors.New("unsupported fileType")
}
output, err := exec.Command(osascript, "-e", `choose file of type {`+filters+`} with prompt "`+title+`"`).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
fmt.Printf("go-flutter/plugins/image_picker: file dialog exited with code %d and output `%s`\n", exitError.ExitCode(), string(output))
return "", nil // user probably canceled or closed the selection window
}
return "", errors.Wrap(err, "failed to open file dialog")
}
trimmedOutput := strings.TrimSpace(string(output))
pathParts := strings.Split(trimmedOutput, ":")
path := string(filepath.Separator) + filepath.Join(pathParts[1:]...)
return path, nil
}