Machine-sensitive tab labels, show \\Server\Share\Parent\Path as 'Server-Path'

What is it?

This script makes identifying tabs on remote machines in LAN a bit easier,
by inserting the machine name and/or a Unicode symbol into the tab label.
E.g. if you open \\MyGamingRig\Games\Diablo2
the tab label will show MyGamingRig-Diablo2.

All machines in my LAN have similar/identical directory structures.
It gets often confusing to recognize to which machine
the "Temp" or "Downloads" or "DigiMusic" tab belongs.
I could not find a similar script, so I cooked up this little one.

There is no customization UI nor will be.
If you need to adjust the logic, see setMachineSensitiveTabLabel().

It reacts to folder changes, stored lister layouts, etc.
On local paths, it simply shows default DOpus label.

It looks like this:

Copy to your scripts folder:

Script code for reference:

/* eslint-disable quotes */
/* eslint-disable no-inner-declarations */
/* global Enumerator DOpus */
/* eslint indent: [2, 4, {"SwitchCase": 1}] */
///<reference path="./_DOpusDefinitions.d.ts" />

/*
# What is it?
This script makes identifying tabs on remote machines in LAN a bit easier,
by inserting the machine name and/or a Unicode symbol into the tab label.
E.g. if you open \\MyGamingRig\Games\Diablo2
the tab label will show MyGamingRig-Diablo2.

All machines in my LAN have similar/identical directory structures.
It gets often confusing to recognize to which machine
the "Temp" or "Downloads" or "DigiMusic" tab belongs.
I could not find a similar script, so I cooked up this little one.

There is no customization UI nor will be.
If you need to adjust the logic, see setMachineSensitiveTabLabel().

It reacts to folder changes, stored lister layouts, etc.
On local paths, it simply shows default DOpus label.

# Unicode Icons
DO does not allow to set custom tab icons, yet,
but if you set Tab font to Segoe UI Emoji/Symbol or alike
in the preferences, you can insert a unicode symbol.

For emojis beyond FFFF, e.g. 1F989 (owl),
you need to use a so-called surrogate pair (SP), e.g. \uD83E\uDD89.
To get the SP code, use the form at the bottom of:
https://datacadamia.com/data/type/text/surrogate

SP calculation logic from the link above:
function surrogateCalculator(S){
   H = Math.floor((S - 0x10000) / 0x400) + 0xD800;
   L = ((S - 0x10000) % 0x400) + 0xDC00;
   return [H,L];
}
function printSurrogatePair(codePoint){
   let [H,L] = surrogateCalculator(codePoint);
   let outputText = 'The UTF16 surrogate pair for '+codePoint+' is \\u'+H.toString(16).toUpperCase()+'\\u'+L.toString(16).toUpperCase()+' ('+String.fromCodePoint(codePoint)+')';
   console.log(outputText);
}
Note: String.fromCodePoint() does not work in JScript!
*/
var emojiPrefix = {
    'Apollo' : '\uD83C\uDFF9',  // bow+arrow, SP of \u1F3F9
    'Mercury': '\uD83D\uDC0D',  // snake (kinda related to caduceus), SP of \u1F40D - alternatives (are too small): '\u2624' caduceus - '\u269A' staff of Hermes (Mercury in Greek) - '\u263F' astrological mercury
    'Pluto'  : '\u2442',        // fork (kinda) - alternative: '\u2647' astrological pluto
    'Minerva': '\uD83E\uDD89',  // owl, SP of \u1F989
    'Vulcan' : '\uD83D\uDD28'   // hammer, SP of \u1F528
};

/** @param {DOpusScriptInitData} initData */
// eslint-disable-next-line no-unused-vars
function OnInit(initData) {
    initData.name           = 'CuMachineSensitiveTabLabels';
    initData.version        = '1.0';
    initData.copyright      = '© 2021 cuneytyilmaz.com';
    initData.url            = 'https://github.com/cy-gh/';
    initData.desc           = 'Prepend remote computer name to tab labels';
    initData.default_enable = true;
    initData.min_version    = '12.0';
    initData.group          = 'cy';
}

/** @param {DOpusOpenTabData} oOpenTabData */
// eslint-disable-next-line no-unused-vars
function OnOpenTab(oOpenTabData) {
    setMachineSensitiveTabLabel(oOpenTabData.tab);
}

/** @param {DOpusOpenListerData} oOpenListerData */
// eslint-disable-next-line no-unused-vars
function  OnOpenLister(oOpenListerData) {
    if (!oOpenListerData.after) { return true; }
    for (var e = new Enumerator(oOpenListerData.lister.tabs); !e.atEnd(); e.moveNext()) {
        /** @type {DOpusTab} */
        var dopusTab = e.item();
        setMachineSensitiveTabLabel(dopusTab);
    }
}

/** @param {DOpusAfterFolderChangeData} oAfterFolderChangeData */
// eslint-disable-next-line no-unused-vars
function OnAfterFolderChange(oAfterFolderChangeData) {
    setMachineSensitiveTabLabel(oAfterFolderChangeData.tab);
}

/** @param {DOpusTab} oTab */
function setMachineSensitiveTabLabel(oTab) {
    // if new label is empty, DOpus will set its default label
    var sNewLabel = '';
    var matches = (''+oTab.path).match(reServerExtractor);
    if (matches) {
        // prepend machine-name and last portion of path

        // variant 1: emoji + name + path
        // sNewLabel = (typeof emojiPrefix !== 'undefined' && emojiPrefix[matches[1]] + ' ' || '' ) + matches[1] + '-' + matches[2];

        // variant 2: only emoji + path
        // (emoji reverts back to machine name if not found or lookup array is not defined)
        sNewLabel = (typeof emojiPrefix !== 'undefined' && emojiPrefix[matches[1]] + ' ' || matches[1] + '-') + matches[2];
    }
    cmd.setSourceTab(oTab);
    cmd.runCommand('go tabname' + ( sNewLabel ? '="' + sNewLabel + '"' : ''));
}

// do not touch
var cmd = DOpus.create().command();
// 1: server name, 2: last portion of path
var reServerExtractor = new RegExp(/^\\\\([^\\]+).*?([^\\]+)$/);
1 Like