{"record":{"id":"a55c722b20e3284c","repo":"chenglou/pretext","slug":"failed-to-create-safari-automation-window-windo","errorCode":null,"errorMessage":"Failed to create Safari automation window: ${windowIdRaw}","messagePattern":"Failed to create Safari automation window: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"scripts/browser-automation.ts","lineNumber":432,"sourceCode":"  const scriptLines = ['tell application \"Safari\"']\n\n  if (options.foreground === true) {\n    scriptLines.unshift('tell application \"Safari\" to activate')\n  }\n\n  scriptLines.push('set targetDocument to make new document with properties {URL:\"about:blank\"}')\n  if (options.foreground === true) {\n    scriptLines.push('set targetWindow to front window')\n    scriptLines.push('set index of targetWindow to 1')\n  }\n  scriptLines.push('return id of front window as string')\n  scriptLines.push('end tell')\n\n  const windowIdRaw = options.foreground === true ? runAppleScript(scriptLines) : runBackgroundAppleScript(scriptLines)\n\n  const windowId = Number.parseInt(windowIdRaw, 10)\n  if (!Number.isFinite(windowId)) {\n    throw new Error(`Failed to create Safari automation window: ${windowIdRaw}`)\n  }\n\n  return {\n    navigate(url) {\n      const navigateLines = [\n        'tell application \"Safari\"',\n        `set targetWindow to first window whose id is ${windowId}`,\n      ]\n      if (options.foreground === true) {\n        navigateLines.unshift('tell application \"Safari\" to activate')\n        navigateLines.push('set index of targetWindow to 1')\n      }\n      navigateLines.push(`set URL of current tab of targetWindow to ${JSON.stringify(url)}`)\n      navigateLines.push('end tell')\n      if (options.foreground === true) {\n        runAppleScript(navigateLines)\n      } else {\n        runBackgroundAppleScript(navigateLines)","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/chenglou/pretext/blob/ac49b09b7d83ede19581fa94a8b892b07d309baf/scripts/browser-automation.ts#L414-L450","documentation":"Thrown in createSafariSession() after running an AppleScript that creates a new Safari document and returns the front window's id 'as string'. The returned value is parsed with Number.parseInt; if it is not a finite number, the AppleScript did not return a valid window id and the Safari session cannot proceed. The message includes the raw (non-numeric) return value to aid diagnosis.","triggerScenarios":"The AppleScript `make new document with properties {URL:\"about:blank\"}` failed or returned an error string instead of a window id — Safari is not installed, not running, denied AppleScript automation permission, or returned an error message string that parseInt rejects. macOS Security & Privacy → Automation permissions are a frequent culprit.","commonSituations":"First run on a machine where the scripting host (Terminal/iTerm/IDE) has not been granted permission to control Safari; Safari not installed or not at the expected app bundle path; Safari running in a state that refuses new documents; a macOS version where the 'id of front window' AppleScript property behaves differently.","solutions":["Grant AppleScript automation permission: System Settings → Privacy & Security → Automation → allow your terminal/IDE to control Safari.","Ensure Safari is installed and open it once manually.","Run the AppleScript snippet manually in Script Editor to see the exact error or unexpected return value.","If the return value is an error string, address that underlying Safari/permission error."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify Safari is installed and the host has automation permission before scripting.\nimport { existsSync } from 'node:fs'\nif (!existsSync('/Applications/Safari.app')) {\n  throw new Error('Safari not found at /Applications/Safari.app')\n}","typeGuard":null,"tryCatchPattern":"// Surface the AppleScript error clearly instead of the generic parseInt failure.\nconst windowIdRaw = runAppleScript(scriptLines)\nconst windowId = Number.parseInt(windowIdRaw, 10)\nif (!Number.isFinite(windowId)) {\n  throw new Error(`Safari automation returned non-numeric window id '${windowIdRaw}'. Grant AppleScript permission to control Safari.`)\n}","preventionTips":["Grant AppleScript automation permission: System Settings → Privacy & Security → Automation.","Open Safari once manually before running automation.","Test the 'make new document' AppleScript in Script Editor first.","Run the scripting host (Terminal/IDE) as a user with Accessibility/Automation entitlements."],"tags":["browser-automation","safari","applescript","macos-permission","automation"],"backgroundTag":null,"analyzedSha":"ac49b09b7d83ede19581fa94a8b892b07d309baf","analyzedAt":"2026-08-12T17:03:16.263Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}