Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

feat: macOS state-aware media pause via MediaRemote framework

Alcahest863221968c7e
diff --git a/package.json b/package.json
index 4e46aa366..5b0d39b08 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,8 @@
"compile:text-monitor": "node scripts/build-text-monitor.js",
"compile:winpaste": "node scripts/build-windows-fast-paste.js",
"compile:linux-paste": "node scripts/build-linux-fast-paste.js",
- "compile:native": "npm run compile:globe && npm run compile:fast-paste && npm run compile:winkeys && npm run compile:winpaste && npm run compile:linux-paste && npm run compile:text-monitor",
+ "compile:media-remote": "node scripts/build-media-remote.js",
+ "compile:native": "npm run compile:globe && npm run compile:fast-paste && npm run compile:winkeys && npm run compile:winpaste && npm run compile:linux-paste && npm run compile:text-monitor && npm run compile:media-remote",
"prestart": "npm run compile:native",
"start": "electron .",
"predev": "npm run compile:native",
diff --git a/resources/macos-media-remote.swift b/resources/macos-media-remote.swift
new file mode 100644
index 000000000..d165d9bef
--- /dev/null
+++ b/resources/macos-media-remote.swift
@@ -0,0 +1,69 @@
+import Foundation
+
+// MediaRemote.framework command constants
+let kMRPlay: UInt32 = 0
+let kMRPause: UInt32 = 1
+let kMRTogglePlayPause: UInt32 = 2
+
+// C function type aliases from MediaRemote.framework
+typealias MRMediaRemoteSendCommandType = @convention(c) (UInt32, Optional<AnyObject>) -> Bool
+typealias MRMediaRemoteGetNowPlayingApplicationIsPlayingType = @convention(c) (DispatchQueue, @escaping (Bool) -> Void) -> Void
+
+func loadMediaRemote() -> (send: MRMediaRemoteSendCommandType, isPlaying: MRMediaRemoteGetNowPlayingApplicationIsPlayingType)? {
+ let frameworkPath = "/System/Library/PrivateFrameworks/MediaRemote.framework/MediaRemote"
+ guard let handle = dlopen(frameworkPath, RTLD_NOW) else { return nil }
+
+ guard let sendPtr = dlsym(handle, "MRMediaRemoteSendCommand"),
+ let isPlayingPtr = dlsym(handle, "MRMediaRemoteGetNowPlayingApplicationIsPlaying") else {
+ return nil
+ }
+
+ let send = unsafeBitCast(sendPtr, to: MRMediaRemoteSendCommandType.self)
+ let isPlaying = unsafeBitCast(isPlayingPtr, to: MRMediaRemoteGetNowPlayingApplicationIsPlayingType.self)
+ return (send: send, isPlaying: isPlaying)
+}
+
+func checkIsPlaying(_ isPlayingFn: MRMediaRemoteGetNowPlayingApplicationIsPlayingType) -> Bool {
+ let semaphore = DispatchSemaphore(value: 0)
+ var playing = false
+ isPlayingFn(DispatchQueue.main) { result in
+ playing = result
+ semaphore.signal()
+ }
+ _ = semaphore.wait(timeout: .now() + 2)
+ return playing
+}
+
+guard let mr = loadMediaRemote() else {
+ print("ERROR")
+ exit(1)
+}
+
+let args = CommandLine.arguments
+let command = args.count > 1 ? args[1] : ""
+
+switch command {
+case "--is-playing":
+ let playing = checkIsPlaying(mr.isPlaying)
+ print(playing ? "PLAYING" : "NOT_PLAYING")
+ exit(playing ? 0 : 1)
+
+case "--pause":
+ let playing = checkIsPlaying(mr.isPlaying)
+ if !playing {
+ print("NOT_PLAYING")
+ exit(1)
+ }
+ let ok = mr.send(kMRPause, nil)
+ print(ok ? "OK" : "FAIL")
+ exit(ok ? 0 : 1)
+
+case "--play":
+ let ok = mr.send(kMRPlay, nil)
+ print(ok ? "OK" : "FAIL")
+ exit(ok ? 0 : 1)
+
+default:
+ print("Usage: macos-media-remote --is-playing|--pause|--play")
+ exit(1)
+}
diff --git a/scripts/build-media-remote.js b/scripts/build-media-remote.js
new file mode 100644
index 000000000..27f0c66c7
--- /dev/null
+++ b/scripts/build-media-remote.js
@@ -0,0 +1,177 @@
+#!/usr/bin/env node
+
+const { spawnSync } = require("child_process");
+const crypto = require("crypto");
+const fs = require("fs");
+const path = require("path");
+
+const isMac = process.platform === "darwin";
+if (!isMac) {
+ process.exit(0);
+}
+
+// Support cross-compilation via --arch flag or TARGET_ARCH env var
+const archIndex = process.argv.indexOf("--arch");
+const targetArch =
+ (archIndex !== -1 && process.argv[archIndex + 1]) || process.env.TARGET_ARCH || process.arch;
+
+const ARCH_TO_TARGET = {
+ arm64: "arm64-apple-macosx11.0",
+ x64: "x86_64-apple-macosx10.15",
+};
+const swiftTarget = ARCH_TO_TARGET[targetArch];
+if (!swiftTarget) {
+ console.error(`[media-remote] Unsupported architecture: ${targetArch}`);
+ process.exit(1);
+}
+
+const projectRoot = path.resolve(__dirname, "..");
+const swiftSource = path.join(projectRoot, "resources", "macos-media-remote.swift");
+const outputDir = path.join(projectRoot, "resources", "bin");
+const outputBinary = path.join(outputDir, "macos-media-remote");
+const hashFile = path.join(outputDir, `.macos-media-remote.${targetArch}.hash`);
+const moduleCacheDir = path.join(outputDir, ".swift-module-cache");
+
+// Mach-O CPU type constants for architecture verification
+const ARCH_CPU_TYPE = {
+ arm64: 0x0100000c,
+ x64: 0x01000007,
+};
+
+function log(message) {
+ console.log(`[media-remote] ${message}`);
+}
+
+function ensureDir(dirPath) {
+ if (!fs.existsSync(dirPath)) {
+ fs.mkdirSync(dirPath, { recursive: true });
+ }
+}
+
+function verifyBinaryArch(binaryPath, expectedArch) {
+ try {
+ const fd = fs.openSync(binaryPath, "r");
+ const header = Buffer.alloc(8);
+ fs.readSync(fd, header, 0, 8, 0);
+ fs.closeSync(fd);
+
+ const magic = header.readUInt32LE(0);
+ if (magic !== 0xfeedfacf) {
+ return false;
+ }
+ const cpuType = header.readInt32LE(4);
+ const expectedCpu = ARCH_CPU_TYPE[expectedArch];
+ return cpuType === expectedCpu;
+ } catch {
+ return false;
+ }
+}
+
+if (!fs.existsSync(swiftSource)) {
+ console.error(`[media-remote] Swift source not found at ${swiftSource}`);
+ process.exit(1);
+}
+
+ensureDir(outputDir);
+ensureDir(moduleCacheDir);
+
+let needsBuild = true;
+if (fs.existsSync(outputBinary)) {
+ if (!verifyBinaryArch(outputBinary, targetArch)) {
+ log(`Existing binary is wrong architecture (expected ${targetArch}), rebuild needed`);
+ needsBuild = true;
+ } else {
+ try {
+ const binaryStat = fs.statSync(outputBinary);
+ const sourceStat = fs.statSync(swiftSource);
+ if (binaryStat.mtimeMs >= sourceStat.mtimeMs) {
+ needsBuild = false;
+ }
+ } catch {
+ needsBuild = true;
+ }
+ }
+}
+
+// Secondary check: compare source hash
+if (!needsBuild && fs.existsSync(outputBinary)) {
+ try {
+ const sourceContent = fs.readFileSync(swiftSource, "utf8");
+ const currentHash = crypto.createHash("sha256").update(sourceContent).digest("hex");
+
+ if (fs.existsSync(hashFile)) {
+ const savedHash = fs.readFileSync(hashFile, "utf8").trim();
+ if (savedHash !== currentHash) {
+ log("Source hash changed, rebuild needed");
+ needsBuild = true;
+ }
+ } else {
+ log(`No hash file for ${targetArch}, rebuild needed`);
+ needsBuild = true;
+ }
+ } catch (err) {
+ log(`Hash check failed: ${err.message}, forcing rebuild`);
+ needsBuild = true;
+ }
+}
+
+if (!needsBuild) {
+ process.exit(0);
+}
+
+function attemptCompile(command, args) {
+ log(`Compiling with ${[command, ...args].join(" ")}`);
+ return spawnSync(command, args, {
+ stdio: "inherit",
+ env: {
+ ...process.env,
+ SWIFT_MODULE_CACHE_PATH: moduleCacheDir,
+ },
+ });
+}
+
+const compileArgs = [
+ swiftSource,
+ "-O",
+ "-target",
+ swiftTarget,
+ "-module-cache-path",
+ moduleCacheDir,
+ "-o",
+ outputBinary,
+];
+
+let result = attemptCompile("xcrun", ["swiftc", ...compileArgs]);
+
+if (result.status !== 0) {
+ result = attemptCompile("swiftc", compileArgs);
+}
+
+if (result.status !== 0) {
+ console.error("[media-remote] Failed to compile macOS media-remote binary.");
+ process.exit(result.status ?? 1);
+}
+
+try {
+ fs.chmodSync(outputBinary, 0o755);
+} catch (error) {
+ console.warn(`[media-remote] Unable to set executable permissions: ${error.message}`);
+}
+
+if (!verifyBinaryArch(outputBinary, targetArch)) {
+ console.error(
+ `[media-remote] FATAL: Compiled binary architecture does not match target (${targetArch}).`
+ );
+ process.exit(1);
+}
+
+// Save source hash after successful build
+try {
+ const sourceContent = fs.readFileSync(swiftSource, "utf8");
+ const hash = crypto.createHash("sha256").update(sourceContent).digest("hex");
+ fs.writeFileSync(hashFile, hash);
+} catch (err) {
+ log(`Warning: Could not save source hash: ${err.message}`);
+}
+
+log(`Successfully built macOS media-remote binary (${targetArch}).`);
diff --git a/src/helpers/mediaPlayer.js b/src/helpers/mediaPlayer.js
index 466747a05..595effad3 100644
--- a/src/helpers/mediaPlayer.js
+++ b/src/helpers/mediaPlayer.js
@@ -9,8 +9,10 @@ class MediaPlayer {
this._linuxBinaryPath = null;
this._nircmdChecked = false;
this._nircmdPath = null;
+ this._macBinaryChecked = false;
+ this._macBinaryPath = null;
this._pausedPlayers = []; // MPRIS players we paused (Linux)
- this._didPause = false; // Whether we sent a pause on macOS/Windows
+ this._didPause = false; // Whether we sent a pause via toggle fallback
}
_resolveLinuxFastPaste() {
@@ -62,6 +64,33 @@ class MediaPlayer {
return null;
}
+ _resolveMacMediaRemote() {
+ if (this._macBinaryChecked) return this._macBinaryPath;
+ this._macBinaryChecked = true;
+
+ const candidates = [
+ path.join(__dirname, "..", "..", "resources", "bin", "macos-media-remote"),
+ path.join(__dirname, "..", "..", "resources", "macos-media-remote"),
+ ];
+
+ if (process.resourcesPath) {
+ candidates.push(path.join(process.resourcesPath, "bin", "macos-media-remote"));
+ }
+
+ for (const candidate of candidates) {
+ try {
+ if (fs.existsSync(candidate)) {
+ fs.accessSync(candidate, fs.constants.X_OK);
+ this._macBinaryPath = candidate;
+ return candidate;
+ }
+ } catch {
+ continue;
+ }
+ }
+ return null;
+ }
+
pauseMedia() {
try {
if (process.platform === "linux") {
@@ -271,19 +300,31 @@ class MediaPlayer {
return toggled;
}
- // --- macOS ---
+ // --- macOS: MediaRemote-aware pause/resume ---
_pauseMacOS() {
this._didPause = false;
- const result = spawnSync("osascript", [
- "-e",
- 'tell application "System Events" to key code 100',
- ], {
- stdio: "pipe",
- timeout: 3000,
- });
- if (result.status === 0) {
- debugLogger.debug("Media paused via osascript", {}, "media");
+
+ // Try MediaRemote binary first (state-aware, no toggle)
+ const binary = this._resolveMacMediaRemote();
+ if (binary) {
+ const result = spawnSync(binary, ["--pause"], {
+ stdio: "pipe",
+ timeout: 3000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media paused via MediaRemote", {}, "media");
+ this._didPause = true;
+ return true;
+ }
+ // exit 1 = nothing was playing, don't fallback to toggle
+ const output = (result.stdout?.toString() || "").trim();
+ if (output === "NOT_PLAYING") return false;
+ }
+
+ // Fallback to media key toggle
+ debugLogger.debug("MediaRemote unavailable, falling back to osascript", {}, "media");
+ if (this._sendMacMediaKey()) {
this._didPause = true;
return true;
}
@@ -293,6 +334,24 @@ class MediaPlayer {
_resumeMacOS() {
if (!this._didPause) return false;
this._didPause = false;
+
+ const binary = this._resolveMacMediaRemote();
+ if (binary) {
+ const result = spawnSync(binary, ["--play"], {
+ stdio: "pipe",
+ timeout: 3000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media resumed via MediaRemote", {}, "media");
+ return true;
+ }
+ }
+
+ // Fallback to media key toggle
+ return this._sendMacMediaKey();
+ }
+
+ _sendMacMediaKey() {
const result = spawnSync("osascript", [
"-e",
'tell application "System Events" to key code 100',
@@ -301,7 +360,7 @@ class MediaPlayer {
timeout: 3000,
});
if (result.status === 0) {
- debugLogger.debug("Media resumed via osascript", {}, "media");
+ debugLogger.debug("Media key sent via osascript", {}, "media");
return true;
}
return false;