diff --git a/v2/pkg/mac/mac.go b/v2/pkg/mac/mac.go new file mode 100644 index 00000000..5d369138 --- /dev/null +++ b/v2/pkg/mac/mac.go @@ -0,0 +1,51 @@ +// build +package mac + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/leaanthony/slicer" + "github.com/pkg/errors" + "github.com/wailsapp/wails/v2/internal/shell" +) + +func StartAtLogin(enabled bool) error { + exe, err := os.Executable() + if err != nil { + return errors.Wrap(err, "Error running os.Executable:") + } + binName := filepath.Base(exe) + if !strings.HasSuffix(exe, "/Contents/MacOS/"+binName) { + return fmt.Errorf("app needs to be running as package.app file to start at startup") + } + appPath := strings.TrimSuffix(exe, "/Contents/MacOS/"+binName) + var command string + if enabled { + command = fmt.Sprintf("tell application \"System Events\" to make login item at end with properties {name: \"%s\",path:\"%s\", hidden:false}", binName, appPath) + } else { + command = fmt.Sprintf("tell application \"System Events\" to delete login item \"%s\"", binName) + } + _, stde, err := shell.RunCommand("/tmp", "osascript", "-e", command) + if err != nil { + errors.Wrap(err, stde) + } + return nil +} + +func StartsAtLogin() (bool, error) { + exe, err := os.Executable() + if err != nil { + return false, err + } + binName := filepath.Base(exe) + results, stde, err := shell.RunCommand("/tmp", "osascript", "-e", `tell application "System Events" to get the name of every login item`) + if err != nil { + return false, errors.Wrap(err, stde) + } + results = strings.TrimSpace(results) + startupApps := slicer.String(strings.Split(results, ", ")) + return startupApps.Contains(binName), nil +}