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.

Applied on main's paths module, keeping its import list.
This commit is contained in:
dotCooCoo committed 2026-11-29 13:46:54 -08:00
1 parent 9d175f4e85
commit e861b20a33
3 files changed
+25 -1

No files matched your search

+7
View File
@@ -404,8 +404,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))))
@@ -98,6 +98,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
@@ -3097,7 +3097,8 @@ through `$`:
those pages list, such as `app.version` and `Document.save()`, and the members of the pages of the page-item kinds
themselves that this page does not describe 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