Page 1 of 1

Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 00:04
by Jerry
[XYplorer 64 Using 28.00.0300]

Puzzled by this one. I have a toolbar button with the On left-click field set to load a script (load "myscript.xys"). When I press the button, it just displays the script (with variables assigned) in a popdown rectangle instead of doing the action of the script. I have other scripts bound to buttons that work just fine. I assume there's something wrong with the scripting code? But why does it show the script instead of running it? See below. All it does is that if I selected a html file, it looks for and also selects the corresponding _files folder as created when you save a complete web page from chrome.


// Get the base name of the selected file
$name = <curbase>;

// Build the path to the corresponding _files folder
$filesFolder = "<curpath>\" . $name . "_files\";

// Check if the _files folder exists
if (exists($filesFolder) == 2) {
// Select both the HTML file and the _files folder
selectitems <curitem> . "|" . $filesFolder;
}

Re: Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 00:20
by PeterH
All non-comment lines should be indented.

Re: Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 00:27
by Jerry
PeterH wrote: 17 Oct 2025 00:20 All non-comment lines should be indented.
Ah, ok that fixed it. But I have other scripts where at least the first line is not-indented. So the rule is that the first line can be regular but all others must be indented?

Re: Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 01:00
by WirlyWirly
Like Peter said, when writing a multi-line .xys script, everything after the first line of code (except comments) needs indenting.

If you don't indent each line, XY will interpret each individual line of code as if it were a stand-alone command, which is why you're getting that popupmenu for you to pick which of those commands (lines) you want to run. Indenting everything tells the interpreter that this whole file is a single script, by that logic no popupmenu is generated and the code is just executed as expected.

Basically, XY requires each script in a file to be indented like a function. The benefit of this is that it allows you to create a powerful popupmenu that contains various different scripts for you to pick from. Try this for example...

Code: Select all

// Script #1
"Say hello XY!"
    msg "Hello %user%!";

// Script #2
"Name that File!"
    $name = <curbase>;

    // Build the path to the corresponding _files folder
    $filesFolder = "<curpath>\" . $name . "_files\";

    // Check if the _files folder exists
    if (exists($filesFolder) == 2) {
    // Select both the HTML file and the _files folder
    selectitems <curitem> . "|" . $filesFolder;
Personally, I'm not a fan of this, I feel that relying on indention to differentiate between a popupmenu and a multi-line script is confusing, especially when you're just starting off. Using a Shebang such as #!xymulti to signify this file is a single script is the way I'd have gone.

Re: Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 01:15
by Jerry
Ok, now that I got that, things are clear. I WAS confused because other scripts I use don't indent the first line, and I didn't read anything into the other lines being indented.

Re: Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 11:46
by PeterH
As WirlyWirly explained :tup:

Maybe you should start reading a bit from Help [F1]
There: Advanced Topics / Scripting

The first few pages explain basics about script files ...

Later on you can read parts you think you need,
and (!) read (and understand) scripts of other people.
It's a never ending process :maf:
But it's worth it, for :idea: :idea: :idea:

Re: Pressing toolbar button with script just shows the script, doesn't run it

Posted: 17 Oct 2025 15:13
by Jerry
PeterH wrote: 17 Oct 2025 11:46 Maybe you should start reading a bit from Help [F1]
Actually, these days it's easier to have an AI engine write the script for you, which is how I came up with this particular script.Unfortunately, it didn't know about the indenting, but otherwise after that minor correction, the script works fine.

But yes, I agree that spending a little time learning the basics of XY scripting is a good idea.