Files
magpie/internal/gui/terminal_apps_darwin.m
T
Karenandyetone 2693cd8828 feat(gui): choose macOS session terminal (#241)
* feat(gui): choose macOS session terminal

* session terminal: build the darwin CLI without cgo, drop the dead macOS 11 path, and open Terminal when the .command default is not a terminal

- terminal_apps_darwin.go imports "C" but had no cgo tag, and terminal_apps_other.go was !darwin only, so `CGO_ENABLED=0 GOOS=darwin go build -tags nogui .` (make release-cli's darwin targets) failed with undefined discoverTerminals. The cgo file and its test are now darwin && cgo; the stub is !darwin || !cgo.
- magpie needs macOS 12 (LSMinimumSystemVersion, -mmacosx-version-min=12.0), so the macOS 11 branch (temp .command file, deprecated LSCopyApplicationURLsForURL, CFRelease) never ran; it is gone along with the CoreServices link.
- With no saved choice, a default .command handler that is not a listed terminal, or a failed lookup, now opens Terminal as before the PR instead of failing; Settings labels the system option "System default (Terminal)" then, and the unused "System default (unavailable)" string is dropped.
- session-terminal.test.cjs covers an editor as the default in Chromium and WebKit, en and zh, and is on the README's node --test line.

---------

Co-authored-by: yetone <yetoneful@gmail.com>
2026-09-30 02:36:12 +08:00

71 lines
3.3 KiB
Objective-C

//go:build darwin
#import <AppKit/AppKit.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#include <stdlib.h>
#include <string.h>
// A generic text editor can open a shell script too. Keep only apps whose
// bundle explicitly claims .command or Terminal's shell-script content type.
static BOOL claimsCommandFile(NSBundle *bundle) {
NSArray *documentTypes = bundle.infoDictionary[@"CFBundleDocumentTypes"];
if (![documentTypes isKindOfClass:[NSArray class]]) return NO;
for (NSDictionary *documentType in documentTypes) {
if (![documentType isKindOfClass:[NSDictionary class]]) continue;
NSArray *extensions = documentType[@"CFBundleTypeExtensions"];
if (![extensions isKindOfClass:[NSArray class]]) extensions = @[];
for (NSString *extension in extensions) {
if ([extension isKindOfClass:[NSString class]] && [extension caseInsensitiveCompare:@"command"] == NSOrderedSame) {
return YES;
}
}
NSArray *contentTypes = documentType[@"LSItemContentTypes"];
if (![contentTypes isKindOfClass:[NSArray class]]) contentTypes = @[];
for (NSString *contentType in contentTypes) {
if ([contentType isKindOfClass:[NSString class]] && [contentType isEqualToString:@"com.apple.terminal.shell-script"]) {
return YES;
}
}
}
return NO;
}
char *magpieTerminalAppsJSON(void) {
@autoreleasepool {
UTType *type = [UTType typeWithFilenameExtension:@"command"];
if (type == nil) return NULL;
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSURL *defaultURL;
NSArray<NSURL *> *appURLs;
// magpie needs macOS 12 (LSMinimumSystemVersion); the check only
// keeps builds aimed at an older default target quiet.
if (@available(macOS 12.0, *)) {
defaultURL = [workspace URLForApplicationToOpenContentType:type];
appURLs = [workspace URLsForApplicationsToOpenContentType:type];
} else {
return NULL;
}
NSString *defaultID = defaultURL ? [NSBundle bundleWithURL:defaultURL].bundleIdentifier : nil;
NSMutableArray *apps = [NSMutableArray array];
NSMutableSet *seen = [NSMutableSet set];
for (NSURL *appURL in appURLs) {
NSBundle *bundle = [NSBundle bundleWithURL:appURL];
NSString *bundleID = bundle.bundleIdentifier;
if (bundleID.length == 0 || !claimsCommandFile(bundle) || [seen containsObject:bundleID]) continue;
NSString *name = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
if (name.length == 0) name = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
if (name.length == 0) name = appURL.lastPathComponent.stringByDeletingPathExtension;
[apps addObject:@{ @"id": bundleID, @"name": name, @"path": appURL.path }];
[seen addObject:bundleID];
}
NSDictionary *result = @{ @"apps": apps, @"default": defaultID ?: @"" };
NSData *data = [NSJSONSerialization dataWithJSONObject:result options:0 error:NULL];
if (data == nil) return NULL;
char *output = malloc(data.length + 1);
if (output == NULL) return NULL;
memcpy(output, data.bytes, data.length);
output[data.length] = '\0';
return output;
}
}