From e923de0ff880a14196f22b9e84725e3750f83500 Mon Sep 17 00:00:00 2001 From: Gert Pellin Date: Fri, 23 Oct 2026 09:41:23 +0200 Subject: [PATCH 1/2] CLI: run and batch skip Deselect with nothing selected, as playback does (#8802) Action playback treats Deselect with nothing selected as a silent no-op since #6744, as Photoshop does. photocraft-cli runs its --cmd list and batch steps one by one through command_run, so a Deselect after Reveal Selection (which drops the selection since #5251) stopped the whole run with "no selection" and wrote no output. run and batch now use the same check as playback (actions_cmds::noop_step, now public). run still prints a result line for the skipped step. A single Deselect outside a script stays disabled, as the #6744 test wants. Tests: deselect_noop_tests::run_skips_deselect_with_nothing_selected and batch_skips_deselect_with_nothing_selected (both fail on main). --- apps/photocraft-cli/src/lib.rs | 100 +++++++++++++++++++++++++++++- crates/engine/src/actions_cmds.rs | 3 +- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/apps/photocraft-cli/src/lib.rs b/apps/photocraft-cli/src/lib.rs index c2497e1bf1..e57e22cb8c 100644 --- a/apps/photocraft-cli/src/lib.rs +++ b/apps/photocraft-cli/src/lib.rs @@ -390,6 +390,11 @@ fn run_cmds(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { return Err("run needs at least one --cmd (or --out)".into()); } for (id, params) in cmds { + // Deselect with nothing selected is a silent no-op in a script, as in action playback (#8802). + if photocraft_engine::actions_cmds::noop_step(&h.session, &id) { + print_json(out, &json!({"command": id, "result": null}), true)?; + continue; + } let r = h.command_run(&id, params).map_err(|e| format!("`{id}`: {e}"))?; if let Some(e) = photocraft_engine::actions_cmds::nested_failure(&id, &r) { return Err(format!("`{id}`: {e}")); @@ -573,7 +578,10 @@ fn batch(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { h.session.actions.list = set.clone(); h.open(input).map_err(|e| e.to_string())?; for (id, p) in &actions { - if photocraft_engine::actions_cmds::shell_view_command(id) || photocraft_engine::actions_cmds::shell_save_command(id) { + if photocraft_engine::actions_cmds::shell_view_command(id) + || photocraft_engine::actions_cmds::shell_save_command(id) + || photocraft_engine::actions_cmds::noop_step(&h.session, id) + { continue; } let r = h.command_run(id, p.clone()).map_err(|e| format!("`{id}`: {e}"))?; @@ -876,3 +884,93 @@ mod batch_action_set_tests { let _ = std::fs::remove_dir_all(&dir); } } + +#[cfg(test)] +mod deselect_noop_tests { + use super::*; + + fn invoke(args: &[&str]) -> (i32, String, String) { + let (mut output, mut errors) = (Vec::new(), Vec::new()); + let args: Vec = args.iter().map(|s| s.to_string()).collect(); + let code = run(&args, &mut output, &mut errors); + (code, String::from_utf8(output).unwrap(), String::from_utf8(errors).unwrap()) + } + + /// #8802: Deselect with nothing selected is skipped in a `run`, as action playback does + /// (#6744), so a script that deselects after Reveal Selection still finishes and saves. + #[test] + fn run_skips_deselect_with_nothing_selected() { + let folder = std::env::temp_dir().join(format!("photocraft-cli-deselect-run-{}", std::process::id())); + std::fs::create_dir_all(&folder).unwrap(); + let native = folder.join("out.pcraft").to_string_lossy().to_string(); + let (code, stdout, stderr) = invoke(&[ + "run", + "--new", + r#"{"width":16,"height":16}"#, + "--cmd", + "select.rect", + "--params", + r#"{"x":0,"y":0,"width":8,"height":16}"#, + "--cmd", + "layer.new.layer", + "--cmd", + "layer.layerMask.revealSelection", + "--cmd", + "select.deselect", + "--cmd", + "layer.new.layer", + "--params", + r#"{"name":"after"}"#, + "--out", + &native, + ]); + assert_eq!(code, 0, "{stderr}"); + let results: Vec = stdout.lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(results.len(), 5, "every command reports a result: {stdout}"); + assert_eq!(results[3], json!({"command": "select.deselect", "result": null})); + let (code, stdout, stderr) = invoke(&["info", &native, "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert_eq!(info["layers"][0]["name"], "after", "the steps after Deselect ran: {info}"); + + // Deselect with a selection still deselects: Inverse afterwards finds nothing to invert. + let (code, _, stderr) = invoke(&[ + "run", + "--new", + r#"{"width":16,"height":16}"#, + "--cmd", + "select.all", + "--cmd", + "select.deselect", + "--cmd", + "select.inverse", + "--out", + &native, + ]); + assert_eq!(code, 1, "{stderr}"); + assert!(stderr.contains("`select.inverse`") && stderr.contains("no selection"), "{stderr}"); + + let _ = std::fs::remove_dir_all(&folder); + } + + /// #8802: a batched action that starts with Deselect plays on files with nothing selected, + /// as the same action does in the Actions panel (#6744). + #[test] + fn batch_skips_deselect_with_nothing_selected() { + let dir = std::env::temp_dir().join(format!("photocraft-cli-deselect-batch-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let path = |name: &str| dir.join(name).to_string_lossy().to_string(); + std::fs::create_dir_all(dir.join("in")).unwrap(); + let (code, _, stderr) = invoke(&["run", "--new", r#"{"width":8,"height":6}"#, "--out", &path("in/a.png")]); + assert_eq!(code, 0, "{stderr}"); + std::fs::write(dir.join("steps.json"), json!([["select.deselect", {}], ["image.imageRotation.90cw", {}]]).to_string()).unwrap(); + let (code, stdout, stderr) = invoke(&["batch", "--actions", &path("steps.json"), "--in", &path("in"), "--out", &path("out")]); + assert_eq!(code, 0, "{stdout}{stderr}"); + assert!(stdout.contains("1 succeeded, 0 failed"), "{stdout}"); + let (code, stdout, stderr) = invoke(&["info", &path("out/a.png"), "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(6), Some(8)), "the step after Deselect ran: {info}"); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rs index 6f559a5659..8788455158 100644 --- a/crates/engine/src/actions_cmds.rs +++ b/crates/engine/src/actions_cmds.rs @@ -354,7 +354,8 @@ fn stop(s: &mut Session, _p: &Value) -> Result { /// A step Photoshop plays as a silent no-op although its menu item is greyed: Deselect with an /// open document and nothing selected (actions often start with it to clear any selection, #6744). -fn noop_step(s: &Session, id: &str) -> bool { +/// The CLI's `run` and `batch` scripts skip it too (#8802). +pub fn noop_step(s: &Session, id: &str) -> bool { id == "select.deselect" && s.active().is_some_and(|d| d.doc.selection.is_none()) } From fa8c55da56790bd38670fcdf0f15928ea53636e9 Mon Sep 17 00:00:00 2001 From: Gert Pellin Date: Fri, 23 Oct 2026 10:06:32 +0200 Subject: [PATCH 2/2] CLI: document run and batch, and the test helper --- apps/photocraft-cli/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/photocraft-cli/src/lib.rs b/apps/photocraft-cli/src/lib.rs index e57e22cb8c..5eae884338 100644 --- a/apps/photocraft-cli/src/lib.rs +++ b/apps/photocraft-cli/src/lib.rs @@ -365,6 +365,8 @@ fn command_list(a: &Args) -> Result, String> { Ok(cmds) } +/// `run`: open a file (or make a new one), run each `--cmd` in order, print each result as a +/// JSON line and save to `--out`. Deselect with nothing selected is skipped (#8802). fn run_cmds(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { let opts = export_opts(a)?; let mut h = Headless::trusted_local(); @@ -539,6 +541,8 @@ fn strip_shell_steps(action: &mut Action) { (action.steps, action.disabled, action.dialogs) = (steps, disabled, dialogs); } +/// `batch`: play the steps of an actions file on every image in `--in` and save to `--out`. +/// Deselect with nothing selected is skipped, as in playback (#8802). fn batch(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { let actions_path = a.get("--actions").ok_or("batch needs --actions ")?; let in_dir = PathBuf::from(a.get("--in").ok_or("batch needs --in ")?); @@ -889,6 +893,7 @@ mod batch_action_set_tests { mod deselect_noop_tests { use super::*; + /// Run the CLI with `args`: (exit code, stdout, stderr). fn invoke(args: &[&str]) -> (i32, String, String) { let (mut output, mut errors) = (Vec::new(), Vec::new()); let args: Vec = args.iter().map(|s| s.to_string()).collect();