Ariver
2026-06-29 9d4f977026bbb4516f8cc97f715c0dc28bf2a13f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//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
}