github.com/secoba/wails/v2@v2.6.4/pkg/mac/login_darwin.go (about) 1 // Package mac provides MacOS related utility functions for Wails applications 2 package mac 3 4 import ( 5 "fmt" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/leaanthony/slicer" 11 "github.com/pkg/errors" 12 "github.com/secoba/wails/v2/internal/shell" 13 ) 14 15 // StartAtLogin will either add or remove this application to/from the login 16 // items, depending on the given boolean flag. The limitation is that the 17 // currently running app must be in an app bundle. 18 func StartAtLogin(enabled bool) error { 19 exe, err := os.Executable() 20 if err != nil { 21 return errors.Wrap(err, "Error running os.Executable:") 22 } 23 binName := filepath.Base(exe) 24 if !strings.HasSuffix(exe, "/Contents/MacOS/"+binName) { 25 return fmt.Errorf("app needs to be running as package.app file to start at login") 26 } 27 appPath := strings.TrimSuffix(exe, "/Contents/MacOS/"+binName) 28 var command string 29 if enabled { 30 command = fmt.Sprintf("tell application \"System Events\" to make login item at end with properties {name: \"%s\",path:\"%s\", hidden:false}", binName, appPath) 31 } else { 32 command = fmt.Sprintf("tell application \"System Events\" to delete login item \"%s\"", binName) 33 } 34 _, stde, err := shell.RunCommand("/tmp", "osascript", "-e", command) 35 if err != nil { 36 return errors.Wrap(err, stde) 37 } 38 return nil 39 } 40 41 // StartsAtLogin will indicate if this application is in the login 42 // items. The limitation is that the currently running app must be 43 // in an app bundle. 44 func StartsAtLogin() (bool, error) { 45 exe, err := os.Executable() 46 if err != nil { 47 return false, err 48 } 49 binName := filepath.Base(exe) 50 if !strings.HasSuffix(exe, "/Contents/MacOS/"+binName) { 51 return false, fmt.Errorf("app needs to be running as package.app file to start at login") 52 } 53 results, stde, err := shell.RunCommand("/tmp", "osascript", "-e", `tell application "System Events" to get the name of every login item`) 54 if err != nil { 55 return false, errors.Wrap(err, stde) 56 } 57 results = strings.TrimSpace(results) 58 startupApps := slicer.String(strings.Split(results, ", ")) 59 return startupApps.Contains(binName), nil 60 }