zenity/internal/zenutil/run_darwin.go

91 lines
2.1 KiB
Go
Raw Normal View History

2020-01-19 06:57:05 -05:00
package zenutil
2020-01-05 07:37:45 -05:00
import (
2020-01-28 07:46:43 -05:00
"context"
2020-01-06 19:57:00 -05:00
"os"
2020-01-05 07:37:45 -05:00
"os/exec"
"strings"
2020-01-06 19:57:00 -05:00
"syscall"
2020-01-05 07:37:45 -05:00
)
2020-01-29 11:45:40 -05:00
// Run is internal.
2020-01-28 07:46:43 -05:00
func Run(ctx context.Context, script string, data interface{}) ([]byte, error) {
2020-01-05 07:37:45 -05:00
var buf strings.Builder
err := scripts.ExecuteTemplate(&buf, script, data)
if err != nil {
return nil, err
}
2020-01-21 07:03:58 -05:00
script = buf.String()
2020-01-19 06:57:05 -05:00
if Command {
2020-01-09 06:17:39 -05:00
path, err := exec.LookPath("osascript")
2020-01-06 19:57:00 -05:00
if err == nil {
os.Stderr.Close()
2021-02-18 21:20:57 -05:00
syscall.Exec(path, []string{"osascript", "-l", "JavaScript", "-e", script}, nil)
2020-01-06 19:57:00 -05:00
}
}
2020-01-28 07:46:43 -05:00
if ctx != nil {
2021-02-18 21:20:57 -05:00
cmd := exec.CommandContext(ctx, "osascript", "-l", "JavaScript")
2020-01-28 07:46:43 -05:00
cmd.Stdin = strings.NewReader(script)
2020-01-29 06:09:06 -05:00
out, err := cmd.Output()
if ctx.Err() != nil {
err = ctx.Err()
}
return out, err
2020-01-28 07:46:43 -05:00
}
2021-02-18 21:20:57 -05:00
cmd := exec.Command("osascript", "-l", "JavaScript")
2020-01-21 07:03:58 -05:00
cmd.Stdin = strings.NewReader(script)
2020-01-05 07:37:45 -05:00
return cmd.Output()
}
2021-02-19 12:46:47 -05:00
// File is internal.
2020-01-05 07:37:45 -05:00
type File struct {
2021-02-19 12:46:47 -05:00
Operation string
Separator string
Options FileOptions
}
// FileOptions is internal.
type FileOptions struct {
Prompt string `json:"withPrompt,omitempty"`
Type []string `json:"ofType,omitempty"`
Name string `json:"defaultName,omitempty"`
Location string `json:"defaultLocation,omitempty"`
Multiple bool `json:"multipleSelectionsAllowed,omitempty"`
Invisibles bool `json:"invisibles,omitempty"`
2020-01-05 07:37:45 -05:00
}
2021-02-19 12:46:47 -05:00
// Msg is internal.
2020-01-05 07:37:45 -05:00
type Msg struct {
2020-01-06 07:01:51 -05:00
Operation string
Text string
2020-01-06 19:57:00 -05:00
Extra string
2021-02-19 12:46:47 -05:00
Options MsgOptions
2020-01-05 07:37:45 -05:00
}
2020-01-26 11:04:49 -05:00
2021-02-19 12:46:47 -05:00
// MsgOptions is internal.
type MsgOptions struct {
Message string `json:"message,omitempty"`
As string `json:"as,omitempty"`
Title string `json:"withTitle,omitempty"`
Icon string `json:"withIcon,omitempty"`
Buttons []string `json:"buttons,omitempty"`
Cancel int `json:"cancelButton,omitempty"`
Default int `json:"defaultButton,omitempty"`
Timeout int `json:"givingUpAfter,omitempty"`
}
// Notify is internal.
2020-01-26 11:04:49 -05:00
type Notify struct {
2021-02-19 12:46:47 -05:00
Text string
Options NotifyOptions
}
// NotifyOptions is internal.
type NotifyOptions struct {
Title string `json:"withTitle,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
2020-01-26 11:04:49 -05:00
}