diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index f8caab9adb..e70dfbb86a 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -1378,17 +1378,15 @@ fn item_selected(h: &Host, a: &[Data]) -> Answer { fn item_set_selected(h: &Host, a: &[Data]) -> Answer { let item = this_item(h, a)?; let on = boolean(arg(a, 1), "selected")?; - let mut ids = with_doc(h, item.doc, |_, st| st.selection.objects.clone()); - if ids.contains(&item.id) == on { + if with_doc(h, item.doc, |_, st| st.selection.objects.contains(&item.id)) == on { return Ok(Data::Undefined); } - if on { - let mut given = selection_ids(h, item.doc, arg(a, 0))?; - ids.append(&mut given); - } else { - ids.retain(|i| *i != item.id); - } - set_selection(h, item.doc, ids) + // Adding or removing the one object keeps how the others are selected (their direct-selected + // anchors and segments, and the key object). + let ids = if on { selection_ids(h, item.doc, arg(a, 0))? } else { vec![item.id] }; + let id = if on { "select.add" } else { "select.toggle" }; + on_doc(h, item.doc.uid, false, |h| command(h, id, &json!({"ids": ids.iter().map(|i| i.0).collect::>()})))?; + Ok(Data::Undefined) } /// The item's geometric bounds (`visual`: its visible bounds) in document coordinates and the diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index a0e63a5d06..9d933378ea 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -835,6 +835,24 @@ fn headless_add_document_dialog_fails_without_creating() { assert_eq!(s.documents().len(), 1); } +#[test] +fn setting_selected_keeps_how_the_other_objects_are_selected() { + let (mut s, a, b) = doc(); + let c = rect(&mut s, 200.0, 100.0, 50.0, 50.0); + s.execute("select.set", &json!({"ids": [b.0]})).unwrap(); + s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 0]], "mode": "add"})).unwrap(); + let anchors = s.active().unwrap().selection.anchors.get(&a).cloned(); + assert!(anchors.is_some()); + log( + &mut s, + "var p = app.activeDocument.pageItems;\ + for (var i = 0; i < p.length; i++) { if (p[i].left == 200) p[i].selected = true; if (p[i].left == 100) p[i].selected = false; }", + ); + let sel = &s.active().unwrap().selection; + assert_eq!(sel.objects, vec![a, c]); + assert_eq!(sel.anchors.get(&a).cloned(), anchors, "the direct-selected anchors stay"); +} + #[test] fn add_document_takes_only_a_document_preset() { let mut s = Session::new(); diff --git a/docs/mcp.md b/docs/mcp.md index 182e0fe1c8..a826f5b3b8 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2644,7 +2644,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot has `length`, `[i]`, `index(key)`, `getByName(name)` and `removeAll()`; `i in` either collection is true for each entry `[i]` gives. Each page item (https://ai-scripting.docsforadobe.dev/jsobjref/PageItem/) has `typename`, `parent`, `name`, `note` (the - Attributes panel's note, trimmed as `attributes.set` trims it), `hidden`, `locked`, `selected`, `opacity` (0 to + Attributes panel's note, trimmed as `attributes.set` trims it), `hidden`, `locked`, `selected` (assigning it adds + or removes only that object, as `select.add` and `select.toggle` do, and the other objects stay selected as they + were), `opacity` (0 to 100), `position`, `left`, `top`, `width`, `height`, `geometricBounds`, `visibleBounds`, `controlBounds` (the visible bounds), `editable`, `zOrderPosition`, `duplicate(relativeObject?, insertionLocation?)`, `move(relativeObject, insertionLocation?)`, `remove()` and `zOrder(method)`