forked from ncruces/zenity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_darwin.go
114 lines (101 loc) · 2.79 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package zenity
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/jeesk/zenity/internal/zenutil"
)
func selectFile(opts options) (name string, err error) {
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, _, err = splitDirAndName(opts.filename)
if data.Options.Location != "" && err == nil {
data.Options.Location, err = filepath.Abs(data.Options.Location)
}
if err != nil {
return "", err
}
handleAttach(&opts, &data)
if i, ok := opts.windowIcon.(string); ok {
data.WindowIcon = i
}
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFile"
data.Options.Type = opts.fileFilters.types()
}
out, err := zenutil.Run(opts.ctx, "file", data)
return strResult(opts, out, err)
}
func handleAttach(opts *options, data *zenutil.File) {
if opts.dontAttachDarwinWindow {
return
}
if opts.attach != nil {
data.Application = opts.attach
} else {
cmd := exec.Command("osascript", "-e", `tell application "System Events"
set frontAppName to name of first application process whose frontmost is true
end tell
return frontAppName`)
output, err := cmd.Output()
if err == nil {
processID := strings.TrimSpace(string(output))
data.Application = processID
} else {
fmt.Println("macos Failed to get process ID:", err)
}
}
}
func selectFileMultiple(opts options) (list []string, err error) {
var data zenutil.File
data.Separator = zenutil.Separator
data.Options.Multiple = true
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, _, err = splitDirAndName(opts.filename)
if data.Options.Location != "" && err == nil {
data.Options.Location, err = filepath.Abs(data.Options.Location)
}
if err != nil {
return nil, err
}
handleAttach(&opts, &data)
if i, ok := opts.windowIcon.(string); ok {
data.WindowIcon = i
}
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFile"
data.Options.Type = opts.fileFilters.types()
}
out, err := zenutil.Run(opts.ctx, "file", data)
return lstResult(opts, out, err)
}
func selectFileSave(opts options) (name string, err error) {
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, data.Options.Name, err = splitDirAndName(opts.filename)
if data.Options.Location != "" && err == nil {
data.Options.Location, err = filepath.Abs(data.Options.Location)
}
if err != nil {
return "", err
}
handleAttach(&opts, &data)
if i, ok := opts.windowIcon.(string); ok {
data.WindowIcon = i
}
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFileName"
}
out, err := zenutil.Run(opts.ctx, "file", data)
return strResult(opts, out, err)
}