Page 1 of 1

Show/Hide everything (need help with script)

Posted: 24 Aug 2018 21:20
by John_C
Here are two scripts which will show/hide any hidden files, folders or drives.

Is there a way to combine them somehow? I mean, I press custom button on toolbar and this button will toggle between these two scripts. Is it possible?

Code: Select all

"Show Everything"
    $HideProtectedOperatingSystemFiles = get("#388");
    $ShowHiddenDrives = get("#492");
    $ShowHiddenFilesAndFolders = get("#493");
    $ShowSystemFilesAndFolders = get("#494");

    if ($HideProtectedOperatingSystemFiles == 1) { #388; }
    if ($ShowHiddenDrives == 0) { #492; }
    if ($ShowHiddenFilesAndFolders == 0) { #493; }
    if ($ShowSystemFilesAndFolders == 0) { #494; }

"Hide Everything"
    $HideProtectedOperatingSystemFiles = get("#388");
    $ShowHiddenDrives = get("#492");
    $ShowHiddenFilesAndFolders = get("#493");
    $ShowSystemFilesAndFolders = get("#494");

    if ($HideProtectedOperatingSystemFiles == 0) { #388; }
    if ($ShowHiddenDrives == 1) { #492; }
    if ($ShowHiddenFilesAndFolders == 1) { #493; }
    if ($ShowSystemFilesAndFolders == 1) { #494; }

Re: Show/Hide everything (need help with script)

Posted: 24 Aug 2018 21:36
by highend

Code: Select all

    perm $P_Show_Hide_State; // 1 = hidden, 2 = shown
    if (!regexmatches($P_Show_Hide_State, "(1|2)")) { $P_Show_Hide_State = 1; }

    // Show everything
    if ($P_Show_Hide_State == 1) {
        $HideProtectedOperatingSystemFiles = get("#388");
        $ShowHiddenDrives = get("#492");
        $ShowHiddenFilesAndFolders = get("#493");
        $ShowSystemFilesAndFolders = get("#494");

        if ($HideProtectedOperatingSystemFiles == 1) { #388; }
        if ($ShowHiddenDrives == 0) { #492; }
        if ($ShowHiddenFilesAndFolders == 0) { #493; }
        if ($ShowSystemFilesAndFolders == 0) { #494; }
        $P_Show_Hide_State = 2;

    // Hide everything
    } elseif ($P_Show_Hide_State == 2) {
        $HideProtectedOperatingSystemFiles = get("#388");
        $ShowHiddenDrives = get("#492");
        $ShowHiddenFilesAndFolders = get("#493");
        $ShowSystemFilesAndFolders = get("#494");

        if ($HideProtectedOperatingSystemFiles == 0) { #388; }
        if ($ShowHiddenDrives == 1) { #492; }
        if ($ShowHiddenFilesAndFolders == 1) { #493; }
        if ($ShowSystemFilesAndFolders == 1) { #494; }
        $P_Show_Hide_State = 1;
    }
Alternatively use get("trigger"); and execute either one on left / right clicking
the button...

Re: Show/Hide everything (need help with script)

Posted: 24 Aug 2018 21:44
by John_C
highend wrote:...
Thank you very much :)

Re: Show/Hide everything (need help with script)

Posted: 30 Aug 2018 10:26
by John_C
highend, could you describe what is the purpose of "if (!regexmatches..." line?

Code: Select all

"Show Everything"
    perm $P_Show_Hide_State; // 0 = Hidden, 1 = Shown
    if (!regexmatches($P_Show_Hide_State, "(0|1)")) { $P_Show_Hide_State = 0; }

    // #388 - Hide protected operating system files
    // #493 - Show hidden files and folders
    // #494 - Show system files and folders

    // Show everything
    if ($P_Show_Hide_State == 0) {
        if (get("#388") == 1) { #388; }
        if (get("#493") == 0) { #493; }
        if (get("#494") == 0) { #494; }
        $P_Show_Hide_State = 1;
        ctbstate(1);
    }

    // Hide everything
    elseif ($P_Show_Hide_State == 1) {
        if (get("#388") == 0) { #388; }
        if (get("#493") == 1) { #493; }
        if (get("#494") == 1) { #494; }
        $P_Show_Hide_State = 0;
        ctbstate(0);
    }
And question about coding style. In your scripts, I see that you prefer lowerCamelCase for local variables. But in this example you use Upper_Snake_Case for permanent variable. Is it your general coding style convention:

Code: Select all

localVariable
P_Permanent_Variable
?

Re: Show/Hide everything (need help with script)

Posted: 30 Aug 2018 10:34
by highend
Try this as a script:

Code: Select all

    text "_" . $myVar . "_";
    $myVar = "some value";
    text "_" . $myVar . "_";
In other words: If a variable has NO value at all, it's value is the name of the variable itself
So: That regex is checking if the variable is set to either 1 or 2 and if this is NOT the case, init
it to a default value (you're using an older version of my script which used 0 | 1 values, which
is not an ideal way to check them -> switched them to 1 | 2)

Everyone likes different variable names. $P_... are used for permanent ones. Iirc even XYs help file recommends
this (and I agree on this). You are free to use your own scheme (as long as the names are valid)
$MyVar
$myVar (I like this one)
$My_Var
$my_var
etc.

Re: Show/Hide everything (need help with script)

Posted: 30 Aug 2018 10:59
by John_C
highend wrote:If a variable has NO value at all, it's value is the name of the variable itself
So: That regex is checking if the variable is set to either 1 or 2 and if this is NOT the case, init
it to a default value
Yeah, I understand. But it seems, as I tested, the initial value is always 0.
... 0 | 1 values, which is not an ideal way to check them -> switched them to 1 | 2
But why?

Re: Show/Hide everything (need help with script)

Posted: 30 Aug 2018 11:01
by highend
It's 0 because the way to check for it isn't a viable way (it will always be reinitialized to 0 because of the return value of regexmatches). Because of that I switched it to 1/2

Re: Show/Hide everything (need help with script)

Posted: 30 Aug 2018 17:25
by klownboy
Hi highend, when establishing the initial state of the permanent variable $P_Show_Hide_State, couldn't we use SC isset in this case? Of course we can't initialize the perm variable before the use of isset check since the result would always be "1", but you could set it perm afterwards. If everything works as it should, the perm variable check is only done the first time the script is used. For the very first run you don't really care if it's 1 or 2 only that it's not 1 or 2. Something like the following works (but you prefer using regex :) ):

Code: Select all

    if(! isset($P_Show_Hide_State)) {$P_Show_Hide_State = 1; }
    perm $P_Show_Hide_State;  // 1 = hidden, 2 = shown

Re: Show/Hide everything (need help with script)

Posted: 30 Aug 2018 17:36
by highend
Hi Ken,

sure, this would work. But I'm additionally initializing the variable to a default value if it isn't set correctly at all (but still set) in a single step.
That's why I prefer "my" way :mrgreen: