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.
- Download: SmartGetSizes_Handler (vb).osp (4.03 KB)
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 ... 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!