mirror of
https://github.com/taigrr/wails.git
synced 2026-04-02 05:08:54 -07:00
Add package for mac functions
This commit is contained in:
51
v2/pkg/mac/mac.go
Normal file
51
v2/pkg/mac/mac.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user