Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(cli): open the app when macOS hands a launch to a running CLI process

Etienne Lescot394aebef95f1
diff --git a/electron/cli/cliMain.ts b/electron/cli/cliMain.ts
index f56980c3f..7c446bef3 100644
--- a/electron/cli/cliMain.ts
+++ b/electron/cli/cliMain.ts
@@ -14,6 +14,7 @@ import { serveMcpOverStdio } from "../mcp/stdio-server";
import { registerSttIpc, shutdownStt } from "../stt";
import { createRunnerWindow } from "../windows";
import { CLI_USAGE, type CliCommand } from "./args";
+import { openGuiFromCli } from "./openGui";
import { runInfoCommand, runPackCommand } from "./projectCommands";
import { createRunnerHost } from "./runnerHost";
import { isolateCliSessionData } from "./sessionData";
@@ -273,9 +274,11 @@ export function runCli(command: CliCommand): void {
// so the WebGL-based export renderer still works.
app.commandLine.appendSwitch("enable-unsafe-swiftshader");
- // Never show the dock icon for CLI runs.
+ // Never show the dock icon for CLI runs, and hand a launch of the app that macOS routed
+ // to this process on to the GUI (see openGui.ts).
if (process.platform === "darwin") {
app.dock?.hide();
+ app.on("activate", () => openGuiFromCli());
}
app.on("window-all-closed", () => {
diff --git a/electron/cli/openGui.test.ts b/electron/cli/openGui.test.ts
new file mode 100644
index 000000000..193b6763e
--- /dev/null
+++ b/electron/cli/openGui.test.ts
@@ -0,0 +1,45 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const spawn = vi.hoisted(() => vi.fn());
+vi.mock("node:child_process", () => ({ spawn }));
+
+import { appBundleOf, openGuiFromCli } from "./openGui";
+
+describe("appBundleOf", () => {
+ it("climbs from Contents/MacOS to the .app bundle", () => {
+ expect(appBundleOf("/Applications/Openscreen.app/Contents/MacOS/Openscreen")).toBe(
+ "/Applications/Openscreen.app",
+ );
+ expect(appBundleOf("/Users/me/Downloads/Open Screen.app/Contents/MacOS/Openscreen")).toBe(
+ "/Users/me/Downloads/Open Screen.app",
+ );
+ });
+
+ it("refuses anything that is not an executable inside an .app", () => {
+ // A dev run is node_modules' Electron.app driving this repo: it is an .app, but not ours
+ // to launch, and openGuiFromCli is only reached from a packaged CLI run anyway.
+ expect(appBundleOf("/usr/local/bin/openscreen")).toBeNull();
+ expect(appBundleOf("/Applications/Openscreen.app/Contents/Resources/openscreen")).toBeNull();
+ expect(appBundleOf("/opt/Openscreen/Contents/MacOS/Openscreen")).toBeNull();
+ });
+});
+
+describe("openGuiFromCli", () => {
+ beforeEach(() => {
+ spawn.mockReset();
+ spawn.mockReturnValue({ on: vi.fn(), unref: vi.fn() });
+ });
+
+ it("opens the bundle as a new instance, detached", () => {
+ openGuiFromCli("/Applications/Openscreen.app/Contents/MacOS/Openscreen");
+ expect(spawn).toHaveBeenCalledWith("open", ["-n", "/Applications/Openscreen.app"], {
+ detached: true,
+ stdio: "ignore",
+ });
+ });
+
+ it("does nothing outside a bundle", () => {
+ openGuiFromCli("/usr/local/bin/openscreen");
+ expect(spawn).not.toHaveBeenCalled();
+ });
+});
diff --git a/electron/cli/openGui.ts b/electron/cli/openGui.ts
new file mode 100644
index 000000000..a6841edcc
--- /dev/null
+++ b/electron/cli/openGui.ts
@@ -0,0 +1,38 @@
+// Opening the app while a CLI run of it is alive, on macOS.
+//
+// `Openscreen mcp` (and `record`, `export`) run the app's own executable, so LaunchServices
+// counts them as a running copy of OpenScreen. Finder, the Dock and `open` then hand a launch
+// to that process, a hidden one with no window, instead of starting the GUI: the app looked
+// dead for as long as an agent kept its MCP session open (#1214). Each such launch reaches the
+// CLI process as `activate`, so it starts the GUI itself, as a new instance. When a GUI is
+// already running, that instance loses the single-instance lock and brings it forward through
+// `second-instance`.
+
+import { spawn } from "node:child_process";
+import path from "node:path";
+
+/** The `.app` bundle an executable at `Contents/MacOS/<name>` belongs to, or null. */
+export function appBundleOf(execPath: string): string | null {
+ const macosDir = path.posix.dirname(execPath);
+ const contentsDir = path.posix.dirname(macosDir);
+ const bundle = path.posix.dirname(contentsDir);
+ if (
+ path.posix.basename(macosDir) !== "MacOS" ||
+ path.posix.basename(contentsDir) !== "Contents" ||
+ !bundle.endsWith(".app")
+ ) {
+ return null;
+ }
+ return bundle;
+}
+
+/** Start the GUI from its bundle as a new instance. A no-op outside a packaged `.app`. */
+export function openGuiFromCli(execPath: string = process.execPath): void {
+ const bundle = appBundleOf(execPath);
+ if (!bundle) return;
+ const child = spawn("open", ["-n", bundle], { detached: true, stdio: "ignore" });
+ child.on("error", () => {
+ // Nothing to report to: stdout and stderr belong to the CLI protocol.
+ });
+ child.unref();
+}