diff --git a/src/main/capabilities/vision.ts b/src/main/capabilities/vision.ts index c9eaa80..6193755 100644 --- a/src/main/capabilities/vision.ts +++ b/src/main/capabilities/vision.ts @@ -75,6 +75,18 @@ export function cropPng(source: PNG, region: { x: number; y: number; width: numb return cropped; } +/** 目标路径容错:填的是文件夹(已存在或以分隔符结尾)时,自动往里存默认文件名 */ +async function resolveOutputPath(outputPath: string, defaultName: string): Promise { + if (/[\\/]$/.test(outputPath)) return path.join(outputPath, defaultName); + try { + const stat = await fs.stat(outputPath); + if (stat.isDirectory()) return path.join(outputPath, defaultName); + } catch { + // 路径不存在说明是要新建的文件,原样返回 + } + return outputPath; +} + register("Image_Screenshot", async (ctx, node, args) => { const folder = text(args, ["outImagePath", "folder"]); const name = text(args, ["outImageName", "name"], `screenshot_${Date.now()}.png`); @@ -95,9 +107,12 @@ register("Image_Screenshot", async (ctx, node, args) => { image = PNG.sync.read(await fs.readFile(sourceFile)); output = folder ? path.resolve(path.join(folder, name)) : sourceFile; } else { - // 当前屏幕来源:image_path 即保存路径,缺省用 输出目录+文件名;再缺省落流程证据目录(路径可预期) + // 当前屏幕来源:image_path 即保存路径(文件夹则自动追加默认文件名),缺省用 输出目录+文件名;再缺省落流程证据目录(路径可预期) image = await capturePrimaryScreenPng(); - output = path.resolve(imageArg || (folder ? path.join(folder, name) : path.join(ctx.evidenceDir, name))); + output = await resolveOutputPath( + path.resolve(imageArg || (folder ? path.join(folder, name) : path.join(ctx.evidenceDir, name))), + name + ); } if (range === "2") {