"global" functions

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
MBaas
Posts: 683
Joined: 15 Feb 2016 21:08

"global" functions

Post 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)
Last edited by MBaas on 10 Jan 2023 14:15, edited 1 time in total.
____________________________________________________________________________________
Happy user. Screen scaling 100% and W11Pro [Version 10.0.26200.7922], XY 28.30.0600 * * LinkTree

highend
Posts: 14926
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: "global" functions

Post 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...
One of my scripts helped you out? Please donate via Paypal

MBaas
Posts: 683
Joined: 15 Feb 2016 21:08

Re: "global" functions

Post 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...
____________________________________________________________________________________
Happy user. Screen scaling 100% and W11Pro [Version 10.0.26200.7922], XY 28.30.0600 * * LinkTree

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: "global" functions

Post 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.

MBaas
Posts: 683
Joined: 15 Feb 2016 21:08

Re: "global" functions

Post 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;
Last edited by MBaas on 11 Jan 2023 16:22, edited 1 time in total.
____________________________________________________________________________________
Happy user. Screen scaling 100% and W11Pro [Version 10.0.26200.7922], XY 28.30.0600 * * LinkTree

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: "global" functions

Post by klownboy »

Thanks MBaas, I'll have to give it a try.

MBaas
Posts: 683
Joined: 15 Feb 2016 21:08

Re: "global" functions

Post by MBaas »

Sorry, there was a silly typo in confirmCMD - I fixed it in the previous post!
____________________________________________________________________________________
Happy user. Screen scaling 100% and W11Pro [Version 10.0.26200.7922], XY 28.30.0600 * * LinkTree

LittleBiG
Posts: 1848
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: "global" functions

Post 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

Post Reply