From f341cb5b0d8834da90d684dfe1df076c39577a60 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 15:22:31 -0800 Subject: [PATCH] Scripts: a chosen script keeps script.run's reply with startup scripts on With Run Startup Scripts on, ui.runScript and ui.runInstalledScript return script.run's reply, or its error when the chosen script cannot start, with the startup run's reply under startup. While a startup script runs first, the reply is {running: true, queued: true}. --- crates/mcp/src/ui_catalog.rs | 4 +-- crates/ui-egui/src/menus/ui_commands.rs | 4 +-- crates/ui-egui/src/script_runs.rs | 22 ++++++++++++++-- crates/ui-egui/src/tests/scripts_menu.rs | 33 ++++++++++++++++++++++++ docs/mcp.md | 5 +++- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 18d0ebc247..82e0fc6489 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -2022,13 +2022,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "ui.runScript", "Other Script…", "Cmd+F12", - "{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker)", + "{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker); with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar", ), ( "ui.runInstalledScript", "Run Script", "", - "{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again", + "{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again; with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar", ), ( "ui.checkScript", diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index f67f316f5f..8ebfe0d9c6 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -2023,13 +2023,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "ui.runScript", "Other Script…", "Cmd+F12", - "{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker)", + "{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker); with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar", ), ( crate::scripts_menu::COMMAND, "Run Script", "", - "{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again", + "{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again; with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar", ), ( "ui.checkScript", diff --git a/crates/ui-egui/src/script_runs.rs b/crates/ui-egui/src/script_runs.rs index 5b5ffc099b..d9c9960ac0 100644 --- a/crates/ui-egui/src/script_runs.rs +++ b/crates/ui-egui/src/script_runs.rs @@ -124,12 +124,30 @@ fn ask(app: &mut VectorcraftApp) -> bool { /// the startup scripts when General › Run Startup Scripts is on (`script.startup.run`). A startup /// script that cannot start, a listing of the Startup Scripts folder that was not read in full, and /// a folder that is there and cannot be read show in the status bar ([`reply_notes`]). +/// → `script.run`'s reply, with `startup` holding `script.startup.run`'s when the startup scripts +/// were run: the chosen script's own reply when it ran to its end at once, `{running: true, name}` +/// when it is running, or `{running: true, queued: true}` while a startup script runs before it. +/// A chosen script that could not start gives its error, as `script.run` does. pub(crate) fn run_chosen(app: &mut VectorcraftApp, params: Value) -> Result { if app.session.startup_skip().is_some() { return app.run("script.run", params); } - let reply = app.run("script.startup.run", json!({"reason": "menu", "then": params}))?; - reply_notes(app, &reply); + let mut startup = app.run("script.startup.run", json!({"reason": "menu", "then": params}))?; + reply_notes(app, &startup); + let mut reply = match startup.as_object_mut().and_then(|o| o.remove("then")) { + Some(then) => then, + // Nothing runs and the chosen script gave no reply: it could not start, and it is the + // last of the runs that could not start. + None if !app.session.script_running() => { + let last = startup["failed"].as_array().and_then(|failed| failed.last()); + return Err(last.and_then(|failed| failed["error"].as_str()).unwrap_or_default().to_string()); + } + None if app.session.startup_script_running() => json!({"running": true, "queued": true}), + None => json!({"running": true, "name": app.session.script_name().unwrap_or_default()}), + }; + if let Some(o) = reply.as_object_mut() { + o.insert("startup".to_string(), startup); + } Ok(reply) } diff --git a/crates/ui-egui/src/tests/scripts_menu.rs b/crates/ui-egui/src/tests/scripts_menu.rs index ce1dfdfae8..e0604081fd 100644 --- a/crates/ui-egui/src/tests/scripts_menu.rs +++ b/crates/ui-egui/src/tests/scripts_menu.rs @@ -525,6 +525,39 @@ fn with_run_startup_scripts_on_other_script_and_the_launch_run_the_startup_scrip let _ = std::fs::remove_dir_all(dir); } +#[test] +fn with_run_startup_scripts_on_a_chosen_script_still_replies_as_script_run() { + let dir = folder("startup-reply"); + let mut app = app(&dir); + app.session.prefs.show_external_script_warning = false; + let off = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap(); + finish(&mut app); + assert_eq!(off, json!({"running": true, "name": "chosen"})); + let empty = dir.join("Startup Scripts"); + std::fs::create_dir_all(&empty).unwrap(); + app.session.attach_startup_scripts_dir(empty); + app.run("prefs.set", json!({"key": "runStartupScripts", "value": true})).unwrap(); + // An empty folder: the chosen script starts at once, named as its run is, by a #script line too. + let r = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap(); + finish(&mut app); + assert_eq!((&r["running"], &r["name"], &r["startup"]["queued"]), (&json!(true), &json!("chosen"), &json!(1)), "{r}"); + let named = format!("#script \"Named\"\n{DRAW}"); + let r = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": named })).unwrap().unwrap(); + finish(&mut app); + assert_eq!(r["name"], "Named", "{r}"); + // A chosen script that cannot start gives its error, as script.run does. + let alone = app.run("script.run", json!({"source": "vc.log(("})).unwrap_err(); + let e = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": "vc.log((" })).unwrap().unwrap_err(); + assert_eq!(e, alone); + // A startup script runs first: the chosen script is queued after it. + startup_folder(&mut app, &dir); + let r = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap(); + finish(&mut app); + assert_eq!((&r["running"], &r["queued"], &r["startup"]["queued"]), (&json!(true), &json!(true), &json!(2)), "{r}"); + assert_eq!(started(&app).last().map(String::as_str), Some("chosen")); + let _ = std::fs::remove_dir_all(dir); +} + #[test] fn a_stopped_startup_script_says_it_was_stopped_and_the_chosen_script_still_runs() { let dir = folder("startup-stop"); diff --git a/docs/mcp.md b/docs/mcp.md index 56557457bb..8311998f81 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3311,7 +3311,10 @@ reply is `{reason, skipped: false, running, queued, failed, disabled, truncated, the runs that could not start before the first one did. Each run then reports as a `script.run` in the app does, the next one starts at the frame after, and a later run that cannot start, the chosen script included, is named in the status bar. Stop (`script.stop`) asked for between two runs stops the run that starts next. Under `--automation-read-root` it reads the folder only when the folder lies inside a -read root. +read root. With the preference on, `ui.runScript` and `ui.runInstalledScript` still return `script.run`'s reply, +or its error when the chosen script cannot start, and add `startup` holding the `script.startup.run` reply. While a +startup script runs before the chosen script, that reply is `{running: true, queued: true}`, and a chosen script that +then cannot start is named in the status bar. ```sh vectorcraft-cli run --cmd script.startup.run --params '{"reason": "launch"}'