Scripts (JS & VBS) Snippet: Enumerating listers and tabs

The scripting API returns collections of objects in various places. For example, DOpus.listers is a collection containing a lister object for each lister that is open.

This post provides examples of how to enumerate those collections in JScript and VBScript.

The two scripts below do the same thing: Print the path of each folder tab in each lister. The only difference between the scripts is the language they are written in.

JScript:

DOpus.Output("These tabs are open:");
DOpus.Output("");
for (var eListers = new Enumerator(DOpus.listers); !eListers.atEnd(); eListers.moveNext())
{
	for (var eTabs = new Enumerator(eListers.item().tabs); !eTabs.atEnd(); eTabs.moveNext())
	{
		DOpus.Output("  " + eTabs.item().path);
	}
}

VBScript:

Option Explicit
Dim lister
Dim tab
DOpus.Output "These tabs are open:"
DOpus.Output ""
For Each lister In DOpus.listers
	For Each tab In lister.tabs
		DOpus.Output "  " & tab.path
	Next
Next


Removing objects while they are being enumerated:

To avoid unexpected behaviour in your scripts, be careful not to remove objects from the collection(s) you are enumerating.

For example, if you wish to go through each lister or tab and close particular ones, you should make a note of all the objects you wish to close, and finish enumerating, before you start closing them.

One way to do this is to create your own Vector and store the objects you wish to close in it. (It's safe to enumerate a Vector you created yourself in this way, unlike the collections which Opus gives to you.)

For a real example of this, see this script: Close all tabs matching a path wildcard.

1 Like