How to show item IDs in a script column?

Using code based on an example provided by @lxp I attempted to create a column to display item IDs. It always displays 0 for each item instead of a unique ID. If, instead of item.id I display item.ext the correct extension displays.

image

image

// Show item IDs

function OnInit(initData) {
    initData.name = 'ID';
    initData.desc = 'Show DOpus internal item IDs';
    initData.version = '1.0';
    initData.default_enable = true;
}

function OnAddColumns(addColData) {
    var col = addColData.AddColumn();
    col.name = 'ID';
    col.header = 'ID';
    col.label = 'ID';
    col.method = 'OnColumn';
    col.justify = 'right';
}

function OnColumn(scriptColData) {
    var item = scriptColData.item;
	scriptColData.value = item.id; // Always shows 0 instead of item ID
	//scriptColData.value = item.ext; // Works with item extension
}

It's possible the ID isn't populated when calling column scripts.

I'm not sure what the IDs would be useful for, or why it's even there on the object; maybe there's something you can pass it to that I've forgotten about. But it's probably not that useful for anything else. The IDs are only unique for a given file display tab, and only until it refreshes or changes folders.

Editing the default script slightly, you can see the IDs via a toolbar button:

function OnClick(clickData)
{
	DOpus.ClearOutput();
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection

	DOpus.Output("Selected items in " + clickData.func.sourcetab.path + ":");
	if (clickData.func.sourcetab.selected.count == 0)
	{
		DOpus.Output("  (none)");
	}
	else
	{
		for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
		{
			var item = eSel.item();
			DOpus.Output(item.id + " " + item.name);
		}
	}
}

Yes, I know how to access item IDs from a script and I am aware of the the caveats you mention @leo

According to the documentation ScriptColumnData.item "Returns an Item object representing the file or folder that Opus wants the script to return the column value for." and id is a standard item object property, hence my surprise that attempting to access this way doesn't work.

Without going into unnecessary detail, I use item.id for mapping purposes in a fairly complex script I am working on. Being able to see the corresponding IDs in a column whilst debugging would have been useful but since that is not currently possible I will have to use a DOpus.Output reference list instead.

@aussieboykie FWIW, an example of how Evaluator can be abused for the purpose.

It doesn't work very well for a column since when you expand a folder, the calculated values don't change, but it should serve for other purposes.

// Test for item IDs
// copyright (c) 2024  Christian Arellano García

function OnInit(initData) {
	initData.name = 'ID';
	initData.desc = 'Show DOpus internal item IDs';
	initData.version = '1.0';
	initData.default_enable = true;
}

function OnAddColumns(addColData) {
	var col = addColData.AddColumn();
	col.name = 'ID';
	col.header = 'ID (Script)';
	col.label = 'ID';
	col.method = 'OnColumn';
	col.justify = 'right';
	col.autorefresh = true;
	col.namerefresh = true;
}
var id_map;

function OnColumn(scriptColData) {
	var item = scriptColData.item;
	if (!id_map || id_map.count != scriptColData.tab.stats.items) {
		getMapID(scriptColData.tab);
	}
	if (id_map.Exists(scriptColData.item)) scriptColData.value = id_map(scriptColData.item);
	return;
}

function getMapID(tab) {
	// DOpus.Output('Creating map...');
	id_map = DOpus.Create.Map();
	var items = tab.all;
	if (items.count == 0) return;
	var nested = items.RemoveNested(); //remove nested items and get the count
	if (!nested) { //no nested, so the list is good to use as it is
		for (var i = 0; i < items.count; i++) {
			id_map(items(i)) = i + 1;
		}
	}
	else { //use Evaluator
		// DOpus.Output('Evaluator...');
		var cmd = DOpus.Create.Command();
		tab.Update(); //needed to redo items list
		cmd.SetSourceTab(tab);
		var cmdline = 'Select FILTERDEF =$glob:id_map=$glob:id_map + fullpath + "?"; return false;'
		cmd.SetFiles(tab.all);
		cmd.RunCommand("Select NONE");
		// DOpus.Output(cmdline);
		cmd.RunCommand(cmdline);
		DOpus.Delay(50);		
		if (DOpus.Vars.Exists('id_map')) {
			var values = DOpus.Vars.Get('id_map').split('?');
			for (var i = 0; i < values.length; i++) {
				// DOpus.Output(values[i]);
				id_map(values[i]) = i + 1;
			}
			DOpus.Vars.Delete('id_map');
		}
		else
			DOpus.Output('Unable to create id_map!! ');
	}
	return;
}

We'll feed the ID value through to script columns in the next beta.

1 Like

That's Rube Goldberg level of trickery! :+1: