Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(macos): resolve paste failure caused by wrong accessibility check and stale focus (#313)
Gabriel Steinfacd3f29fc4a
diff --git a/preload.js b/preload.jsindex 35490546c..9bb9caf5b 100644--- a/preload.js+++ b/preload.js@@ -123,6 +123,7 @@ contextBridge.exposeInMainWorld("electronAPI", {createProductionEnvFile: (key) => ipcRenderer.invoke("create-production-env-file", key),// Clipboard functions+ checkAccessibilityPermission: () => ipcRenderer.invoke("check-accessibility-permission"),readClipboard: () => ipcRenderer.invoke("read-clipboard"),writeClipboard: (text) => ipcRenderer.invoke("write-clipboard", text),checkPasteTools: () => ipcRenderer.invoke("check-paste-tools"),diff --git a/src/helpers/clipboard.js b/src/helpers/clipboard.jsindex 1e12397f8..047a24326 100644--- a/src/helpers/clipboard.js+++ b/src/helpers/clipboard.js@@ -1,4 +1,4 @@-const { clipboard } = require("electron");+const { clipboard, systemPreferences } = require("electron");const { spawn, spawnSync } = require("child_process");const { killProcess } = require("../utils/process");const path = require("path");@@ -7,10 +7,9 @@ const debugLogger = require("./debugLogger");const CACHE_TTL_MS = 30000;-// macOS accessibility: once granted, permissions persist across app sessions,-// so use a long TTL. Denied results re-check quickly so granting takes effect fast.-const ACCESSIBILITY_GRANTED_TTL_MS = 24 * 60 * 60 * 1000;-const ACCESSIBILITY_DENIED_TTL_MS = 5000;+// isTrustedAccessibilityClient() is a cheap synchronous syscall, so the cache+// only exists to debounce the dialog shown on denial.+const ACCESSIBILITY_CHECK_TTL_MS = 5000;const getLinuxDesktopEnv = () =>[process.env.XDG_CURRENT_DESKTOP, process.env.XDG_SESSION_DESKTOP, process.env.DESKTOP_SESSION]@@ -327,7 +326,14 @@ class ClipboardManager {}this.safeLog("✅ Permissions granted, attempting to paste...");- await this.pasteMacOS(originalClipboard, options);+ try {+ await this.pasteMacOS(originalClipboard, options);+ } catch (firstError) {+ this.safeLog("⚠️ First paste attempt failed, retrying...", firstError?.message);+ clipboard.writeText(text);+ await new Promise((r) => setTimeout(r, 200));+ await this.pasteMacOS(originalClipboard, options);+ }} else if (platform === "win32") {const winFastPaste = this.resolveWindowsFastPasteBinary();if (winFastPaste) {@@ -1244,44 +1250,17 @@ class ClipboardManager {return this.accessibilityCache.value;}- return new Promise((resolve) => {- const testProcess = spawn("osascript", [- "-e",- 'tell application "System Events" to get name of first process',- ]);-- let testOutput = "";- let testError = "";-- testProcess.stdout.on("data", (data) => {- testOutput += data.toString();- });-- testProcess.stderr.on("data", (data) => {- testError += data.toString();- });+ const allowed = systemPreferences.isTrustedAccessibilityClient(false);+ this.accessibilityCache = {+ value: allowed,+ expiresAt: Date.now() + ACCESSIBILITY_CHECK_TTL_MS,+ };- testProcess.on("close", (code) => {- const allowed = code === 0;- this.accessibilityCache = {- value: allowed,- expiresAt:- Date.now() + (allowed ? ACCESSIBILITY_GRANTED_TTL_MS : ACCESSIBILITY_DENIED_TTL_MS),- };- if (!allowed) {- this.showAccessibilityDialog(testError);- }- resolve(allowed);- });+ if (!allowed) {+ this.showAccessibilityDialog("not allowed assistive access");+ }- testProcess.on("error", (error) => {- this.accessibilityCache = {- value: false,- expiresAt: Date.now() + ACCESSIBILITY_DENIED_TTL_MS,- };- resolve(false);- });- });+ return allowed;}showAccessibilityDialog(testError) {diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex 3d2665fcd..8546f4b34 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -441,16 +441,28 @@ class IPCHandlers {});ipcMain.handle("paste-text", async (event, text, options) => {- // If the floating dictation panel currently has focus, blur it first so the+ // If the floating dictation panel currently has focus, dismiss it so the// paste keystroke lands in the user's target app instead of the overlay.const mainWindow = this.windowManager?.mainWindow;if (mainWindow && !mainWindow.isDestroyed() && mainWindow.isFocused()) {- mainWindow.blur();- await new Promise((resolve) => setTimeout(resolve, 80));+ if (process.platform === "darwin") {+ // hide() forces macOS to activate the previous app; showInactive()+ // restores the overlay without stealing focus.+ mainWindow.hide();+ await new Promise((resolve) => setTimeout(resolve, 120));+ mainWindow.showInactive();+ } else {+ mainWindow.blur();+ await new Promise((resolve) => setTimeout(resolve, 80));+ }}return this.clipboardManager.pasteText(text, { ...options, webContents: event.sender });});+ ipcMain.handle("check-accessibility-permission", async () => {+ return this.clipboardManager.checkAccessibilityPermissions();+ });+ipcMain.handle("read-clipboard", async (event) => {return this.clipboardManager.readClipboard();});diff --git a/src/hooks/usePermissions.ts b/src/hooks/usePermissions.tsindex 3d4da5656..671638060 100644--- a/src/hooks/usePermissions.ts+++ b/src/hooks/usePermissions.ts@@ -246,6 +246,17 @@ export const usePermissions = (checkPasteToolsAvailability();}, [checkPasteToolsAvailability]);+ // On macOS, re-validate accessibility permission on mount to override stale+ // localStorage values (e.g. after app update changes the code signature).+ useEffect(() => {+ if (getPlatform() !== "darwin") return;+ window.electronAPI?.checkAccessibilityPermission?.().then((granted) => {+ if (!granted) {+ setAccessibilityPermissionGranted(false);+ }+ });+ }, [setAccessibilityPermissionGranted]);+const testAccessibilityPermission = useCallback(async () => {const platform = getPlatform();diff --git a/src/types/electron.ts b/src/types/electron.tsindex 032f34320..dfc712128 100644--- a/src/types/electron.ts+++ b/src/types/electron.ts@@ -345,6 +345,7 @@ declare global {}) => Promise<void>;// Clipboard operations+ checkAccessibilityPermission: () => Promise<boolean>;readClipboard: () => Promise<string>;writeClipboard: (text: string) => Promise<{ success: boolean }>;checkPasteTools: () => Promise<PasteToolsResult>;