-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.37 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
package main
import (
_ "embed"
"fmt"
"log"
"os"
"os/exec"
"runtime/debug"
"strings"
"github.com/urfave/cli/v2"
)
//go:embed append_to_latest_unshared_note.scpt
var appleScript string
var Version string = getVersion()
func main() {
app := &cli.App{
Name: "note",
Usage: "Append to latest Apple Notes note",
ArgsUsage: "\"Text to append\"",
Description: "Ignores shared notes. Formats as new line by default.",
HideHelpCommand: true,
Version: Version,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "bulleted",
Aliases: []string{"b"},
Usage: "Format text as bulleted list item",
},
},
Action: func(c *cli.Context) error {
if c.Args().Len() < 1 {
cli.ShowAppHelpAndExit(c, 1)
}
// Get all text input and format it
text := strings.Join(c.Args().Slice(), " ")
if c.Bool("bulleted") {
text = "b " + text
}
// Execute AppleScript
cmd := exec.Command("osascript", "-e", appleScript, text)
_, err := cmd.Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to run AppleScript to write to note: "+err.Error())
return nil
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getVersion() string {
info, _ := debug.ReadBuildInfo()
version := info.Main.Version
if version == "" {
version = "(devel)"
}
return version
}