autostart: Open at login is listed as magpie in Login Items, not under the developer's name on its signature (#1512, ysicing)

System Settings > General > Login Items ("Allow in the Background")
listed magpie's launch agent as "Xipeng Guan" with a blank icon: a plain
LaunchAgent is attributed to the name on its program's signature unless
the plist names the app it belongs to. The launch agent now carries
AssociatedBundleIdentifiers with the bundle id read from the app's own
Info.plist (com.yetone.magpie for a release), Apple's supported way for
an app's legacy launch agent (macOS 13+; older systems ignore the key).
A program outside an .app names none. Refresh already rewrites an older
magpie's plist at start, so users who turned it on before get the key
without toggling; an Info.plist that fails to read never drops a key
written before.

SMAppService was not taken: it needs a plist inside the signed bundle and
a cgo/ObjC bridge, and changes how Enabled is read, a bigger move for a
display-name fix.

Run: TestLaunchAgentNamesItsApp fails on the old code (no
AssociatedBundleIdentifiers) and with the unread-Info.plist guard
removed; passes now, plutil -lint and plutil -extract read the key.
A plist written for /Applications/magpie.app into a sandbox HOME lints
OK and names com.yetone.magpie. go test -tags nogui ./internal/autostart,
gofmt -l, go vet, GOOS=linux/windows go vet -tags nogui pass.
Not tried: the Login Items pane itself with a real login item
(~/Library/LaunchAgents is off-limits here); the reporter confirms.
This commit is contained in:
yetone
2026-10-11 01:20:18 +08:00
parent 7af45761f7
commit 40e9df4e70
2 changed files with 113 additions and 4 deletions
+33 -4
View File
@@ -31,7 +31,10 @@ func enable(exe string) error {
return os.WriteFile(p, plist(exe), 0o644)
}
// plist is the launch agent for exe. AbandonProcessGroup: launchd kills
// plist is the launch agent for exe. AssociatedBundleIdentifiers names the
// app the program belongs to: without it, System Settings' Login Items
// lists a launch agent under the name on its signature, the developer's
// own (#1512), rather than as magpie with its icon. AbandonProcessGroup: launchd kills
// whatever a job started once the job exits, and a restart to update is
// the job quitting with the shell that opens the new version still
// waiting for it to go — without the key, a magpie opened at login never
@@ -48,9 +51,31 @@ func plist(exe string) []byte {
<key>LimitLoadToSessionType</key><string>Aqua</string>
<key>ProcessType</key><string>Interactive</string>
<key>AbandonProcessGroup</key><true/>
</dict>
%s</dict>
</plist>
`, label, html.EscapeString(exe), Arg))
`, label, html.EscapeString(exe), Arg, associated(exe)))
}
var bundleID = regexp.MustCompile(`<key>CFBundleIdentifier</key>\s*<string>([^<]+)</string>`)
// associated is the AssociatedBundleIdentifiers entry for the app exe sits
// in, read from that app's own Info.plist, so a build with another id
// names its own. A program outside an app, or an app whose Info.plist
// can't be read, has none: the key then names nothing at all.
func associated(exe string) string {
macos := filepath.Dir(exe)
if filepath.Base(macos) != "MacOS" {
return ""
}
b, err := os.ReadFile(filepath.Join(filepath.Dir(macos), "Info.plist"))
if err != nil {
return ""
}
m := bundleID.FindSubmatch(b)
if m == nil {
return ""
}
return fmt.Sprintf("\t<key>AssociatedBundleIdentifiers</key>\n\t<array><string>%s</string></array>\n", html.EscapeString(html.UnescapeString(string(m[1]))))
}
var program = regexp.MustCompile(`<key>ProgramArguments</key>\s*<array><string>([^<]*)</string>`)
@@ -68,7 +93,11 @@ func refresh() error {
if m == nil {
return nil // not one magpie wrote
}
want := plist(html.UnescapeString(string(m[1])))
exe := html.UnescapeString(string(m[1]))
if associated(exe) == "" && bytes.Contains(b, []byte("<key>AssociatedBundleIdentifiers</key>")) {
return nil // the app's Info.plist didn't read this time: not a reason to drop its name
}
want := plist(exe)
if bytes.Equal(b, want) {
return nil
}
@@ -84,3 +84,83 @@ func TestRefreshOlderLaunchAgent(t *testing.T) {
t.Fatalf("a launch agent magpie didn't write was changed:\n%s", b)
}
}
// fakeApp lays out an app bundle as a release's: Contents/MacOS/magpie
// beside Contents/Info.plist carrying the bundle id.
func fakeApp(t *testing.T, dir, id string) string {
t.Helper()
app := filepath.Join(dir, "Tom & Jerry", "magpie.app", "Contents")
if err := os.MkdirAll(filepath.Join(app, "MacOS"), 0o755); err != nil {
t.Fatal(err)
}
info := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key><string>magpie</string>
<key>CFBundleIdentifier</key><string>` + id + `</string>
<key>CFBundleExecutable</key><string>magpie</string>
</dict>
</plist>
`
if err := os.WriteFile(filepath.Join(app, "Info.plist"), []byte(info), 0o644); err != nil {
t.Fatal(err)
}
exe := filepath.Join(app, "MacOS", "magpie")
if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
return exe
}
// Login Items lists a launch agent under the name its program is signed
// with, the developer's own, unless the agent names the app it belongs to
// (#1512, ysicing: "Xipeng Guan" instead of magpie). The launch agent for
// a program inside an app names that app's bundle id, an older magpie's is
// rewritten with it, and a bare program names none.
func TestLaunchAgentNamesItsApp(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
exe := fakeApp(t, home, "com.yetone.magpie")
key := "<key>AssociatedBundleIdentifiers</key>\n\t<array><string>com.yetone.magpie</string></array>"
if b := string(plist(exe)); !strings.Contains(b, key) {
t.Fatalf("the launch agent doesn't name magpie's app:\n%s", b)
}
p := record()
os.MkdirAll(filepath.Dir(p), 0o755)
older := strings.Replace(olderPlist, "/Applications/Tom &amp; Jerry/magpie.app/Contents/MacOS/magpie", strings.ReplaceAll(exe, "&", "&amp;"), 1)
if err := os.WriteFile(p, []byte(older), 0o644); err != nil {
t.Fatal(err)
}
if err := Refresh(); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(p)
if !strings.Contains(string(b), key) {
t.Fatalf("an older launch agent isn't rewritten to name the app:\n%s", b)
}
if out, err := exec.Command("plutil", "-lint", p).CombinedOutput(); err != nil {
t.Fatalf("not a valid plist: %s", out)
}
out, err := exec.Command("plutil", "-extract", "AssociatedBundleIdentifiers.0", "raw", p).CombinedOutput()
if err != nil || strings.TrimSpace(string(out)) != "com.yetone.magpie" {
t.Fatalf("plutil reads AssociatedBundleIdentifiers as %q (%v)", out, err)
}
// the app's Info.plist not reading is no reason to drop the name
os.Rename(filepath.Join(filepath.Dir(filepath.Dir(exe)), "Info.plist"), filepath.Join(home, "Info.plist.aside"))
if err := Refresh(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(p); !strings.Contains(string(b), key) {
t.Fatalf("the app's name was dropped over an unread Info.plist:\n%s", b)
}
// a program outside any app has nothing to name
if b := string(plist(filepath.Join(home, "bin", "magpie"))); strings.Contains(b, "AssociatedBundleIdentifiers") {
t.Fatalf("a bare program's launch agent names an app:\n%s", b)
}
}