diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index b7873009b9..67f181e7e0 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -795,7 +795,10 @@ const MENU_COMMANDS: &[(&str, &str)] = &[ /// describes the method. A /// name it does not list throws an error that names it. A menu item that is not available, with /// no document open or nothing selected that it acts on, does nothing, as a dimmed menu item does -/// (VectorCraft's own rule). `Fit Artboard to artwork bounds` fits the active artboard. +/// (VectorCraft's own rule). A menu item the menus show as available runs its command, and an error +/// that command returns throws. `Fit Artboard to artwork bounds` fits the active artboard, and does +/// nothing when that artboard is locked or has no art, as the canvas's right-click menu dims +/// the item then ([`DocState::any_art_to_fit`]). fn app_menu_command(h: &Host, a: &[Data]) -> Answer { let Data::String(name) = arg(a, 1) else { return Err(HostError::Type("executeMenuCommand needs a menu command, as a string".into())) }; if name.len() > MAX_TEXT_BYTES { @@ -811,7 +814,13 @@ fn app_menu_command(h: &Host, a: &[Data]) -> Answer { return Ok(Data::Undefined); } match (id, s.active()) { - ("artboard.fitToArt", Some(st)) => json!({"index": active_artboard(st)}), + ("artboard.fitToArt", Some(st)) => { + let index = active_artboard(st); + if !st.any_art_to_fit(&[index]) { + return Ok(Data::Undefined); + } + json!({"index": index}) + } _ => json!({}), } }; diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 108cc647dc..6474d1e1fc 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -1394,6 +1394,19 @@ fn the_fit_to_artwork_menu_command_fits_the_artboard_to_the_art() { log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds');"); let after = s.active().unwrap().doc.artboards[0].rect; assert!(after.width() < before.width() && after.height() < before.height(), "{before:?} -> {after:?}"); + // An artboard that is locked, or has no art on it, leaves the menu item dimmed: it does nothing. + let (mut s, _, _) = doc(); + s.execute("artboard.setProps", &json!({"index": 0, "locked": true})).unwrap(); + let steps = undo_steps(&s); + assert_eq!(log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds'); vc.log(1);"), vec![json!(1)]); + assert_eq!(s.active().unwrap().doc.artboards[0].rect, before); + assert_eq!(undo_steps(&s), steps, "locked"); + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let empty = s.active().unwrap().doc.artboards[0].rect; + assert_eq!(log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds'); vc.log(1);"), vec![json!(1)]); + assert_eq!(s.active().unwrap().doc.artboards[0].rect, empty); + assert_eq!(undo_steps(&s), 0, "no art"); } #[test] diff --git a/crates/engine/tests/it/script_host_vs_scriptcheck.rs b/crates/engine/tests/it/script_host_vs_scriptcheck.rs index 4368ce29b7..d8aa36bdf9 100644 --- a/crates/engine/tests/it/script_host_vs_scriptcheck.rs +++ b/crates/engine/tests/it/script_host_vs_scriptcheck.rs @@ -82,8 +82,8 @@ const INSIDE: &[(&str, &str)] = &[ ("Application.", "prototype"), ( "Application.prototype.", - "activeDocument activeDocument= beep beep= constructor coordinateSystem coordinateSystem= documents getIsFileOpen \ - selection selection= toString typename", + "activeDocument activeDocument= beep beep= constructor coordinateSystem coordinateSystem= documents executeMenuCommand \ + getIsFileOpen selection selection= toString typename", ), ("DocumentPreset.", "prototype"), ( diff --git a/docs/mcp.md b/docs/mcp.md index 471325ae46..596910c197 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2678,7 +2678,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot `Fit Artboard to artwork bounds` (https://gist.github.com/shalakolee/df3246659d4289eb99149b9ed00a00ee), which runs `artboard.fitToArt` on the active artboard. Names are matched exactly. Any other name throws an error that names it, and a name that is not a string throws a `TypeError`. VectorCraft's own rule: a menu item that is not - available, with no document open or nothing selected that it acts on, does nothing and adds no undo step. A script may name the application's members without `app.`, as + available, with no document open, nothing selected that it acts on, or a locked active artboard or one with no art + for `Fit Artboard to artwork bounds`, does nothing and adds no undo step. An available menu item runs its command, + and an error that command returns throws. A script may name the application's members without `app.`, as [Accessing and referencing objects](https://ai-scripting.docsforadobe.dev/scriptingJavascript/objectReferences/) describes; in VectorCraft that holds for `activeDocument`, `documents` and `selection`. VectorCraft's own rules: assigning `activeDocument` or `selection` without `app.` assigns the application's member. A top-level