Skip to content

getWindowIDViaAX: --window N on a zero-tab window fails with -1728 (current tab used as existence proxy) (sister concern from #85) #86

Description

@kiki830621

Problem

SafariBridge.swiftgetWindowIDViaAX(windowIndex:)(line ~2583-2587)用 current tab of window N 來「驗證 window N 是否存在」:

tell application "Safari"
    set t to current tab of window N
end tell

— Source: SafariBridge.swift:2581-2587,註解明說用途是 "Validate window N exists"

current tab 是 window 存在性的錯誤 proxy:window N 確實存在、只是有 0 tabs 時,current tab of window N 不存在 → AppleScript raise -1728 → 命令被誤報失敗,即使 window N 明明存在。此處沒有 try guard

Type

bug

Reproduction

  1. Safari 有一個存在但 0-tab 的 window(假設它是 window index 3)
  2. 跑一個走 AX window-ID resolution 的命令,例如 safari-browser screenshot --window 3 out.png(screenshot / pdf / 需要 CGWindowID 的路徑都經 getWindowIDViaAX
  3. line 2585 current tab of window 3 raise -1728 → 命令失敗

Expected

--window N 指向「存在但 0-tab」的 window 時,存在性驗證應成功(window 存在),不該因為它沒有 current tab 就誤判失敗。

Actual

current tab of window N 對 0-tab window raise -1728,validate 步驟失敗。

Root Cause

current tab 當 window 存在性檢查。應改用不依賴 tab 的存在性判斷,例如 if exists window Nid of window N(window 有 id 即存在,與 tab 數無關)。

Suggested fix

把 line 2585 換成不碰 tab 的存在性驗證:

tell application "Safari"
    if not (exists window N) then error "no such window"
    -- 或 set _ to id of window N
end tell

Relation to #85

同根(0-tab window 缺 guard),不同觸發點#85 是「枚舉所有 window」(listAllWindows) 時撞到任一 0-tab window;本 issue 是「使用者用 --window N 明確指定一個 0-tab window」時走 AX 路徑失敗。建議一併處理,徹底根除「用 current tab 當存在性/枚舉 proxy」的 anti-pattern。

對照(已正確,供範圍界定,不需改)

  • getFrontWindowID(SafariBridge.swift:2962-2965)的 current tab of front window 已包在 try 內,front window 0-tab 不 crash(降級為 nil name)。
  • daemon enumerateWindows(PreCompiledScripts.swift:81-104)先數 tabCount、完全不碰 current tab,已正確。

Source: surfaced during /idd-diagnose #85 sister concern surfacing (Step 3.6)


Current Status

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions