fix: 桌面窗口未找到时输出明确错误而非进程崩溃

This commit is contained in:
yu 2026-10-28 14:28:03 +08:00
parent 3bea611750
commit 73a3f47bdf

View File

@ -136,8 +136,11 @@ function clickScript(t: UiaTarget): string {
const doubleClick = clickway === "1" || /double/i.test(clickway); const doubleClick = clickway === "1" || /double/i.test(clickway);
return `${UIA_USING} return `${UIA_USING}
${FIND_HELPER} ${FIND_HELPER}
$parent = Find-RpaWindow ${psString(s.window ?? "")} $el = $null
$el = Find-RpaElement $parent ${psString(s.name ?? "")} ${psString(s.id ?? "")} ${psString(s.type ?? "")} ${psString(s.className ?? "")} ${s.index} try {
$parent = Find-RpaWindow ${psString(s.window ?? "")}
$el = Find-RpaElement $parent ${psString(s.name ?? "")} ${psString(s.id ?? "")} ${psString(s.type ?? "")} ${psString(s.className ?? "")} ${s.index}
} catch { $el = $null }
if ($null -eq $el) { Write-Output "JSON_NOT_FOUND"; exit 0 } if ($null -eq $el) { Write-Output "JSON_NOT_FOUND"; exit 0 }
$rect = $el.Current.BoundingRectangle $rect = $el.Current.BoundingRectangle
$offscreen = $el.Current.IsOffscreen -or $rect.Width -le 0 -or $rect.Height -le 0 $offscreen = $el.Current.IsOffscreen -or $rect.Width -le 0 -or $rect.Height -le 0
@ -190,8 +193,9 @@ function setValueScript(t: UiaTarget, value: string): string {
const s = t.selector; const s = t.selector;
return `${UIA_USING} return `${UIA_USING}
${FIND_HELPER} ${FIND_HELPER}
$parent = Find-RpaWindow ${psString(s.window ?? "")} try {
$el = Find-RpaElement $parent ${psString(s.name ?? "")} ${psString(s.id ?? "")} ${psString(s.type ?? "")} ${psString(s.className ?? "")} ${s.index} $parent = Find-RpaWindow ${psString(s.window ?? "")}
$el = Find-RpaElement $parent ${psString(s.name ?? "")} ${psString(s.id ?? "")} ${psString(s.type ?? "")} ${psString(s.className ?? "")} ${s.index}
if ($null -eq $el) { Write-Output "JSON_NOT_FOUND"; exit 0 } if ($null -eq $el) { Write-Output "JSON_NOT_FOUND"; exit 0 }
$value = ${psString(value)} $value = ${psString(value)}
$method = "value_pattern" $method = "value_pattern"
@ -225,8 +229,9 @@ function getValueScript(t: UiaTarget): string {
const s = t.selector; const s = t.selector;
return `${UIA_USING} return `${UIA_USING}
${FIND_HELPER} ${FIND_HELPER}
$parent = Find-RpaWindow ${psString(s.window ?? "")} try {
$el = Find-RpaElement $parent ${psString(s.name ?? "")} ${psString(s.id ?? "")} ${psString(s.type ?? "")} ${psString(s.className ?? "")} ${s.index} $parent = Find-RpaWindow ${psString(s.window ?? "")}
$el = Find-RpaElement $parent ${psString(s.name ?? "")} ${psString(s.id ?? "")} ${psString(s.type ?? "")} ${psString(s.className ?? "")} ${s.index}
if ($null -eq $el) { Write-Output "JSON_NOT_FOUND"; exit 0 } if ($null -eq $el) { Write-Output "JSON_NOT_FOUND"; exit 0 }
$value = $el.Current.Name $value = $el.Current.Name
try { $vp = $el.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern); $value = $vp.Value } catch {} try { $vp = $el.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern); $value = $vp.Value } catch {}
@ -247,7 +252,7 @@ async function runScript(script: string, t: UiaTarget, timeoutMs = 60000): Promi
function requireFound(result: Record<string, unknown> | null, t: UiaTarget, action: string): Record<string, unknown> { function requireFound(result: Record<string, unknown> | null, t: UiaTarget, action: string): Record<string, unknown> {
if (!result) { if (!result) {
const s = t.selector; const s = t.selector;
throw new RuntimeError("desktop_element_not_found", `桌面元素未找到(${action}):${JSON.stringify({ window: s.window, name: s.name, id: s.id, type: s.type, className: s.className, index: s.index })}`, { nodeId: t.node.id, componentId: t.node.componentId }); throw new RuntimeError("desktop_element_not_found", `桌面元素未找到(${action}):${JSON.stringify({ window: s.window, name: s.name, id: s.id, type: s.type, className: s.className, index: s.index })}。若指定了窗口,请确认应用已启动且主窗口未最小化到托盘`, { nodeId: t.node.id, componentId: t.node.componentId });
} }
return result; return result;
} }
@ -291,3 +296,5 @@ register("Element_ExistsGround", existsGround);
register("Element_GetValueGround", getValueGround); register("Element_GetValueGround", getValueGround);
register("Element_WaitElementGround", waitElementGround); register("Element_WaitElementGround", waitElementGround);