-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (61 loc) · 1.54 KB
/
main.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
package main
import (
"log"
"os"
"os/exec"
"strings"
"github.com/andybrewer/mack"
)
func touchFile(path string, filename string) error {
cmd := exec.Command("touch", filename)
cmd.Dir = path
return cmd.Run()
}
func main() {
path, err := mack.Tell("Finder",
"if (count of Finder windows) is 0 then",
"set dir to (desktop as alias)",
"else",
"set dir to ((target of Finder window 1) as alias)",
"end if",
"return POSIX path of dir")
if err != nil {
panic(err)
}
log.Printf("Topmost finder path: %s\n", path)
response, err := mack.Dialog("New Filename", "Insert Filename", "newFile.txt")
if err != nil {
panic(err)
}
if response.Clicked == "Cancel" {
// handle the Cancel event
os.Exit(2)
} else {
dialogResult := strings.Split(response.Text, "Legacy")
log.Printf("Dialog Result: %s", dialogResult)
log.Printf("Dialog Result Length: %d", len(dialogResult))
newFilename := ""
switch len(dialogResult) {
case 0:
log.Printf("No filename provided")
os.Exit(2)
case 1:
log.Printf("Filename provided: %s", dialogResult[0])
newFilename = strings.TrimSpace(dialogResult[0])
case 2:
log.Printf("Filename provided: %s", dialogResult[1])
newFilename = strings.TrimSpace(dialogResult[1])
case 3:
log.Printf("Filename provided: %s", dialogResult[2])
newFilename = strings.TrimSpace(dialogResult[2])
default:
log.Printf("Unknown count for dialog result")
os.Exit(2)
}
err := touchFile(path, newFilename)
if err != nil {
panic(err)
}
log.Printf("File Created: %s", newFilename)
}
}