BlueGit

timfa posprint

fix(macos): replace osacompile applet with native Cocoa launcher

timfa Omer Pesket · pushed by timfa

Authored 2026-10-26 · 3 weeks ago

Hash
b95959c7e095aa5f0405c520cb14a2cfdf8bcf29
Parent
528d4d7a
4 files changed +94 −40
packaging/macos/Info.plist +4 −4
@@ -7,9 +7,9 @@
7 7 <key>CFBundleDisplayName</key>
8 8 <string>POS Print</string>
9 9 <key>CFBundleExecutable</key>
10 <string>applet</string>
10 <string>posprint-launcher</string>
11 11 <key>CFBundleIconFile</key>
12 <string>applet</string>
12 <string>AppIcon</string>
13 13 <key>CFBundleIdentifier</key>
14 14 <string>com.timfa.posprint</string>
15 15 <key>CFBundleInfoDictionaryVersion</key>
@@ -20,12 +20,12 @@
20 20 <string>APPL</string>
21 21 <key>CFBundleShortVersionString</key>
22 22 <string>@VERSION@</string>
23 <key>CFBundleSignature</key>
24 <string>aplt</string>
25 23 <key>CFBundleVersion</key>
26 24 <string>@VERSION@</string>
27 25 <key>LSMinimumSystemVersion</key>
28 26 <string>11.0</string>
27 <key>LSUIElement</key>
28 <true/>
29 29 <key>NSHighResolutionCapable</key>
30 30 <true/>
31 31 <key>CFBundleURLTypes</key>
@@ -7,9 +7,9 @@
7 <key>CFBundleDisplayName</key>
7 <key>CFBundleDisplayName</key>
8 <string>POS Print</string>
8 <string>POS Print</string>
9 <key>CFBundleExecutable</key>
9 <key>CFBundleExecutable</key>
10 <string>applet</string>
10 <string>posprint-launcher</string>
11 <key>CFBundleIconFile</key>
11 <key>CFBundleIconFile</key>
12 <string>applet</string>
12 <string>AppIcon</string>
13 <key>CFBundleIdentifier</key>
13 <key>CFBundleIdentifier</key>
14 <string>com.timfa.posprint</string>
14 <string>com.timfa.posprint</string>
15 <key>CFBundleInfoDictionaryVersion</key>
15 <key>CFBundleInfoDictionaryVersion</key>
@@ -20,12 +20,12 @@
20 <string>APPL</string>
20 <string>APPL</string>
21 <key>CFBundleShortVersionString</key>
21 <key>CFBundleShortVersionString</key>
22 <string>@VERSION@</string>
22 <string>@VERSION@</string>
23 <key>CFBundleSignature</key>
24 <string>aplt</string>
25 <key>CFBundleVersion</key>
23 <key>CFBundleVersion</key>
26 <string>@VERSION@</string>
24 <string>@VERSION@</string>
27 <key>LSMinimumSystemVersion</key>
25 <key>LSMinimumSystemVersion</key>
28 <string>11.0</string>
26 <string>11.0</string>
27 <key>LSUIElement</key>
28 <true/>
29 <key>NSHighResolutionCapable</key>
29 <key>NSHighResolutionCapable</key>
30 <true/>
30 <true/>
31 <key>CFBundleURLTypes</key>
31 <key>CFBundleURLTypes</key>
packaging/macos/launcher.m added +76 −0
@@ -0,0 +1,76 @@
1 #import <Cocoa/Cocoa.h>
2 #include <spawn.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 extern char **environ;
7
8 @interface AppDelegate : NSObject <NSApplicationDelegate>
9 @property (nonatomic, assign) BOOL handledOpen;
10 @end
11
12 @implementation AppDelegate
13
14 - (void)spawnPosprintWithArg:(NSString *)arg {
15 NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
16 NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];
17 const char *bin = [binPath fileSystemRepresentation];
18
19 pid_t pid;
20 if (arg != nil) {
21 const char *cArg = [arg UTF8String];
22 char *const argv[] = { (char *)bin, (char *)cArg, NULL };
23 posix_spawn(&pid, bin, NULL, NULL, argv, environ);
24 } else {
25 char *const argv[] = { (char *)bin, NULL };
26 posix_spawn(&pid, bin, NULL, NULL, argv, environ);
27 }
28 }
29
30 - (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls {
31 self.handledOpen = YES;
32 for (NSURL *url in urls) {
33 if ([url isFileURL]) {
34 [self spawnPosprintWithArg:[url path]];
35 } else {
36 [self spawnPosprintWithArg:[url absoluteString]];
37 }
38 }
39 _exit(0);
40 }
41
42 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
43 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(80 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
44 if (!self.handledOpen) {
45 [self spawnPosprintWithArg:nil];
46 }
47 _exit(0);
48 });
49 }
50
51 @end
52
53 int main(int argc, const char * argv[]) {
54 @autoreleasepool {
55 if (argc > 1) {
56 NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
57 NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];
58 const char *bin = [binPath fileSystemRepresentation];
59 char **newArgv = calloc((size_t)argc + 1, sizeof(char *));
60 newArgv[0] = (char *)bin;
61 for (int i = 1; i < argc; i++) {
62 newArgv[i] = (char *)argv[i];
63 }
64 newArgv[argc] = NULL;
65 pid_t pid;
66 posix_spawn(&pid, bin, NULL, NULL, newArgv, environ);
67 free(newArgv);
68 return 0;
69 }
70 NSApplication *app = [NSApplication sharedApplication];
71 AppDelegate *delegate = [[AppDelegate alloc] init];
72 [app setDelegate:delegate];
73 [app run];
74 }
75 return 0;
76 }
@@ -0,0 +1,76 @@
1#import <Cocoa/Cocoa.h>
2#include <spawn.h>
3#include <stdlib.h>
4#include <unistd.h>
5
6extern char **environ;
7
8@interface AppDelegate : NSObject <NSApplicationDelegate>
9@property (nonatomic, assign) BOOL handledOpen;
10@end
11
12@implementation AppDelegate
13
14- (void)spawnPosprintWithArg:(NSString *)arg {
15 NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
16 NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];
17 const char *bin = [binPath fileSystemRepresentation];
18
19 pid_t pid;
20 if (arg != nil) {
21 const char *cArg = [arg UTF8String];
22 char *const argv[] = { (char *)bin, (char *)cArg, NULL };
23 posix_spawn(&pid, bin, NULL, NULL, argv, environ);
24 } else {
25 char *const argv[] = { (char *)bin, NULL };
26 posix_spawn(&pid, bin, NULL, NULL, argv, environ);
27 }
28}
29
30- (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls {
31 self.handledOpen = YES;
32 for (NSURL *url in urls) {
33 if ([url isFileURL]) {
34 [self spawnPosprintWithArg:[url path]];
35 } else {
36 [self spawnPosprintWithArg:[url absoluteString]];
37 }
38 }
39 _exit(0);
40}
41
42- (void)applicationDidFinishLaunching:(NSNotification *)notification {
43 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(80 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
44 if (!self.handledOpen) {
45 [self spawnPosprintWithArg:nil];
46 }
47 _exit(0);
48 });
49}
50
51@end
52
53int main(int argc, const char * argv[]) {
54 @autoreleasepool {
55 if (argc > 1) {
56 NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
57 NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];
58 const char *bin = [binPath fileSystemRepresentation];
59 char **newArgv = calloc((size_t)argc + 1, sizeof(char *));
60 newArgv[0] = (char *)bin;
61 for (int i = 1; i < argc; i++) {
62 newArgv[i] = (char *)argv[i];
63 }
64 newArgv[argc] = NULL;
65 pid_t pid;
66 posix_spawn(&pid, bin, NULL, NULL, newArgv, environ);
67 free(newArgv);
68 return 0;
69 }
70 NSApplication *app = [NSApplication sharedApplication];
71 AppDelegate *delegate = [[AppDelegate alloc] init];
72 [app setDelegate:delegate];
73 [app run];
74 }
75 return 0;
76}
scripts/build-macos.sh +13 −36
@@ -102,44 +102,21 @@ VERSION="$(grep '^version = ' "$ROOT/Cargo.toml" | head -n1 | tr -d '\r' | sed -
102 102 if [[ "$BUILD_APP" == "true" ]]; then
103 103 APP_DIR="$ROOT/target/macos/Posprint.app"
104 104 rm -rf "$APP_DIR"
105 mkdir -p "$ROOT/target/macos"
106
107 # Compile an AppleEvent launcher bundle so macOS delivers GURL (posprint://)
108 # and odoc (.pos files) events and forwards them to the embedded Rust binary.
109 APPLESCRIPT_SRC="$(mktemp /tmp/posprint-applet.XXXXXX.applescript)"
110 trap 'rm -f "$APPLESCRIPT_SRC"' EXIT
111 cat > "$APPLESCRIPT_SRC" <<'APPLESCRIPT'
112 on resolve_bin()
113 set bundlePath to POSIX path of (path to me)
114 return bundlePath & "Contents/MacOS/posprint-bin"
115 end resolve_bin
116
117 on open location this_URL
118 set binPath to resolve_bin()
119 do shell script quoted form of binPath & " " & quoted form of this_URL & " >/dev/null 2>&1 &"
120 end open location
121
122 on open these_items
123 set binPath to resolve_bin()
124 repeat with an_item in these_items
125 set itemPath to POSIX path of an_item
126 do shell script quoted form of binPath & " " & quoted form of itemPath & " >/dev/null 2>&1 &"
127 end repeat
128 end open
129
130 on run
131 set binPath to resolve_bin()
132 do shell script quoted form of binPath & " >/dev/null 2>&1 &"
133 end run
134 APPLESCRIPT
135
136 osacompile -o "$APP_DIR" "$APPLESCRIPT_SRC"
105 mkdir -p "$APP_DIR/Contents/MacOS" "$APP_DIR/Contents/Resources"
106
107 # Compile native Cocoa launcher that handles posprint:// URLs, .pos files,
108 # and direct app launches by spawning Contents/MacOS/posprint-bin.
109 cc -O2 -framework Cocoa \
110 -o "$APP_DIR/Contents/MacOS/posprint-launcher" \
111 "$ROOT/packaging/macos/launcher.m"
112 chmod +x "$APP_DIR/Contents/MacOS/posprint-launcher"
113
137 114 cp "$BIN" "$APP_DIR/Contents/MacOS/posprint-bin"
138 115 chmod +x "$APP_DIR/Contents/MacOS/posprint-bin"
139 116
140 117 sed "s/@VERSION@/${VERSION}/g" "$ROOT/packaging/macos/Info.plist" > "$APP_DIR/Contents/Info.plist"
141 118
142 # Build applet.icns from pos-logo.png if sips and iconutil are available.
119 # Build AppIcon.icns from pos-logo.png if sips and iconutil are available.
143 120 if command -v sips &>/dev/null && command -v iconutil &>/dev/null && [[ -f "$ROOT/pos-logo.png" ]]; then
144 121 ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"
145 122 for size in 16 32 128 256 512; do
@@ -147,7 +124,7 @@ APPLESCRIPT
147 124 size2=$((size * 2))
148 125 sips -z "$size2" "$size2" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}@2x.png" >/dev/null 2>&1 || true
149 126 done
150 iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/applet.icns" >/dev/null 2>&1 || true
127 iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/AppIcon.icns" >/dev/null 2>&1 || true
151 128 rm -rf "$ICONSET_DIR"
152 129 fi
153 130
@@ -172,8 +149,8 @@ APPLESCRIPT
172 149 cp "$BIN" /opt/homebrew/bin/posprint
173 150 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
174 151 elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then
175 cp "$BIN" /usr/local/bin/posprint
176 echo "Installed CLI binary: /usr/local/bin/posprint"
152 cp "$BIN" /opt/homebrew/bin/posprint
153 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
177 154 fi
178 155
179 156 LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
@@ -102,44 +102,21 @@ VERSION="$(grep '^version = ' "$ROOT/Cargo.toml" | head -n1 | tr -d '\r' | sed -
102if [[ "$BUILD_APP" == "true" ]]; then
102if [[ "$BUILD_APP" == "true" ]]; then
103 APP_DIR="$ROOT/target/macos/Posprint.app"
103 APP_DIR="$ROOT/target/macos/Posprint.app"
104 rm -rf "$APP_DIR"
104 rm -rf "$APP_DIR"
105 mkdir -p "$ROOT/target/macos"
105 mkdir -p "$APP_DIR/Contents/MacOS" "$APP_DIR/Contents/Resources"
106
106
107 # Compile an AppleEvent launcher bundle so macOS delivers GURL (posprint://)
107 # Compile native Cocoa launcher that handles posprint:// URLs, .pos files,
108 # and odoc (.pos files) events and forwards them to the embedded Rust binary.
108 # and direct app launches by spawning Contents/MacOS/posprint-bin.
109 APPLESCRIPT_SRC="$(mktemp /tmp/posprint-applet.XXXXXX.applescript)"
109 cc -O2 -framework Cocoa \
110 trap 'rm -f "$APPLESCRIPT_SRC"' EXIT
110 -o "$APP_DIR/Contents/MacOS/posprint-launcher" \
111 cat > "$APPLESCRIPT_SRC" <<'APPLESCRIPT'
111 "$ROOT/packaging/macos/launcher.m"
112on resolve_bin()
112 chmod +x "$APP_DIR/Contents/MacOS/posprint-launcher"
113 set bundlePath to POSIX path of (path to me)
113
114 return bundlePath & "Contents/MacOS/posprint-bin"
115end resolve_bin
116
117on open location this_URL
118 set binPath to resolve_bin()
119 do shell script quoted form of binPath & " " & quoted form of this_URL & " >/dev/null 2>&1 &"
120end open location
121
122on open these_items
123 set binPath to resolve_bin()
124 repeat with an_item in these_items
125 set itemPath to POSIX path of an_item
126 do shell script quoted form of binPath & " " & quoted form of itemPath & " >/dev/null 2>&1 &"
127 end repeat
128end open
129
130on run
131 set binPath to resolve_bin()
132 do shell script quoted form of binPath & " >/dev/null 2>&1 &"
133end run
134APPLESCRIPT
135
136 osacompile -o "$APP_DIR" "$APPLESCRIPT_SRC"
137 cp "$BIN" "$APP_DIR/Contents/MacOS/posprint-bin"
114 cp "$BIN" "$APP_DIR/Contents/MacOS/posprint-bin"
138 chmod +x "$APP_DIR/Contents/MacOS/posprint-bin"
115 chmod +x "$APP_DIR/Contents/MacOS/posprint-bin"
139
116
140 sed "s/@VERSION@/${VERSION}/g" "$ROOT/packaging/macos/Info.plist" > "$APP_DIR/Contents/Info.plist"
117 sed "s/@VERSION@/${VERSION}/g" "$ROOT/packaging/macos/Info.plist" > "$APP_DIR/Contents/Info.plist"
141
118
142 # Build applet.icns from pos-logo.png if sips and iconutil are available.
119 # Build AppIcon.icns from pos-logo.png if sips and iconutil are available.
143 if command -v sips &>/dev/null && command -v iconutil &>/dev/null && [[ -f "$ROOT/pos-logo.png" ]]; then
120 if command -v sips &>/dev/null && command -v iconutil &>/dev/null && [[ -f "$ROOT/pos-logo.png" ]]; then
144 ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"
121 ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"
145 for size in 16 32 128 256 512; do
122 for size in 16 32 128 256 512; do
@@ -147,7 +124,7 @@ APPLESCRIPT
147 size2=$((size * 2))
124 size2=$((size * 2))
148 sips -z "$size2" "$size2" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}@2x.png" >/dev/null 2>&1 || true
125 sips -z "$size2" "$size2" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}@2x.png" >/dev/null 2>&1 || true
149 done
126 done
150 iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/applet.icns" >/dev/null 2>&1 || true
127 iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/AppIcon.icns" >/dev/null 2>&1 || true
151 rm -rf "$ICONSET_DIR"
128 rm -rf "$ICONSET_DIR"
152 fi
129 fi
153
130
@@ -172,8 +149,8 @@ APPLESCRIPT
172 cp "$BIN" /opt/homebrew/bin/posprint
149 cp "$BIN" /opt/homebrew/bin/posprint
173 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
150 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
174 elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then
151 elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then
175 cp "$BIN" /usr/local/bin/posprint
152 cp "$BIN" /opt/homebrew/bin/posprint
176 echo "Installed CLI binary: /usr/local/bin/posprint"
153 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
177 fi
154 fi
178
155
179 LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
156 LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
tests/release_scripts.rs +1 −0
@@ -294,6 +294,7 @@ fn expected_release_filenames_match_packaging_tools() {
294 294 fn macos_info_plist_registers_posprint_url_scheme_and_pos_extension() {
295 295 let plist = include_str!("../packaging/macos/Info.plist");
296 296 assert!(plist.contains("<string>com.timfa.posprint</string>"));
297 assert!(plist.contains("<string>posprint-launcher</string>"));
297 298 assert!(plist.contains("<string>posprint</string>"));
298 299 assert!(plist.contains("<string>pos</string>"));
299 300 assert!(plist.contains("@VERSION@"));
@@ -294,6 +294,7 @@ fn expected_release_filenames_match_packaging_tools() {
294fn macos_info_plist_registers_posprint_url_scheme_and_pos_extension() {
294fn macos_info_plist_registers_posprint_url_scheme_and_pos_extension() {
295 let plist = include_str!("../packaging/macos/Info.plist");
295 let plist = include_str!("../packaging/macos/Info.plist");
296 assert!(plist.contains("<string>com.timfa.posprint</string>"));
296 assert!(plist.contains("<string>com.timfa.posprint</string>"));
297 assert!(plist.contains("<string>posprint-launcher</string>"));
297 assert!(plist.contains("<string>posprint</string>"));
298 assert!(plist.contains("<string>posprint</string>"));
298 assert!(plist.contains("<string>pos</string>"));
299 assert!(plist.contains("<string>pos</string>"));
299 assert!(plist.contains("@VERSION@"));
300 assert!(plist.contains("@VERSION@"));