From ec8eb571ebb177bdfad712d036e948861efd5e03 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 7 Jan 2020 00:57:00 +0000 Subject: [PATCH] WIP: zenity command (macos). --- .gitignore | 2 -- README.md | 2 +- cmd/zenity/.gitignore | 1 + cmd/zenity/main.go | 24 ++++++++++++++++++++++++ internal/cmd/cmd.go | 3 +++ internal/osa/.gitignore | 1 + internal/osa/osa_darwin.go | 14 ++++++++++++++ internal/osa/scripts/msg.gots | 3 ++- msg_darwin.go | 1 + 9 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 cmd/zenity/.gitignore create mode 100644 cmd/zenity/main.go create mode 100644 internal/cmd/cmd.go create mode 100644 internal/osa/.gitignore diff --git a/.gitignore b/.gitignore index 8311b1d..66fd13c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,3 @@ # Dependency directories (remove the comment below to include it) # vendor/ - -osa_gen_darwin.go \ No newline at end of file diff --git a/README.md b/README.md index d1da7b6..2dd2ad7 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Zenity for Golang \ No newline at end of file +# Zenity dialogs for Golang, Windows and macOS \ No newline at end of file diff --git a/cmd/zenity/.gitignore b/cmd/zenity/.gitignore new file mode 100644 index 0000000..99f8ec0 --- /dev/null +++ b/cmd/zenity/.gitignore @@ -0,0 +1 @@ +zenity diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go new file mode 100644 index 0000000..02574a8 --- /dev/null +++ b/cmd/zenity/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "os" + + "github.com/ncruces/zenity" + "github.com/ncruces/zenity/internal/cmd" +) + +func main() { + cmd.Command = true + + file, err := zenity.SelectFile() + if err != nil { + os.Stderr.WriteString(err.Error()) + os.Stderr.WriteString("\n") + os.Exit(255) + } + if file == "" { + os.Exit(1) + } + os.Stdout.WriteString(file) + os.Stdout.WriteString("\n") +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go new file mode 100644 index 0000000..5d580ed --- /dev/null +++ b/internal/cmd/cmd.go @@ -0,0 +1,3 @@ +package cmd + +var Command bool diff --git a/internal/osa/.gitignore b/internal/osa/.gitignore new file mode 100644 index 0000000..ebe7564 --- /dev/null +++ b/internal/osa/.gitignore @@ -0,0 +1 @@ +osa_gen_darwin.go diff --git a/internal/osa/osa_darwin.go b/internal/osa/osa_darwin.go index e50def8..4938611 100644 --- a/internal/osa/osa_darwin.go +++ b/internal/osa/osa_darwin.go @@ -1,8 +1,12 @@ package osa import ( + "os" "os/exec" "strings" + "syscall" + + "github.com/ncruces/zenity/internal/cmd" ) //go:generate go run scripts/generate.go scripts/ @@ -17,6 +21,15 @@ func Run(script string, data interface{}) ([]byte, error) { res := buf.String() res = res[len("")] + + if cmd.Command { + cmd, err := exec.LookPath("osascript") + if err == nil { + os.Stderr.Close() + syscall.Exec(cmd, []string{"osascript", "-l", "JavaScript", "-e", res}, nil) + } + } + cmd := exec.Command("osascript", "-l", "JavaScript") cmd.Stdin = strings.NewReader(res) return cmd.Output() @@ -39,6 +52,7 @@ type Msg struct { As string Title string Icon string + Extra string Buttons []string Cancel int Default int diff --git a/internal/osa/scripts/msg.gots b/internal/osa/scripts/msg.gots index 6ccdb6e..f905217 100644 --- a/internal/osa/scripts/msg.gots +++ b/internal/osa/scripts/msg.gots @@ -26,4 +26,5 @@ var opts = {} opts.cancelButton = {{.Cancel}} {{end -}} -app[{{.Operation}}]({{.Text}}, opts).buttonReturned \ No newline at end of file +var res = app[{{.Operation}}]({{.Text}}, opts).buttonReturned +res === {{.Extra}} ? res : void 0 \ No newline at end of file diff --git a/msg_darwin.go b/msg_darwin.go index 109ada7..4cc33d0 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -96,6 +96,7 @@ func message(typ int, text string, options []Option) (bool, error) { data.Default = 2 } } + data.Extra = opts.extra } if opts.defcancel { if data.Cancel != 0 {