How to select newest folder if no newest file exists?

I want to select the newest file in a folder and if there are no files at all I want to select the newest folder instead.

Can this be done by a simple command?

Formally expressed I want to execute these commands but the second should only be performed if the first wasn't successful. Is there any modifier that can be used for this usecase?

Select DATE=both,newest TYPE=files DESELECTNOMATCH MAKEVISIBLE
Select DATE=both,newest TYPE=folders DESELECTNOMATCH MAKEVISIBLE

I would use a script. Conditional logic is what scripting is for.

You could run one command, then see if anything is selected (remember to call tab.Update() to get a new snapshot of the selection details after the first command) and if nothing is selected then run the second command.

I think you already have the pieces to do all that from the GoUpDeep/GoDownDeep scripts we discussed before.

Ok, here is the appropriate script. It even allows to toggle between the newest file and folder.

@runonce
@script jscript

function selectNewest(sourcetab, command) {
	var oldItemPath = sourcetab.stats.selitems == 1 ? String(new Enumerator(sourcetab.selected).item()) : null;
	command.RunCommand("Select NONE");
	command.RunCommand("Select DATE=both,newest TYPE=files MAKEVISIBLE");
	if (sourcetab.stats.dirs > 0) {
		sourcetab.Update(); // synchronize tab object with actual tab
		var newItemPath = sourcetab.stats.selitems == 1 ? String(new Enumerator(sourcetab.selected).item()) : null;
		if (newItemPath == null || oldItemPath == newItemPath) {
			command.RunCommand("Select NONE");
			command.RunCommand("Select DATE=both,newest TYPE=dirs MAKEVISIBLE");
		}
	}
}

function OnClick(clickData) {
	selectNewest(clickData.func.sourcetab, clickData.func.command);
}