Scripts: pathItems reads a path by name as pageItems does

pathItems["name"] read as undefined and "name" in pathItems was false
even when a path had that name, while pageItems gives the object
getByName gives. pathItems now does the same: a read with a name
nothing has throws Not found, and the in test is false for it.
This commit is contained in:
dotCooCoo committed 2026-11-25 14:47:24 -08:00
1 parent 199c580ca4
commit da2fc57f35
3 files changed
+28 -4

No files matched your search

+10 -3
View File
@@ -67,9 +67,9 @@ use vectorcraft_js::{Data, HostClass, HostError, HostItem, HostMember, Realm, Ru
use vectorcraft_tools::params::gradient_params;
use super::{
Answer, Coords, Doc, Item, Link, NOT_FOUND, ON_LOCKED_ARTBOARD, STALE_ITEM, arg, boolean, class, colors, command, coordinate, doc_of, edit_item, editable, find_doc,
float, frame, get, given, handle_of, id_of, id_parts, item_of, item_rect, later, method, named, num, number, on_doc, pair, point, point_get,
point_method, point_property, property, this_doc, typename_of, whole, with_doc,
Answer, Coords, Doc, Item, Link, NOT_FOUND, ON_LOCKED_ARTBOARD, STALE_ITEM, arg, boolean, class, colors, command, coordinate, doc_of, edit_item,
editable, find_doc, float, frame, get, given, handle_of, id_of, id_parts, item_of, item_rect, later, method, named, num, number, on_doc, pair,
point, point_get, point_method, point_property, property, this_doc, typename_of, whole, with_doc,
};
use crate::{DocState, NodeId, NodeKind};
@@ -310,8 +310,15 @@ fn items_parent(h: &super::Host, a: &[Data]) -> Answer {
Ok(host_ref("Document", &[float(h.run), float(doc.uid)]))
}
/// `pathItems[i]`, and `pathItems["name"]` as `getByName` reads it.
fn items_at(h: &super::Host, a: &[Data]) -> Answer {
let doc = items_doc(h, a)?;
if let Data::String(_) = arg(a, 1) {
return match items_by_name(h, a) {
Err(HostError::Error(e)) if e == NOT_FOUND && super::probing(a) => Ok(Data::Undefined),
r => r,
};
}
let Data::Number(n) = arg(a, 1) else { return Ok(Data::Undefined) };
let Some(i) = whole(*n).and_then(|i| usize::try_from(i).ok()) else { return Ok(Data::Undefined) };
Ok(with_doc(h, doc, |_, st| path_items(h, st).get(i).map_or(Data::Undefined, |id| super::item_ref(h, st, *id))))
@@ -97,6 +97,22 @@ fn path_items_remove_all_refuses_locked_paths_and_changes_nothing() {
assert_eq!(log(&mut s, "vc.log(app.activeDocument.pathItems.length);"), vec![json!(0)]);
}
/// `pathItems` read with a name gives what `getByName` gives, and throws as it does for no match,
/// as `pageItems` does.
#[test]
fn path_items_read_with_a_name_give_the_named_path() {
let (mut s, a, _) = doc();
s.execute("object.setProps", &json!({"ids": [a.0], "name": "n"})).unwrap();
let out = log(
&mut s,
"var p = app.activeDocument.pathItems, m = [];\
vc.log([p['n'] === p.getByName('n'), p['n'].name, 'n' in p, 'nosuch' in p, p['length'] === p.length]);\
try { p['nosuch']; } catch (e) { m.push(e.message); }\
vc.log(m);",
);
assert_eq!(out, vec![json!([true, "n", true, false, true]), json!(["Not found"])]);
}
/// Paths on a locked artboard stay, a path in a group included: `removeAll()` throws and deletes
/// nothing, as `pageItems.removeAll()` and `remove()` do.
#[test]
+2 -1
View File
@@ -2803,7 +2803,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot
those pages list, such as `app.version` and `Document.save()`, and the members of the pages of the page-item kinds
themselves, such as a text frame's `contents`, are not defined yet and read as `undefined`.
- `Document.pathItems` (https://ai-scripting.docsforadobe.dev/jsobjref/PathItems/) lists the document's paths in the
order of `pageItems`, paths inside groups and compound paths included, with `length`, `[i]`, `index(key)`,
order of `pageItems`, paths inside groups and compound paths included, with `length`, `[i]`, `[name]` (read as
`pageItems["name"]` is read), `index(key)`,
`getByName(name)`, `removeAll()` (when any path is on a locked artboard, it throws
`cannot delete objects on a locked artboard` and deletes nothing), `add()` (a path with no points, from `path.create`, painted as new art; when new
art has no fill and no stroke, the path has neither), and the shape creators