diff --git a/crates/engine/src/script/actions.rs b/crates/engine/src/script/actions.rs index 16c072cebd..a0fabf2103 100644 --- a/crates/engine/src/script/actions.rs +++ b/crates/engine/src/script/actions.rs @@ -15,7 +15,8 @@ //! and throws an error that names the step and the action, while the steps before it stay done. //! - A step a script may not run ([`super::refused_to_scripts`]), a step whose command the engine //! does not have (a View menu step, or one that plays an action), and a stop throw before any step -//! runs. An action cannot play another action, so it cannot play itself. +//! runs, as does such a command among those an `action.batch` step plays. An action cannot play +//! another action, so it cannot play itself. //! - `dialogs` is `true` or `false` (default `false`); VectorCraft's steps open no dialogs, so it //! changes nothing. @@ -229,13 +230,12 @@ fn do_script(h: &Host, a: &[Data]) -> std::result::Result { // Every step is checked before the first runs, so a step that cannot run leaves the documents as // they were. for s in &p.steps { - let refused = s.id == STOP_STEP || refused_to_scripts(&s.id) || crate::find_command(&s.id).is_none(); - if refused { - let (place, name, id) = (&s.place, shown(&p.name), shown(&s.id)); - return Err(HostError::Error(if s.id == STOP_STEP { + if let Some((place, id)) = refused_step(s) { + let name = shown(&p.name); + return Err(HostError::Error(if id == STOP_STEP { format!("step {place} of “{name}” is a stop, which a script cannot wait at") } else { - format!("step {place} of “{name}” runs `{id}`, which a script cannot run") + format!("step {place} of “{name}” runs `{}`, which a script cannot run", shown(&id)) })); } } @@ -263,6 +263,25 @@ fn do_script(h: &Host, a: &[Data]) -> std::result::Result { Ok(Data::Undefined) } +/// The place and command of the first command step `s` would run that a script may not run: the +/// step itself, or one of the `commands` an `action.batch` step plays on each file (placed as the +/// batch's place and its own, as in `3.2`). A stop, a command a script may not run and a command the +/// engine does not have are refused. +fn refused_step(s: &Step) -> Option<(String, String)> { + let refused = |id: &str| id == STOP_STEP || refused_to_scripts(id) || crate::find_command(id).is_none(); + if refused(&s.id) { + return Some((s.place.clone(), s.id.clone())); + } + if s.id != "action.batch" { + return None; + } + let commands = s.params.get("commands").and_then(Value::as_array)?; + commands.iter().enumerate().find_map(|(k, c)| { + let id = c.get("command").and_then(Value::as_str).unwrap_or(""); + refused(id).then(|| (format!("{}.{}", s.place, k + 1), id.to_owned())) + }) +} + /// `params` of step `id`, with the active artboard of the active document added as a /// `command.batch` given that artboard adds it when the Actions panel plays the step: `artboard` for /// the commands that act on the active artboard, `index` for Fit to Artwork Bounds and Fit to diff --git a/crates/engine/src/tests/script_actions.rs b/crates/engine/src/tests/script_actions.rs index 02ee9f5957..4fbcd55751 100644 --- a/crates/engine/src/tests/script_actions.rs +++ b/crates/engine/src/tests/script_actions.rs @@ -146,6 +146,12 @@ fn steps_a_script_may_not_run_stop_the_action_before_any_step() { let m = caught(&mut s, "app.doScript('B', 'Set')", &batch); assert!(m.starts_with("Error: step 1.2 of “B” runs `script.run`"), "{m}"); assert_eq!(objects(&s), 0); + // So is one among the commands a Batch step plays on each file, before the steps ahead of it run. + let files = json!({"command": "action.batch", "params": {"commands": [{"command": "select.all"}, {"command": "prefs.set", "params": {}}]}}); + let batch = set(&[("F", vec![rect(0), files])]); + let m = caught(&mut s, "app.doScript('F', 'Set')", &batch); + assert!(m.starts_with("Error: step 2.2 of “F” runs `prefs.set`, which a script cannot run"), "{m}"); + assert_eq!(objects(&s), 0); } #[test] diff --git a/docs/mcp.md b/docs/mcp.md index cda10102c0..6b507eab99 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2694,7 +2694,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot their exact names, and a name that matches no action or more than one throws an error before any step runs, as does an action with no included step. A stop, and a step whose command a script may not run or the engine does not have (a View menu step, or a step that plays an action or runs a script), throw an error that names the step before any - step runs, so an action cannot play itself. + step runs, as does such a command among the commands a Batch step (`action.batch`) plays on each file, so an + action cannot play itself. Each step then runs as `vc.run` runs a command: it joins the run's one undo step for each changed document, counts as a host call, and reads and writes files only where the script may. A step that fails is put back and throws an error that gives the step's place, the action's name and the step's command, and the steps before it stay done; an