chenglou/pretext · critical · Error

Failed to create Safari automation window: ${windowIdRaw}

Error message

Failed to create Safari automation window: ${windowIdRaw}

What it means

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.

Source

Thrown at scripts/browser-automation.ts:432

  const scriptLines = ['tell application "Safari"']

  if (options.foreground === true) {
    scriptLines.unshift('tell application "Safari" to activate')
  }

  scriptLines.push('set targetDocument to make new document with properties {URL:"about:blank"}')
  if (options.foreground === true) {
    scriptLines.push('set targetWindow to front window')
    scriptLines.push('set index of targetWindow to 1')
  }
  scriptLines.push('return id of front window as string')
  scriptLines.push('end tell')

  const windowIdRaw = options.foreground === true ? runAppleScript(scriptLines) : runBackgroundAppleScript(scriptLines)

  const windowId = Number.parseInt(windowIdRaw, 10)
  if (!Number.isFinite(windowId)) {
    throw new Error(`Failed to create Safari automation window: ${windowIdRaw}`)
  }

  return {
    navigate(url) {
      const navigateLines = [
        'tell application "Safari"',
        `set targetWindow to first window whose id is ${windowId}`,
      ]
      if (options.foreground === true) {
        navigateLines.unshift('tell application "Safari" to activate')
        navigateLines.push('set index of targetWindow to 1')
      }
      navigateLines.push(`set URL of current tab of targetWindow to ${JSON.stringify(url)}`)
      navigateLines.push('end tell')
      if (options.foreground === true) {
        runAppleScript(navigateLines)
      } else {
        runBackgroundAppleScript(navigateLines)

View on GitHub (pinned to ac49b09b7d)

Solutions

  1. Grant AppleScript automation permission: System Settings → Privacy & Security → Automation → allow your terminal/IDE to control Safari.
  2. Ensure Safari is installed and open it once manually.
  3. Run the AppleScript snippet manually in Script Editor to see the exact error or unexpected return value.
  4. If the return value is an error string, address that underlying Safari/permission error.
Defensive patterns

Strategy: validation

Validate before calling

// Verify Safari is installed and the host has automation permission before scripting.
import { existsSync } from 'node:fs'
if (!existsSync('/Applications/Safari.app')) {
  throw new Error('Safari not found at /Applications/Safari.app')
}

Try / catch

// Surface the AppleScript error clearly instead of the generic parseInt failure.
const windowIdRaw = runAppleScript(scriptLines)
const windowId = Number.parseInt(windowIdRaw, 10)
if (!Number.isFinite(windowId)) {
  throw new Error(`Safari automation returned non-numeric window id '${windowIdRaw}'. Grant AppleScript permission to control Safari.`)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of chenglou/pretext@ac49b09b7d (2026-08-12). Data as JSON: /api/errors/a55c722b20e3284c. Report an issue: GitHub.