From 5228beab637323e5ef36424322061ec16b12c8db Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 19 Oct 2026 05:24:31 -0700 Subject: [PATCH] Actions: Batch numbers source files that share a name Over a folder, action.batch named each source file's output after the file without its extension. Two files with the same name, such as a.svg and sub/a.svg with includeSubdirs, got the same Export for Screens folder, Override Save path and Folder destination file. The second file's output replaced the first's, and the batch reported no failure. Batch now adds -2, -3... to the name of each later file whose name was already used, compared without case, as it does for data set names. The numbers follow the order the batch processes the files. With the Folder destination, a.svg and a.ai in one folder now give a.svg and a-2.ai. Docs: docs/mcp.md, docs/target-app-parity.md. --- crates/engine/src/cmd/fileio/action_batch.rs | 76 +++++++++++++++++++- crates/engine/src/cmd/fileio/mod.rs | 2 +- docs/mcp.md | 4 +- docs/target-app-parity.md | 2 +- 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/fileio/action_batch.rs b/crates/engine/src/cmd/fileio/action_batch.rs index feebde2755..b7300931a9 100644 --- a/crates/engine/src/cmd/fileio/action_batch.rs +++ b/crates/engine/src/cmd/fileio/action_batch.rs @@ -92,7 +92,7 @@ pub(super) fn action_batch(s: &mut Session, p: &Value) -> Result { #[cfg(not(target_arch = "wasm32"))] fn batch_folder(s: &mut Session, cmd: &str, p: &Value, override_open: bool, run: &Run<'_>) -> Result { - use super::{absolute_path, may_read}; + use super::{absolute_path, distinct_names, may_read}; let folder = str_param(p, "folder").map(absolute_path).ok_or_else(|| bad(cmd, "missing `folder`"))?; may_read(&folder)?; @@ -104,12 +104,15 @@ fn batch_folder(s: &mut Session, cmd: &str, p: &Value, override_open: bool, run: if files.is_empty() { return Err(bad(cmd, "no readable files in the folder")); } + // Each file's name without its extension, with `-2`, `-3`… after a name an earlier file used + // (`a.svg` and `sub/a.svg`, or `a.svg` and `a.ai`), as data set names are numbered. + let stems = + distinct_names(files.iter().map(|path| std::path::Path::new(path).file_stem().and_then(|s| s.to_str()).unwrap_or("file").to_string())); let steps = filter_steps(run.steps, override_open, false); let run = Run { steps: &steps, ..*run }; let mut processed = Vec::new(); let mut failed = 0usize; - for path in &files { - let stem = std::path::Path::new(path).file_stem().and_then(|s| s.to_str()).unwrap_or("file"); + for (path, stem) in files.iter().zip(&stems) { let ext = super::extension(path); match process_one_file(s, &run, path, stem, &ext) { Ok(row) => processed.push(row), @@ -528,6 +531,73 @@ mod tests { assert!(file_names(&dest).is_empty(), "{r}"); } + /// Two source files with the same name in different subfolders get Export for Screens folders of + /// their own: the second one's folder is numbered, as a repeated data set name is. + #[test] + fn batch_override_export_numbers_export_for_screens_folders_of_files_with_the_same_name() { + let src = tmp("screens-same-src"); + std::fs::create_dir_all(src.join("sub")).unwrap(); + write_svg(&src, "a.svg", "#f00"); + write_svg(&src.join("sub"), "a.svg", "#0f0"); + let dest = tmp("screens-same-out"); + let mut s = Session::new(); + let r = s + .execute( + "action.batch", + &json!({ + "folder": src.to_string_lossy(), + "includeSubdirs": true, + "destinationFolder": dest.to_string_lossy(), + "commands": [{"command": "document.exportForScreens", "params": {"formats": [{"format": "svg"}]}}] + }), + ) + .unwrap(); + for folder in file_names(&dest) { + println!("{folder}/: {:?}", file_names(&dest.join(&folder))); + } + assert_eq!(r["failed"], 0, "{r}"); + assert_eq!(file_names(&dest), ["a", "a-2"], "{r}"); + for (folder, fill) in [("a", "#ff0000"), ("a-2", "#00ff00")] { + assert_eq!(file_names(&dest.join(folder)), ["Artboard-1.svg"], "{r}"); + let svg = std::fs::read_to_string(dest.join(folder).join("Artboard-1.svg")).unwrap(); + assert!(svg.contains(fill), "{folder}: {svg}"); + } + } + + /// Two source files with the same name in different subfolders are saved under names of their + /// own, by Override Save and by the Folder destination: the second one's name is numbered. + #[test] + fn batch_numbers_saved_files_of_source_files_with_the_same_name() { + let src = tmp("save-same-src"); + std::fs::create_dir_all(src.join("sub")).unwrap(); + write_svg(&src, "a.svg", "#f00"); + write_svg(&src.join("sub"), "a.svg", "#0f0"); + let save = json!([{"command": "file.saveAs", "params": {"format": "svg"}}]); + let unlock = json!([{"command": "object.unlockAll", "params": {}}]); + for (tag, destination, commands) in [("save", "none", save), ("folder", "folder", unlock)] { + let dest = tmp(&format!("save-same-{tag}")); + let mut s = Session::new(); + let r = s + .execute( + "action.batch", + &json!({ + "folder": src.to_string_lossy(), + "includeSubdirs": true, + "destination": destination, + "destinationFolder": dest.to_string_lossy(), + "commands": commands + }), + ) + .unwrap(); + assert_eq!(r["failed"], 0, "{tag}: {r}"); + assert_eq!(file_names(&dest), ["a-2.svg", "a.svg"], "{tag}: {r}"); + for (file, fill) in [("a.svg", "#ff0000"), ("a-2.svg", "#00ff00")] { + let svg = std::fs::read_to_string(dest.join(file)).unwrap(); + assert!(svg.contains(fill), "{tag}: {file}: {svg}"); + } + } + } + /// Override Export writes an Export Assets step's files into a folder for each data set inside /// the destination folder, under the assets' names. #[test] diff --git a/crates/engine/src/cmd/fileio/mod.rs b/crates/engine/src/cmd/fileio/mod.rs index 224f8e7543..02c2278d57 100644 --- a/crates/engine/src/cmd/fileio/mod.rs +++ b/crates/engine/src/cmd/fileio/mod.rs @@ -187,7 +187,7 @@ pub fn specs() -> Vec { "Batch…", [], None, - "{commands: [{command, params}] (the action's steps), source?: folder (default) | datasets, folder? (required for folder: the source folder), includeSubdirs?: false, destination?: none (default: leave each file open) | saveAndClose (save over the source and close) | folder (save or export into destinationFolder and close), destinationFolder? (required when destination is folder; also the folder Override Save/Export write into), overrideOpen?: true (skip document.open / file.open steps: the batch opens each source file), overrideSave?: true (point document.save / file.save / file.saveAs at destinationFolder/{stem}.{ext}), overrideExport?: true (point document.export / document.exportSelection the same way, and give document.exportForScreens / assets.export the folder destinationFolder/{stem}, one for each source file or data set, in which the export names its files as it does outside Batch), stopOnError?: true, label?} play the commands on every readable file in folder (and its subfolders when includeSubdirs), or on every data set of the open document (source: datasets; File + Data Set Name for the folder destination and Override Save/Export, with `/`, `\\`, `:`, `<`, `>`, `\"`, `|`, `?`, `*` and control characters in the data set's name as `-`, and `-2`, `-3`… added to a name already used). Caps: 500 files or data sets, 8 folder levels, 256 steps. Batches cannot nest. → {processed: [{path|dataset, result|error}…], count, failed, stopped}", + "{commands: [{command, params}] (the action's steps), source?: folder (default) | datasets, folder? (required for folder: the source folder), includeSubdirs?: false, destination?: none (default: leave each file open) | saveAndClose (save over the source and close) | folder (save or export into destinationFolder and close), destinationFolder? (required when destination is folder; also the folder Override Save/Export write into), overrideOpen?: true (skip document.open / file.open steps: the batch opens each source file), overrideSave?: true (point document.save / file.save / file.saveAs at destinationFolder/{stem}.{ext}), overrideExport?: true (point document.export / document.exportSelection the same way, and give document.exportForScreens / assets.export the folder destinationFolder/{stem}, one for each source file or data set, in which the export names its files as it does outside Batch), stopOnError?: true, label?} play the commands on every readable file in folder (and its subfolders when includeSubdirs; {stem} is the file's name without its extension, with `-2`, `-3`… added to a name an earlier file used), or on every data set of the open document (source: datasets; File + Data Set Name for the folder destination and Override Save/Export, with `/`, `\\`, `:`, `<`, `>`, `\"`, `|`, `?`, `*` and control characters in the data set's name as `-`, and `-2`, `-3`… added to a name already used). Caps: 500 files or data sets, 8 folder levels, 256 steps. Batches cannot nest. → {processed: [{path|dataset, result|error}…], count, failed, stopped}", always, action_batch::action_batch ), diff --git a/docs/mcp.md b/docs/mcp.md index e782fd13f0..3b83207248 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1157,7 +1157,9 @@ already walks Image Trace. Short agent entry points: source file's extension (`.vectorcraft` over data sets). Override Export gives a `document.exportForScreens` or `assets.export` step the `folder` `destinationFolder/`, in place of the step's own folder or an export preset's: one folder for each source file, named after the file without its extension, or for each data set, - named `_`. The export names the files in it as it does outside Batch. + named `_`. The export names the files in it as it does outside Batch. When two source files + have the same name without the extension (`a.svg` and `sub/a.svg`, or `a.svg` and `a.ai`), the folder destination + and the overrides add `-2`, `-3`… to the name for each later file (compared without case), as for data sets. - Image Trace: `imageTrace.presets`, `imageTrace.make {id?, preset?, params?, view?}` (or `imageTrace.makeAndExpand`), `imageTrace.setView`, `imageTrace.expand`, `imageTrace.release`. - Live Paint: `livePaint.make`, `livePaint.merge`, `livePaint.fill {group?, point}`, `livePaint.strokeEdge`, diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 5b0689db7c..47e0365b91 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -430,7 +430,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Variables: `dataset.select`, `dataset.next` and `dataset.prev` name the area type and type on a path whose text they rewrote and that doesn't fit, in the reply's `overflow` key, as `text.setText` does. - Variables: Lock Variables (`variable.lock`, in the Variables panel's bottom bar and menu) stops variables from being defined, deleted or renamed until they are unlocked, while objects can still be bound and unbound and data sets still change and apply; the lock is saved with the document. - Batch…: Override Save and Override Export name each file with the extension of the format the step writes, however the step spells the format (`.svg` and `SVG` give `.svg`, `png8` gives `.png`), in `action.batch`. - - Batch…: Override Export writes the files of an Export for Screens or Export Assets step into a folder for each source file or data set inside the destination folder, under the names the export gives them (`action.batch`). + - Batch…: Override Export writes the files of an Export for Screens or Export Assets step into a folder for each source file or data set inside the destination folder, under the names the export gives them (`action.batch`). When source files have the same name without the extension, Batch adds `-2`, `-3`… to the folder or file name of each later one. - Actions panel (record/playback, persisted), generic parameter dialogs for every "…" command. - Batch… over data sets names each file after the document and the data set (`action.batch`). It writes `/`, `\`, `:`, `<`, `>`, `"`, `|`, `?`, `*` and control characters in the data set's name as `-`, and adds `-2`, `-3`… to a name an earlier data set used. - Export for Screens, Save for Web (slice images, and files named after the document's title) and the Libraries panel name files with the characters Windows does not allow written as `-` and with a `-` after a Windows device name such as `CON`, on every platform (`document.exportForScreens`, `document.exportForWeb`, `library.create`).