SmartGetSizes_Handler: Toggle automatic folder size calculation

Requires Opus version 11.16 or above.

This was as much an excuse to share an example of what could be done using the cool new scripting interfaces in Opus v11 as it was to provide something of potential use to others. It came from discussions in several other threads:

What does it do?:

This solution is an alternative to using the 'Set CALCFOLDERSIZES' command to toggle the behavior of the 'Calculate folder sizes automatically' option in Prefs. The main difference being that this script-based method can be lister (or tab) specific (only affects the particular lister or tab where you enable it) whereas the 'Set CALCFOLDERSIZES' command affects ALL listers. It does NOT simply toggle the Prefs option mentioned above... but rather looks for one of the required Opus environment variables to be set, and if so - runs the 'SmartGetSizes' command.

How to install and use it?:

This script based solution comes in two parts (both of which are required to make this solution work):

The first part is a toolbar button intended to be used as a 'toggle' button that turns the behavior of the script that does all the work 'on' and 'off'.

The second, is a Script Add-in package (.OSP file) that does all the actual 'work'. In turn, the script package itself actually contains two separate scripts...

  • The first script (SmartGetSizes_Handler.* inside the OSP file) does the work to enable automatic size calculation for folders when you toggle the button 'on'. It does so by responding to folder change events generated by Opus every time you open a new folder, a new tab, tabs saved in lister layouts, etc (this event is also generated when you perform a refresh of the current folder).

  • The second script (SmartGetSizes.* inside the OSP file) assists the first script in actually performing the size calculation using a new command ('SmartGetSizes') that this second script adds to Opus. The first script could have just as easily done all the work by itself, but this was a good excuse to show another aspect of the Opus scripting interface which allows a script such as this one to add a command to Opus. Such commands can then be used like any other Opus internal commands directly in toolbar buttons, hotkeys, etc. In fact, as further example of this - the command this script adds ('SmartGetSizes') is used in the toolbar button provided below that toggles the auto size behavior. It uses the command in order to calculate the folder size of all folders in the current path when you toggle it 'on', regardless of whether or not any selected folders have already had their size calculated or not.

    Note: credit to Jonathan Potter, who wrote the working code that inspired this second script as an early example of a script function to solve the needs of the user in the second discussion referenced above...

The toolbar button:

Which you should copy and paste directly to your toolbar.

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>AutoSize</label>
	<icon1>#getsizes</icon1>
	<function type="normal">
		<instruction>@toggle:if $lst:autosize</instruction>
		<instruction />
		<instruction>@ifset:$lst:autosize</instruction>
		<instruction>@set lst:autosize</instruction>
		<instruction />
		<instruction>@ifset:else</instruction>
		<instruction>@set lst:autosize=true</instruction>
		<instruction>SmartGetSizes GETALL NODESELECT</instruction>
	</function>
</button>

The Script Package:

Install the script package (.osp file) either by manually copying it to the /dopusdata\Script AddIns folder, or via drag-and-drop into the Preferences / Toolbars / Scripts page.

Notes:

  • 2014-01-21: Initial release...
  • 2014-03-19: v1.0.2 - both the JScript and VBScript versions were updated. The update adds the option to use a tab local variable (src:autosize) rather than just a lister wide variable (either will work)... per request from tbone.
  • 2014-03-26: v1.0.4 - script updated, added support for using a global (glob:autosize) variable to control the script. Also settled on a consistent method of enabling debug via a global SCRIPT_DEBUG variable.
  • 2014-06-05: v1.0.5 - the VBScript version was updated. The update fixes a missing variable declaration noticed by qiuqiu in another forum thread (thanks again).
  • 2015-12-27: v1.2.0 - scripts renamed to match my new standard, also added a DEBUG_CLEAR option to clear the output log between script runs, and minimum required Opus version is now set to 11.16.
  • 2016-11-22: v1.2.2 - Fixed an issue with an incorrect variable name and logic error when one or more selected folders did not have it's size already calculated. NOTE: that with this latest version, I am only maintaining the VBscript version. However, I've left the previous Jscript version 1.2.0 posted for posterity.

If you're only interested in using the scripts and not modifying them, YOU ONLY NEED TO COPY ONE OF THE VERSIONS TO THE SCRIPT ADDINS FOLDER... they each provide the same functionality. I will post a video showing the installation of this script and related button, as well as show examples of the functionality for clarity.

How does it work?:

If you are in fact interested in modifying the script to make further enhancements, well then you have your pick of whether to do so in either JScript or VBScript. Enjoy and share :slight_smile:... You can rename the OSP files to ZIP and extract the contents, which will show you the two individual scripts I mentioned above. Even if you don't necessarily want to make enhancements, with both natively supported scripting languages represented by the two versions provided here, my hope is that it (along with the examples provided so far by GPSoft and other users) will be of some value to others looking to develop their own scripting skills in order to take advantage of the awesome capabilities GPSoft are bringing to us in v11. This example makes use of the following new features in the v11 BETA:

  • Toolbar Button: the ability to set different types of variables available across a variety of contexts or scopes (tab, lister, global, etc) using the @set directive.
  • Toolbar Button: the ability to then test for the presence of @set variables using the @ifset directive.
  • Toolbar Button: using a new Opus internal command added by the second script in the OSP file(s).
  • SmartGetSizes.*: an example of a Script Add-in written to add a new Opus internal command ('SmartGetSizes').
  • SmartGetSizes_Handler.*: an example of a Script Add-in written as an 'Event Handler'; with this script registering to be called in response to the OnAfterFolderChange event fired by Opus.
  • Both scripts: make use of the ScriptInitData.config. implementation to provide a means for enabling and disabling a script DEBUG flag via the Configure button for the script in Prefs. The flag is used by each script to control whether or not debug logging is sent to the Opus Output Window.

...the scripts are heavily commented with more details on how they work for those of you interested in such detail. I may follow-up this post with some additional comments breaking down the script functionality for any who are interested.

Cheers to all who may find this useful, and MAJOR thanks to GPSoft for the significant enhancements in v11 that make these sorts of things possible!

1 Like

For Users:

Well... Beta 5 was released mere hours after I posted this with an enhancement to allow just this! Show-offs ( :blush: - thanks ).

Root post updated with new button code to take advantage of the enhancement to highlight the button when used to toggle the AutoSize feature on!

Steje

Nice. I modified your button to make it a 3-way with your command on the LMB and a relative size toggle on the RMB, making use of the SORTBY +SomeColumn functionality added in Beta 4.

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none" type="three_button">
	<label>AutoSize (Steje)</label>
	<icon1>#getsizes</icon1>
	<button backcol="none" display="both" label_pos="right" textcol="none">
		<label>AutoSize</label>
		<icon1>#getsizes</icon1>
		<function type="normal">
			<instruction>@toggle:if $lst:autosize</instruction>
			<instruction />
			<instruction>@ifset:$lst:autosize</instruction>
			<instruction>@set lst:autosize</instruction>
			<instruction />
			<instruction>@ifset:else</instruction>
			<instruction>@set lst:autosize=true</instruction>
			<instruction>SmartGetSizes GETALL NODESELECT</instruction>
		</function>
	</button>
	<button backcol="none" display="both" textcol="none">
		<label>Relative Size</label>
		<icon1>#getsizes</icon1>
		<function type="normal">
			<instruction>@ifset:SORTBY=disksizerel</instruction>
			<instruction>Set COLUMNSTOGGLE=disksizerel(2)</instruction>
			<instruction>Set SORTBY=name</instruction>
			<instruction />
			<instruction>@ifset:else</instruction>
			<instruction>Set SORTBY=+disksizerel(2)</instruction>
			<instruction>SmartGetSizes GETALL NODESELECT</instruction>
		</function>
	</button>
</button>

Regards, AB

1 Like

Cool AB, glad someone else is checking it out :slight_smile:...

Update Note:

I just updated the root post again with a minor update to each version of the Script Package... mainly a nod to the gracious enhancements delivered by GPSoft in Beta 5:

  • Updated all scripts to use the ScriptInitData.version property added in B5 to show the script version info next to their listings in Preferences / Toolbars / Scripts.
  • Updated all scripts to replace the use of the global DEBUG flag I had used in each to control debug logging, to the more useful ScriptInitData.config. provision already included in the earlier betas that allowed configuration of script options using the 'Configure' button for scripts under their listings in Preferences / Toolbars / Scripts. Using a gloal var was fine, but in the case of delivering scripts inside an Opus Script Package, not very practical to allow users easy access to turn on logging...
  • Updated the OnAfterFolderChange_smartgetsizes.* scripts comments to reflect the changes previously made to the button code to take advantage of the @toggle enhancement also in B5.

Hello, I'm probably the only one experiencing this but I get this error. Can someone please tell me what I'm doing wrong. I setup the toggle button.

Are you using Opus v11 Beta, and did you install the one of the script packages?

I modified the comments in the root post to make it more clear that BOTH parts (the button AND one of the script packages) are both required for things to work.

The button simply toggles an Opus environment variable and executes the command added by the script... If you don't install the script package, that 'SmartGetSizes' command doesn't exist.

Thank you. I thought it was one method over the other. Didn't realize it still needed the script package installed. Works great now!

Great.... feedback welcome and appreciated.

I tested it, & it works! Cool :sunglasses: . maybe i leave the auto calcultae for my big images collection off, since it takes quite a time after booting to calculate.
Now i have defined two buttons (for AB's version), to calculate the sizes on demand, without having to fiddle with the prefs/save for tab & layout etc.

If this is working for all internal settings/commands in the prefs, without having to nag GP to provide another seldomly needed raw command,
that would be great - uhm, given, that anyone is so kind to code it for us. :smiley:

I'm really looking forward to see, what else those scripts are able to do. Very promising! :thumbsup:

Keep in mind that GPSoft did indeed add a raw Set command to toggle the auto size option on/off. What mine has going for it is that it is lister specific (and if anyone had interest, we could fairly easily add a configuration option to make it TAB specific as well).

Besides that, the example button I provided makes use of my ScriptCommand variation on Jon's 'SmartGetSizes' command so that you also get the folder sizes calculated in the folder you're in at the time you press the button to toggle auto-size on.

If anyone is interested in a Tab-specific option for my version here - shout... and I'll update the script package.

But about being able to toggle all Prefs options, my script version is really just "simulating" this. It's not actually toggling the option, just using a new Opus 11 environment variable in the 'button' portion to control whether or not the 'script' potion then issues the 'SmartGetSizes' command or not. GPSoft haven't actually provided any sort of script specific interface for playing with the options in Prefs :slight_smile:. But as is usually the case, just as they implemented the 'Set CALCFOLDERSIZES' command in response to the same user that inspired me to write this script version - I expect they will continue to entertain enabling other Prefs options via raw commands as people ask, so long as they see the value in doing so. Doing all the work to toggle EVERY prefs option available doesn't really make sense unless they're all out of ideas for new features :wink:.

I always use dual listers with several tabs opened and I would not want all of them to be size-calculated if I toggle automatic folder size calculation.
So this is the shout for that tab-specific option.. o) Thx.. o)

@aussieboykie
Your version of the toggle button makes my toolbar grow somewhat in v-size (about 20pixels), while stejes original does not, even both show the same text and icon.

Any idea what is causing this?

EDIT:
It seems, the button being a "3 button" type made it higher somehow. I don't know what type is it now, but it is even sized now, maybe useful for anybody - visual toggling seems to be enhanced as well.

<?xml version="1.0"?>
<button backcol="none" display="icon" dropdown_glyph="yes" label_pos="right" textcol="none" type="menu_button">
	<label>AutoSize</label>
	<tip>Auto-size sub folders</tip>
	<icon1>#getsizes</icon1>
	<function type="normal">
		<instruction>@toggle:if $lst:autosize</instruction>
		<instruction />
		<instruction>@ifset:$lst:autosize</instruction>
		<instruction>@set lst:autosize</instruction>
		<instruction />
		<instruction>@ifset:else</instruction>
		<instruction>@set lst:autosize=true</instruction>
		<instruction>SmartGetSizes GETALL NODESELECT</instruction>
	</function>
	<button backcol="none" display="both" textcol="none">
		<label>Relative Size</label>
		<icon1>#getsizes</icon1>
		<function type="normal">
			<instruction>@ifset:SORTBY=disksizerel</instruction>
			<instruction>Set COLUMNSTOGGLE=disksizerel(2)</instruction>
			<instruction>Set SORTBY=name</instruction>
			<instruction />
			<instruction>@ifset:else</instruction>
			<instruction>Set SORTBY=+disksizerel(2)</instruction>
			<instruction>SmartGetSizes GETALL NODESELECT</instruction>
		</function>
	</button>
