diff --git a/README.md b/README.md index 5ed26beb1..37296eff0 100644 --- a/README.md +++ b/README.md @@ -361,14 +361,14 @@ You can control xbar behaviour by modifying the `/Library/Application Support/xb { "autoupdate": true, "terminal": { - "appleScriptTemplate2": "" + "appleScriptTemplate3": "" } } ``` * Changes take effect next time xbar starts * `autoupdate` - (boolean) whether to keep xbar automatically updated or not -* `terminal.appleScriptTemplate2` - (string) the AppleScript to use when **Run in terminal** option is used (use `"false"` to turn this feature off) +* `terminal.appleScriptTemplate3` - (string) the AppleScript to use when **Run in terminal** option is used (use `"false"` to turn this feature off) You can delete this file and restart xbar to reset to defaults. diff --git a/app/app.go b/app/app.go index fe3619560..6ddfb20cb 100644 --- a/app/app.go +++ b/app/app.go @@ -229,6 +229,7 @@ func (app *app) RefreshAll() { } for _, plugin := range app.plugins { // Setup plugin + plugin.AppleScriptTemplate = app.settings.Terminal.AppleScriptTemplate3 plugin.OnCycle = app.onCycle plugin.OnRefresh = app.onRefresh if app.Verbose { @@ -353,7 +354,7 @@ func (app *app) newXbarMenu(plugin *plugins.Plugin, asSubmenu bool) *menu.Menu { }) if plugin != nil { items = append(items, menu.Text("Run in terminal…", keys.CmdOrCtrl("t"), func(_ *menu.CallbackData) { - err := plugin.RunInTerminal(app.settings.Terminal.AppleScriptTemplate2) + err := plugin.RunInTerminal(app.settings.Terminal.AppleScriptTemplate3) if err != nil { _, err2 := app.runtime.Dialog.Message(&dialog.MessageDialog{ Type: dialog.ErrorDialog, diff --git a/app/settings.go b/app/settings.go index 4dc4fdd08..b7dd95fd5 100644 --- a/app/settings.go +++ b/app/settings.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "io/ioutil" + "log" "os" "path/filepath" "sync" @@ -20,24 +21,30 @@ type settings struct { AutoUpdate bool `json:"autoupdate"` Terminal struct { - AppleScriptTemplate2 string `json:"appleScriptTemplate2"` + AppleScriptTemplate3 string `json:"appleScriptTemplate3"` } `json:"terminal"` } func (s *settings) setDefaults() { - if s.Terminal.AppleScriptTemplate2 == "" { - s.Terminal.AppleScriptTemplate2 = ` - activate application "Terminal" - tell application "Terminal" - if not (exists window 1) then reopen - set quotedScriptName to quoted form of "{{ .Command }}" - {{ if .Params }} - set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }} - {{ else }} - set commandLine to {{ .Vars }} & " " & quotedScriptName - {{ end }} - do script commandLine - end tell + if s.Terminal.AppleScriptTemplate3 == "" { + s.Terminal.AppleScriptTemplate3 = ` + set quotedScriptName to quoted form of "{{ .Command }}" + {{ if .Params }} + set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }} + {{ else }} + set commandLine to {{ .Vars }} & " " & quotedScriptName + {{ end }} + if application "Terminal" is running then + tell application "Terminal" + do script commandLine + activate + end tell + else + tell application "Terminal" + do script commandLine in window 1 + activate + end tell + end if ` } } @@ -60,6 +67,7 @@ func loadSettings(path string) (*settings, error) { return nil, errors.Wrap(err, "Unmarshal") } s.setDefaults() + log.Printf("### - settings: %+v\n", s) return s, nil } diff --git a/pkg/plugins/action.go b/pkg/plugins/action.go index 314afdc11..c2c831867 100644 --- a/pkg/plugins/action.go +++ b/pkg/plugins/action.go @@ -41,7 +41,7 @@ func (i *Item) Action() ActionFunc { actions = append(actions, actionHref(debugf, i.Params.Href)) } if i.Params.Shell != "" { - actions = append(actions, actionShell(debugf, i, i.Params.Shell, i.Params.ShellParams, i.Plugin.Variables)) + actions = append(actions, actionShell(debugf, i, i.Plugin.AppleScriptTemplate, i.Params.Shell, i.Params.ShellParams, i.Plugin.Variables)) } if i.Params.Refresh { shouldDelayBeforeRefresh := false @@ -115,24 +115,15 @@ func actionHref(debugf DebugFunc, href string) ActionFunc { } // actionShell gets an ActionFunc that runs a shell command. -func actionShell(debugf DebugFunc, item *Item, command string, params, envVars []string) ActionFunc { +func actionShell(debugf DebugFunc, item *Item, appleScriptTemplate, command string, params, envVars []string) ActionFunc { if item.Params.Terminal { - return actionShellTerminal(debugf, item, command, params, envVars) + return actionShellTerminal(debugf, item, appleScriptTemplate, command, params, envVars) } return func(ctx context.Context) { var commandExec string var commandArgs []string - // if item.Params.Terminal { - // shell := os.Getenv("SHELL") - // if shell == "" { - // shell = "/bin/bash" - // } - // commandExec = shell - // commandArgs = append([]string{command}, params...) - // } else { commandExec = command commandArgs = params - //} debugf("exec: %s %s", commandExec, strings.Join(commandArgs, " ")) cmd := exec.CommandContext(context.Background(), commandExec, commandArgs...) cmd.SysProcAttr = &syscall.SysProcAttr{ @@ -156,22 +147,9 @@ func actionShell(debugf DebugFunc, item *Item, command string, params, envVars [ } // actionShellTerminal runs shell commands where terminal=true. -func actionShellTerminal(debugf DebugFunc, item *Item, command string, params, envVars []string) ActionFunc { +func actionShellTerminal(debugf DebugFunc, item *Item, appleScriptTemplate, command string, params, envVars []string) ActionFunc { return func(ctx context.Context) { debugf("exec: RunInTerminal...") - script := ` - activate application "Terminal" - tell application "Terminal" - if not (exists window 1) then reopen - set quotedScriptName to quoted form of "{{ .Command }}" - {{ if .Params }} - set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }} - {{ else }} - set commandLine to {{ .Vars }} & " " & quotedScriptName - {{ end }} - do script commandLine - end tell - ` command := strconv.Quote(command) command = command[1 : len(command)-1] // trim quotes off for i := range params { @@ -179,7 +157,7 @@ func actionShellTerminal(debugf DebugFunc, item *Item, command string, params, e params[i] = params[i][1 : len(params[i])-1] // trim quotes off } paramsStr := strconv.Quote(strings.Join(params, " ")) - err := item.Plugin.runInTerminal(script, command, paramsStr, envVars) + err := item.Plugin.runInTerminal(appleScriptTemplate, command, paramsStr, envVars) if err != nil { debugf("exec: RunInTerminal: err=%s", err) return diff --git a/pkg/plugins/item_params.go b/pkg/plugins/item_params.go index 6d25d2ec2..c46304e7e 100644 --- a/pkg/plugins/item_params.go +++ b/pkg/plugins/item_params.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/leaanthony/go-ansi-parser" - "github.com/pkg/errors" ) @@ -76,6 +75,9 @@ type ItemParams struct { // Terminal indicates whether to run the shell command in a terminal or not. // Default is false. Terminal bool `json:"terminal"` + // // appleScriptTemplate3 is the template for the AppleScript + // // to run this action in a terminal. + // AppleScriptTemplate string `json:"-"` // Refresh indicates whether clicking this item will cause the plugin // to refresh or not. Refresh bool `json:"refresh"` diff --git a/pkg/plugins/plugin.go b/pkg/plugins/plugin.go index 8e3d1d229..9104cd27f 100644 --- a/pkg/plugins/plugin.go +++ b/pkg/plugins/plugin.go @@ -69,6 +69,10 @@ type Plugin struct { // Called in TriggerRefresh() when updating the plugin menu to the // refreshing state, before refreshSignal is triggered. cycleSignal chan (struct{}) + + // appleScriptTemplate3 is the template for the AppleScript + // to run this action in a terminal. + AppleScriptTemplate string } // CleanFilename gets a clean human readable representation of the @@ -279,8 +283,8 @@ func (p *Plugin) CurrentCycleItem() *Item { return p.Items.CycleItems[p.CycleIndex] } -func (p *Plugin) runInTerminal(appleScriptTemplate2, command, paramsStr string, vars []string) error { - tpl, err := template.New("appleScriptTemplate2").Parse(appleScriptTemplate2) +func (p *Plugin) runInTerminal(appleScriptTemplate3, command, paramsStr string, vars []string) error { + tpl, err := template.New("appleScriptTemplate3").Parse(appleScriptTemplate3) if err != nil { return err } @@ -315,8 +319,8 @@ func (p *Plugin) runInTerminal(appleScriptTemplate2, command, paramsStr string, // RunInTerminal runs this plugin in a terminal using the template // apple script. -func (p *Plugin) RunInTerminal(appleScriptTemplate2 string) error { - return p.runInTerminal(appleScriptTemplate2, p.Command, "", p.Variables) +func (p *Plugin) RunInTerminal(appleScriptTemplate3 string) error { + return p.runInTerminal(appleScriptTemplate3, p.Command, "", p.Variables) } // refresh runs the plugin and parses the output, updating the