Include folder labels in tabs

Requires 12.3 or above.

Here's a small script add-in that will add any labels (in the "Tabs" category) from parent folders to the tab text.

Examples:

  • The C:\Temp folder has a label of "Hello":

    C:\\Temp\\Abc\\Def would show "Hello\Def"

    C:\Temp\Abc\Def\Pqr would show "Hello\Pqr"

  • The C:\Users\Fred\Documents folder has a label of "Docs", and its subdirectory Visual Studio 2015 has a label of "VS":

    C:\Users\Fred\Documents\Visual Studio 2015\Projects\MyTestApp would show "Docs\VS\MyTestApp".

  • You have Label Assignments set to give any folder where Name matches *Dropbox* a label of '#':

    D:\odrive\Dropbox (Home)\MyFiles\SomeFolder\Photos would show "Dropbox (Home)\Photos".

    D:\odrive\Dropbox (Work)\Business\Purchases\Invoices would show "Dropbox (Work)\Invoices".

Note the use of the special label '#', which uses the folder name as-is instead of the label name, and is perfect for Label Assignments.

Only labels in the "Tabs" category will be added to the start of your tab labels!

Please let me know what you think, and any suggestions for the code, as it's my first addin :slight_smile:

Download:

Script code:

(This is the same as the download above.)

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "PrefixTabsWithLabels";
	initData.version = "1.1";
	initData.copyright = "(c) 2016 Tenebrous";
	initData.url = "https://resource.dopus.com/viewtopic.php?f=35&t=28208";
	initData.desc = "Enhance tab names by including labels set against parent folders.";
	initData.default_enable = true;
	initData.min_version = "12.2.6";
}

// Called when a tab is activated
function OnActivateTab( activateTabData )
{
  Update( activateTabData["new"] );
}

// Called when the source and destination are changed
function OnSourceDestChange(sourceDestChangeData)
{
    if( sourceDestChangeData.source )
       Update( sourceDestChangeData.tab );
}

// Called after a new folder is read in a tab
function OnAfterFolderChange( afterFolderChangeData )
{
    if ( !afterFolderChangeData.result )
        return;

    Update( afterFolderChangeData.tab );
}

function Update( tab )
{
    try
    {
        PerformUpdate( tab );
    }
    catch( e )
    {

    }
}

function PerformUpdate( tab )
{
    var path = tab.Path;
    var tabLabel = DOpus.FSUtil.GetItem(path).name;
    path.Parent();

    // DOpus.Output( "Colors for " + path );
    // var item = DOpus.FSUtil.GetItem(path);
    // for( var label = new Enumerator(item.Labels("Colors")); !label.atEnd(); label.moveNext() )
    // {
    //     DOpus.Output( "  " + label.item() );
    // }

    // DOpus.Output( "Status for " + path );
    // var item = DOpus.FSUtil.GetItem(path);
    // for( var label = new Enumerator(item.Labels("Status")); !label.atEnd(); label.moveNext() )
    // {
    //     DOpus.Output( "  " + label.item() );
    // }

    // DOpus.Output( "Tabs for " + path );
    // var item = DOpus.FSUtil.GetItem(path);
    // for( var label = new Enumerator(item.Labels("Tabs")); !label.atEnd(); label.moveNext() )
    // {
    //     DOpus.Output( "  " + label.item() );
    // }

    do
    {
        var item = DOpus.FSUtil.GetItem(path);

        for( var label = new Enumerator(item.Labels("Tabs")); !label.atEnd(); label.moveNext() )
        {
            var labelText = label.item();
            if( labelText == '#' )
                tabLabel = item.name + '\\' + tabLabel;
            else
                tabLabel = label.item() + '\\' + tabLabel;
        }
    }
    while( path.Parent );

    SetTabLabel( tab, tabLabel );
}

function SetTabLabel( tab, label )
{
    if (label == undefined || !label)
        label = "";

    var rawCommmand = 'GO TABNAME' + (label != '' ? '="' + label + '"' : '' );
    raw( rawCommmand, tab );
}

function raw(rwCommmand, tab)
{
    var dopusCommand = DOpus.NewCommand;

    if (tab != undefined)
        dopusCommand.SetSourceTab(tab);

    dopusCommand.RunCommand(rwCommmand);
}
1 Like

Nice!

The only thing that's a little annoying is you can't currently hide 'normal' labels from the tabs. e.g. if you have a label assignment to give hidden folders the 'Red' label, then any time you're inside a hidden folder you'll get a 'Red' prefix on your tabs :smiley:

Use Labels("*", "explicit") to only retrieve explicitly assigned labels.

If I do that then this won't work:

So, I think I'll insist upon a "Tabs" category and only use labels from that automatically.

Well... I would... but... Bug? item.Labels can only be limited to the 'Status' category? (Update: Fixed in 12.2.6)

Original post updated - requires 12.2.6 :slight_smile: