Page 1 of 1

Passing arguments when loading .xys script?

Posted: 18 May 2022 03:38
by WirlyWirly
I feel kinda dumb asking this, I've searched through the docs but I can't figure out how to pass arguments to a .xys script and then reference them within the script.

From the input box at User|Manage Commands...|Load Script File|Script File, I'm trying to load a script using this line: <xyscripts>\ChildLauncher\ChildLauncher.xys "openWithMode"

Once the script is running, I want to check if the argument openWithMode was passed. It will be optional, so the script shouldn't break if the argument wasn't passed. Here's my code so far...

Code: Select all

    // ---------------------------------------------------------------------
    // Check if ChildLauncher was called in openWithMode
    // ---------------------------------------------------------------------

    $openWithMode = "false";

    // Toolbar button: Left-click + Shift
    if (get("trigger", "mousebtn") == 1 && get("trigger", "callshift") == 1) {
        $openWithMode = "true";
    }

    // Argument: ChildLauncher.xys "openwith"
    if ($ARGUMENT_1 LikeI "openWithMode") {
        $openWithMode = "true";
    }

I've got the left-click + shift working for toolbar buttons, but I mostly use keyboard shortcuts. I'd like to create a specific keyboard shortcut that passes "openWithMode" when loading the .xys.

Any tips?

Re: Passing arguments when loading .xys script?

Posted: 18 May 2022 06:13
by highend
Scripts don't support arguments themselves. Use a perm variable that you can eval instead

E.g. what I'm using: Transfer the perm into a local var, unset it so there isn't any perm defined anymore (and can't pollute the remembered perm vars across sessions if somebody uses:
Configuration | General | Refresh, Icons, History | Scripting | [x] Remember permanent variables)

So the caller sets up the perm:

Code: Select all

    perm $P_OPEN_WITH_MODE = true; load "{the script to execute}";
and your receiving script:

Code: Select all

    perm $P_OPEN_WITH_MODE;
    $openWithMode = $P_OPEN_WITH_MODE;
    unset $P_OPEN_WITH_MODE;
    
    if ($openWithMode) { ... }

Re: Passing arguments when loading .xys script?

Posted: 19 May 2022 01:35
by WirlyWirly
Thanks highend, it worked perfectly. I got the exact functionality I wanted.

perm $P_CHILDLAUNCHER_OPENWITHMODE = "true"; load "<xyscripts>\ChildLauncher\ChildLauncher.xys";

Code: Select all

    // ---------------------------------------------------------------------
    // Check if ChildLauncher was called in openWithMode
    // ---------------------------------------------------------------------

    $openWithMode = "false";

    // Called using permanent variable: perm $P_CHILDLAUNCHER_OPENWITHMODE = "true"; load "<xyscripts>\ChildLauncher\ChildLauncher.xys";
    perm $P_CHILDLAUNCHER_OPENWITHMODE;
    if ($P_CHILDLAUNCHER_OPENWITHMODE LikeI "true") {
        $openWithMode = "true";
	}
    unset $P_CHILDLAUNCHER_OPENWITHMODE;

    // Toolbar button: Left-click + Shift
    if (get("trigger", "mousebtn") == 1 && get("trigger", "callshift") == 1) {
        $openWithMode = "true";
    }
Looking at a few other scripts I've downloaded, I see that a few of them are doing the same thing. However, it seems that most aren't using the unset command, which I've just noticed has caused clutter in my permanent variables. Good tip, thanks a bunch!