</button>

@tbone:

The root post was updated with a new version of each script package that adds a check for $src:autosize... so the script can now work off of both lister and source tab specific variables. See what you think... And since you mentioned dual display listers, if someone thinks they might want linked tabs or dual-display tabs not linked but running under Navlock enabled, let me know if you want an additional change to have both sides auto-size. I' haven't tested the script in dual-display mode yet... not sure what will happen, but I expect it will work with the LISTER variable toggle button, but I think some more work might be needed to get it to to auto-size both sides of the lister with the new source tab option.

About your other question, the reason the toolbars vertical height increases with AB's triple-button is because the second child-button (Relative Size) has it's label state set to Default. Setting it to show on the Right will fix it. This happens to be related to a pet peeve of mine :wink:.

Thanks Steje.. o) I tried your update, but it feels like its working as before? I mean, it still affects all tabs opened. And to be honest, I did not get your description of this "$src:autosize" thing.
You probably mean the status variable for auto-mode to be tab-scoped now, but where has this been added? Can't find it in the scripts, nor the button - or is this something I need to add? Just trying to understand.. o)

Well, yeah... that "$src:autosize" thing is pretty key to making it work how you asked, so - pretty important :slight_smile:. In short, you'll want to use a button like this:

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>AutoSize</label>
	<icon1>#getsizes</icon1>
	<function type="normal">
		<instruction>@toggle:if $src:autosize</instruction>
		<instruction />
		<instruction>@ifset:$src:autosize</instruction>
		<instruction>@set src:autosize</instruction>
		<instruction />
		<instruction>@ifset:else</instruction>
		<instruction>@set src:autosize=true</instruction>
		<instruction>SmartGetSizes GETALL NODESELECT</instruction>
	</function>
</button>

The original script only looked for a LISTER scoped variable called autosize to be set. The change to the script was to check for a source TAB scoped variable of the same name (src:autosize - set in the button above). Either will now work... but the change to the script necessitates a change to the toolbar button used to toggle the script behavior.

So, the button above is the same as my original example button, just with the lst:autosize stuff swapped out for the newly supported src:autosize variable. I suppose I could have been more clear on what else needed to be done :wink: ...

It was just yesterday that I digged those scope prefixes - I think I got them now.
Excuse: The manual has over 900 pages! Did anybody print it? o)

With script you meant that button-code, right? Is there some guide line on how to call what? o) Is this a button-code, a command, a button-script? Just wondering. o)

To sum up: Now everything is clear, thanks for being very detailed and supplying the button once again!

I had edited the post to make the needs of this solution hopefully a bit more clear. The script and the toolbar button work TOGETHER.

The script portion needs some way of recognizing that you want it to do it's intended job for behavior that you want to turn on and off in an "on demand" fashion... Doing that by enabling/disabling the script via Prefs to accomplish this would be ridiculous. Instead, the most convenient way to do it is to give the user a button they can click just like any other button that turns things on and off in Opus...

So with that, the button needs to do something that the script can pick up on and recognize. That something is the toggling of an environment variable (new to v11).

The original script only checked to see if the button toggled a LISTER scope variable (designated as a LISTER scoped variable by the use of lst: as a prefix to the variable name)... so the first example button was written to do so.

The change to the script was to ALSO check for a source TAB scoped variable (designated in the button as a source TAB scoped variable by the new use of a src: prefix to the variable name).

Yeah, the help file is big :wink:... but keep in mind that work began on this during the beta trials... so the info on the new features used in this script were a bit more concisely presented (in the Beta announcement) :wink:.