Page 1 of 1

"global" functions

Posted: 10 Jan 2023 13:31
by MBaas
Maybe what I want to do is not possible - or maybe I'm over-complicating things. Thought I'd tell you my story ;)

I have some file associations that I'd like to run with variations. Ok, I can duplicate the command and create an alternative entry.
But that slowly fills up the menu...

So I thought "wouldn't it be nice if I could edit the command before it is executed"? In other words: most of my CFA are >run something where something can be any expression. So my simply idea was to do >run confirmCMD(something) where confirmCMD is a user-defined function that takes a string variable as argument and returns a string.

So I wrote a basic prototype for said function:

Code: Select all

function confirmCMD($cmd) {
   text $cmd;
}
and stored it in Scripts\confirmCMD.xys and prefixed the specific CFA with load confirmCMD;

That errors with a message "Script file .. does not contain any valid lines". I added a line "$x=0;" and that removes the err - but the CFA behaves differently now: I am prompted to confirm a message "0" and then the current folder opens in Explorer. I guess it is not possible to run multiple commands.

A solution to this would be to put the whole command into a separate script and simple load that as part of the CFA.
That would, however, lose the "central maintenance" of CFAs, as I'd have CFAs and scripts to look after then :(

Is there a way I could make the function "confirmCMD" easily available to all scripts? (I thought adding /script= to the commandline might do it, but the function was not available afterwards)

Re: "global" functions

Posted: 10 Jan 2023 13:59
by highend
Just don't use a parameter for that function but define a perm variable (before) and check that inside the function? Apart from the "fact" that your function uses %cmd, not $cmd...

Re: "global" functions

Posted: 10 Jan 2023 16:23
by MBaas
Thanks highend! I got it working w/o perm (less pollution) and also managed to pass the argument and result - no idea what why it didn't work before...

Re: "global" functions

Posted: 10 Jan 2023 16:56
by klownboy
Hey MBaas, that sounds interesting. Any chance you could give us some more details on how you got that to work and maybe an example. Thanks.

Re: "global" functions

Posted: 11 Jan 2023 11:05
by MBaas
Sure :)

Here's Scripts\confirmCMD.xys:

Code: Select all

function confirmCMD($e) {
    $c = input("Edit command","",$e,"s","cancel");
    end $c == "cancel";
    return $c;
}
$x=0;  // a valid line to avoid the issue about this script not containing any valid lines. Maybe someone has an idea how to avoid this in a smarter way?
And it is called with this CFA:

Code: Select all

ext>::load "confirmCMD"; $cmd = "MyLongCommand"; $cmd = confirmCMD($cmd);  run $cmd;

Re: "global" functions

Posted: 11 Jan 2023 14:39
by klownboy
Thanks MBaas, I'll have to give it a try.

Re: "global" functions

Posted: 11 Jan 2023 16:23
by MBaas
Sorry, there was a silly typo in confirmCMD - I fixed it in the previous post!

Re: "global" functions

Posted: 12 Jan 2023 15:08
by LittleBiG
MBaas wrote: 10 Jan 2023 13:31 Is there a way I could make the function "confirmCMD" easily available to all scripts? (I thought adding /script= to the commandline might do it, but the function was not available afterwards)
I had the very same wish back then (the only difference was that I wanted to use it differently), but I got no answer.
viewtopic.php?t=25164