//go:build darwin && appstore
|
|
package startup
|
|
/*
|
#cgo CFLAGS: -x objective-c
|
#cgo LDFLAGS: -framework Foundation -framework ServiceManagement
|
#import <Foundation/Foundation.h>
|
#import <ServiceManagement/ServiceManagement.h>
|
#include <stdlib.h>
|
#include <string.h>
|
|
static int appStoreLoginItemStatus(void) {
|
if (@available(macOS 13.0, *)) {
|
SMAppServiceStatus status = [SMAppService mainAppService].status;
|
return status == SMAppServiceStatusEnabled || status == SMAppServiceStatusRequiresApproval;
|
}
|
return 0;
|
}
|
|
static char* appStoreSetLoginItemEnabled(int enabled) {
|
if (@available(macOS 13.0, *)) {
|
NSError* error = nil;
|
BOOL ok = enabled
|
? [[SMAppService mainAppService] registerAndReturnError:&error]
|
: [[SMAppService mainAppService] unregisterAndReturnError:&error];
|
if (ok) return NULL;
|
if (error && error.localizedDescription) {
|
return strdup(error.localizedDescription.UTF8String);
|
}
|
return strdup("SMAppService login item update failed");
|
}
|
return strdup("SMAppService requires macOS 13 or later");
|
}
|
*/
|
import "C"
|
|
import (
|
"fmt"
|
"unsafe"
|
)
|
|
func isEnabled() bool {
|
return C.appStoreLoginItemStatus() != 0
|
}
|
|
func setEnabled(enable bool) error {
|
errMsg := C.appStoreSetLoginItemEnabled(boolToCInt(enable))
|
if errMsg == nil {
|
return nil
|
}
|
defer C.free(unsafe.Pointer(errMsg))
|
return fmt.Errorf("failed to update login item: %s", C.GoString(errMsg))
|
}
|
|
func boolToCInt(v bool) C.int {
|
if v {
|
return 1
|
}
|
return 0
|
}
|