-
Notifications
You must be signed in to change notification settings - Fork 3
/
wastebasket_darwin.go
45 lines (37 loc) · 1011 Bytes
/
wastebasket_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
//go:build darwin && !ios
package wastebasket
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Trash moves a file or folder including its content into the systems trashbin.
func Trash(paths ...string) error {
for _, path := range paths {
_, err := os.Stat(path)
if os.IsNotExist(err) {
continue
}
if err != nil {
return err
}
//Passing a relative path will lead to the Finder not being able to find the file at all.
path, pathToAbsPathError := filepath.Abs(path)
if pathToAbsPathError != nil {
return pathToAbsPathError
}
path = strings.ReplaceAll(path, `"`, `\"`)
osascriptCommand := fmt.Sprintf(`tell app "Finder" to delete POSIX file "%s"`, path)
err = exec.Command("osascript", "-e", osascriptCommand).Run()
if err != nil {
return err
}
}
return nil
}
// Empty clears the platforms trashbin. It uses the `Finder` app to empty the trashbin.
func Empty() error {
return exec.Command("osascript", "-e", `tell app "Finder" to empty`).Run()
}