Get source tab from script run by a global hotkey?

Every tab object is returning true for it's source property.
To reproduce:

  1. save this code as an add-in script
  2. set a global hotkey to run the script
  3. make any window other than a Directory Opus window the foreground window
  4. activate the hotkey
function OnInit(initData) {

	initData.name = "Test Source Tab";

	var cmd = initData.AddCommand();
	cmd.name = "testSourceTab";
	cmd.method = "OnTestSourceTab";
}

function OnTestSourceTab(scriptCmdData) {
		
	DOpus.ClearOutput();
	
	for (var eListers = new Enumerator(DOpus.listers); !eListers.atEnd(); eListers.moveNext())
	{
		for (var eTabs = new Enumerator(eListers.item().tabs); !eTabs.atEnd(); eTabs.moveNext())
		{
			if (eTabs.item().source) {
				DOpus.Output("Source tab is:  " + eTabs.item().path);
			}
		}
	}
	
}

The tab.source property tells you if a tab is on the source side of a lister.

Test tab.visible as well to only get the active tab on the source side:

function OnTestSourceTab(scriptCmdData)
{
	DOpus.ClearOutput();
	for (var eListers = new Enumerator(DOpus.listers); !eListers.atEnd(); eListers.moveNext())
	{
		for (var eTabs = new Enumerator(eListers.item().tabs); !eTabs.atEnd(); eTabs.moveNext())
		{
			var tab = eTabs.item();
			if (tab.source && tab.visible) {
				DOpus.Output("Source tab is:  " + eTabs.item().path);
			}
			else {
				DOpus.Output("Other tab is:  " + eTabs.item().path);
			}
		}
	}
}

Although you can get that more directly, without looping through all the tabs:

function OnTestSourceTab(scriptCmdData)
{
	DOpus.ClearOutput();
	for (var eListers = new Enumerator(DOpus.listers); !eListers.atEnd(); eListers.moveNext())
	{
		DOpus.Output("Source tab is:  " + eListers.item().activetab.path);
	}
}

But, even then, there still may be more than one, since you might have more than one window open.

If you want the singular last active tab, use DOpus.listers.lastactive.activetab instead:

function OnTestSourceTab(scriptCmdData) {
		
	DOpus.ClearOutput();

	DOpus.Output("Last active tab is:  " + DOpus.listers.lastactive.activetab.path);
}

(Of course, a real script should check DOpus.listers.lastactive first, in case no windows are open.)

1 Like