From b8ee4686b03856782fdd851d113a8143620f9585 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 15:39:56 -0800 Subject: [PATCH] Scripts: getIsFileOpen keeps backslashes in Unix file names; DocumentPreset checks its receiver app.getIsFileOpen treated any path with a backslash as a Windows path, turning each backslash into a folder separator and ignoring letter case. On macOS and Linux a backslash is a character of a file name, so getIsFileOpen answered true for the path /work/a\b.ai while /work/a/b.ai was open. Paths now get the Windows rules only on Windows or when they start with a drive letter. DocumentPreset.typename and toString() answered for a preset kept from an earlier run with #targetengine, while its settings throw a TypeError. Both now check the preset the way the settings do. Documents.addDocumentNoUI(startupPreset) was missing. It now makes the document addDocument(startupPreset) makes. --- crates/engine/src/script/dom.rs | 37 ++++++++++++---- crates/engine/src/tests/script_dom.rs | 44 +++++++++++++++++++ .../tests/it/script_host_vs_scriptcheck.rs | 2 +- docs/mcp.md | 9 +++- 4 files changed, 81 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index d8d01956f4..b105e606b6 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -1013,13 +1013,13 @@ fn app_set_coords(h: &Host, a: &[Data]) -> Answer { Ok(Data::Undefined) } -/// A path as `getIsFileOpen` compares it: `\` and `/` alike, and on Windows paths (a drive letter -/// or a `\`) letters in one case. +/// A path as `getIsFileOpen` compares it. A Windows path (any path on Windows, or one that starts +/// with a drive letter) takes `\` and `/` alike and letters in one case. On other systems `\` is a +/// character of a file name, so `/work/a\b.ai` and `/work/a/b.ai` are different files. fn path_key(p: &str) -> String { - let windows = p.contains('\\') || p.as_bytes().get(1) == Some(&b':'); - let p = p.replace('\\', "/"); - let p = p.trim_end_matches('/').to_string(); - if windows { p.to_lowercase() } else { p } + let b = p.as_bytes(); + let windows = cfg!(windows) || (b.len() >= 2 && b[0].is_ascii_alphabetic() && b[1] == b':'); + if windows { p.replace('\\', "/").trim_end_matches('/').to_lowercase() } else { p.trim_end_matches('/').to_string() } } /// `app.redraw()`: returns without changing anything. The app draws a frame between the run's @@ -1503,6 +1503,13 @@ fn docs_add_document(h: &Host, a: &[Data]) -> Answer { }) } +/// `documents.addDocumentNoUI(startupPreset)`: a new document from the New Document profile +/// `startupPreset` names, as `addDocument(startupPreset)` makes it, without the New Document +/// dialog. Arguments after `startupPreset` are ignored. +fn docs_add_document_no_ui(h: &Host, a: &[Data]) -> Answer { + docs_add_document(h, &a[..a.len().min(2)]) +} + /// `new DocumentPreset()`: a record of new-document settings with the defaults the help page /// gives; the other settings are /// left out. The settings stay with the run, which hands the script an object of the @@ -1581,6 +1588,19 @@ fn preset_of(h: &Host, d: &Data) -> Option { (i < h.dom.presets.borrow().len()).then_some(i) } +/// `DocumentPreset.typename`: a `TypeError` for a preset from an earlier run, as its settings give. +fn preset_typename(h: &Host, a: &[Data]) -> Answer { + preset_of(h, arg(a, 0)).ok_or_else(|| HostError::Type("this is not a DocumentPreset".into()))?; + Ok(text("DocumentPreset")) +} + +/// `DocumentPreset.toString()`: `[DocumentPreset]`, or a `TypeError` for a preset from an earlier +/// run. +fn preset_to_string(h: &Host, a: &[Data]) -> Answer { + preset_of(h, arg(a, 0)).ok_or_else(|| HostError::Type("this is not a DocumentPreset".into()))?; + Ok(text("[DocumentPreset]")) +} + /// The enumeration whose values the `DocumentPreset` setting `key` takes, when it takes one. fn preset_enum(key: &str) -> Option<&'static str> { Some(match key { @@ -2949,8 +2969,8 @@ fn classes(link: &Link) -> Vec { new_preset(&h) }); let mut members = vec![ - ("typename".to_string(), typename_of("DocumentPreset"), typed("string")), - ("toString".to_string(), class_text("DocumentPreset"), returns("string", &[])), + ("typename".to_string(), get(link, preset_typename), typed("string")), + ("toString".to_string(), method(link, preset_to_string), returns("string", &[])), ]; members.extend(PRESET_FIELDS.iter().map(|k| (k.to_string(), preset_field(link, k), typed(preset_type(k))))); HostClass { construct: Some(f), ..class("DocumentPreset", None, members, None) } @@ -2988,6 +3008,7 @@ fn classes(link: &Link) -> Vec { method(l, docs_add_document), returns("Document", &[("startupPreset", "string"), ("presetSettings", "DocumentPreset"), ("showOptionsDialog", "boolean")]), ), + ("addDocumentNoUI", method(l, docs_add_document_no_ui), returns("Document", &[("startupPreset", "string")])), ("toString", class_text("Documents"), returns("string", &[])), ]), Some(function(l, docs_at)), diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 14878904fb..330f321df2 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -1382,6 +1382,18 @@ fn a_document_preset_is_an_instance_of_its_class() { assert_eq!(out, vec![json!(true), json!("DocumentPreset"), json!(612)]); } +/// A preset kept from an earlier run throws from `typename` and `toString()` as it does from its +/// settings. +#[test] +fn a_document_preset_from_an_earlier_run_throws_from_typename_and_to_string() { + let mut s = Session::new(); + run(&mut s, "#targetengine keep\nvar p = new DocumentPreset();").unwrap(); + for member in ["p.width", "p.typename", "p.toString()", "String(p)"] { + let e = failure(&mut s, &format!("#targetengine keep\n{member};")); + assert!(e.contains("TypeError") && e.contains("not a DocumentPreset"), "{member}: {e}"); + } +} + /// The values the presets hold count against the run's heap, a value assigned again in place of /// the one before it. #[test] @@ -1559,6 +1571,23 @@ fn headless_add_document_dialog_fails_without_creating() { assert_eq!(s.documents().len(), 1); } +/// `addDocumentNoUI(startupPreset)` makes the document `addDocument(startupPreset)` makes, and +/// ignores the arguments after the preset's name. +#[test] +fn add_document_no_ui_makes_the_startup_presets_document() { + let mut s = Session::new(); + let out = log( + &mut s, + "var a = app.documents.addDocument('Web'); var p = new DocumentPreset(); p.width = 333;\ + var b = app.documents.addDocumentNoUI('Web', p, true);\ + vc.log([b.typename, b.width === a.width, b.height === a.height, b.width === 333, app.documents.length]);", + ); + assert_eq!(out, vec![json!(["Document", true, true, false, 2])]); + let e = failure(&mut s, "app.documents.addDocumentNoUI('No Such Preset');"); + assert!(e.contains("no startup preset called No Such Preset"), "{e}"); + assert_eq!(s.documents().len(), 2); +} + #[test] fn setting_selected_keeps_how_the_other_objects_are_selected() { let (mut s, a, b) = doc(); @@ -2046,6 +2075,21 @@ fn get_is_file_open_cannot_probe_outside_script_scope() { assert!(failure(&mut s, "app.getIsFileOpen(new Array(5000).join('a'));").contains("too long")); } +/// Outside Windows a `\` is part of a file name: `/a\b.svg` is not the open `/a/b.svg`. +#[test] +fn get_is_file_open_keeps_a_backslash_in_a_unix_file_name() { + let (mut s, _, _) = doc(); + let fixture = vectorcraft_testkit::TempDir::new("script-file-open-backslash"); + let dir = fixture.join("a"); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("b.svg"); + s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap(); + let same = serde_json::to_string(&path.to_string_lossy()).unwrap(); + let backslash = serde_json::to_string(&format!("{}\\b.svg", dir.to_string_lossy())).unwrap(); + let out = log(&mut s, &format!("vc.log(app.getIsFileOpen({same})); vc.log(app.getIsFileOpen({backslash}));")); + assert_eq!(out, vec![json!(true), json!(cfg!(windows))]); +} + #[test] fn a_menu_command_runs_its_command_on_the_selection() { let (mut s, a, b) = doc(); diff --git a/crates/engine/tests/it/script_host_vs_scriptcheck.rs b/crates/engine/tests/it/script_host_vs_scriptcheck.rs index c37d57edac..1b2e879c3a 100644 --- a/crates/engine/tests/it/script_host_vs_scriptcheck.rs +++ b/crates/engine/tests/it/script_host_vs_scriptcheck.rs @@ -108,7 +108,7 @@ const INSIDE: &[(&str, &str)] = &[ toString transparencyGrid transparencyGrid= typename units units= width width=", ), ("Documents.", "prototype"), - ("Documents.prototype.", "add addDocument constructor getByName index length parent toString typename"), + ("Documents.prototype.", "add addDocument addDocumentNoUI constructor getByName index length parent toString typename"), ("Document.", "prototype"), ( "Document.prototype.", diff --git a/docs/mcp.md b/docs/mcp.md index 3b647e4c7b..31309d23f8 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2990,7 +2990,10 @@ through `$`: profile and the reason, and makes no document. Each warning from reading the profile's file (a color profile in it that cannot be used, for example) goes to the Script Console as a line `addDocument: `. A `previewMode` of `DocumentPreviewMode.PixelPreview` throws an error that says it is not available yet, and no document is made. - `showOptionsDialog` true throws `Dialog unavailable` with no document made. A `Document` (https://ai-scripting.docsforadobe.dev/jsobjref/Document/) has `name`, + `showOptionsDialog` true throws `Dialog unavailable` with no document made. `addDocumentNoUI(startupPreset)` + makes the document `addDocument(startupPreset)` makes and ignores any further arguments. A `DocumentPreset` kept + from an earlier run (with `#targetengine`) throws a `TypeError` when its settings are read or assigned and when its `typename` + or `toString()` is read. A `Document` (https://ai-scripting.docsforadobe.dev/jsobjref/Document/) has `name`, `width`, `height`, `rulerUnits`, `documentColorSpace`, `rulerOrigin`, `convertCoordinate(point, source, destination)`, `pageItems`, `selection` and `views`. `views` (https://ai-scripting.docsforadobe.dev/jsobjref/Views/) lists each window, tab, or tiled pane the desktop app has drawn for the document, in view-number order. Each view @@ -3093,7 +3096,9 @@ through `$`: nothing changed. While View › Guides › Lock Guides is on, a path made into a guide cannot be selected this way or with `selected`. Assigning a page item's `selected` leaves the direct-selected anchors and segments of the other selected objects selected. `getIsFileOpen` compares the path with the open documents' files as text (in a Windows path, - letter case is ignored and `\` matches `/`) and does not resolve the path it is given. A document counts only when + letter case is ignored and `\` matches `/`) and does not resolve the path it is given. On Windows every path is a + Windows path; on macOS and Linux only a path that starts with a drive letter is, and in the others `\` is a + character of a file name. A document counts only when the automation read roots allow its file, and that check can look the document's file up on disk. A path with `..`, a network path, a NUL character or more than 4,096 bytes throws an error. `app.redraw()` returns `undefined` and does not change the documents or add an undo step. In the app, the documents are drawn as they were before the