Can't get select exact to work

I am using javascript I need to be able to select the newly created file from extracting an archive.

I assumed https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Select.htm would be the correct place to start. The example given is Select "Cat Photo (1).jpg" EXACT

I wrote cmd.Runcommand("Select '" + "selected.item().name.substr(0, selected.item().name.lastIndexOf('.'))" + "' EXACT");

The folder I need selected has the same name as the archive minus the extension. I don't know if there is a better way to do it, but either way the folder does not get selected and I don't see any errors in the script output window.

Please, advise, what am I doing wrong and if there are better ways to do this I am happy to listen.

Here is the entire messy code for reference

// DazExtractTest
// (c) 2023 chris

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Adding_a_new_Internal_Command.htm

// Called by Directory Opus to initialize the script
function OnInit(initData) {
	initData.name = "DazExtractTest";
	initData.version = "1.0";
	initData.copyright = "(c) 2023 chris";
	//	initData.url = "";
	initData.desc = "Extract and move daz archives";
	initData.default_enable = true;
	initData.min_version = "12.0";

	// Create a new ScriptCommand object and initialize it to add the command to Opus
	var cmd = initData.AddCommand();
	cmd.name = "DazExtractTest";
	cmd.method = "start";
	cmd.desc = initData.desc;
	cmd.label = "Extract";
	//For arguments Ex: DazMove funcname to "folder"
	cmd.template = "TO/K,";
}

function start(scriptCmdData) {

	if (scriptCmdData.func.args.got_arg.to) {//Check if parameter TO was passed an argument
		var arg = scriptCmdData.func.args.to;
		var cmd = scriptCmdData.func.command;
		// var clicked = scriptCmdData.func;
		var selectedItems = scriptCmdData.func.sourcetab.selected;
		DOpus.ClearOutput();
		// DOpus.Output("Supplied Argument: " + arg);
		// DOpus.Output("Destination: " + cmd.destination);
		for (var selected = new Enumerator(selectedItems); !selected.atEnd(); selected.moveNext()) {
			// DOpus.Output("Selected Item: " + selected.item().name);
			var extractionLocation = extract(selected.item(), cmd);
			DOpus.Output("Searching " + extractionLocation + " for Archives.");
			cmd.Runcommand("Select '" + "selected.item().name.substr(0, selected.item().name.lastIndexOf('.'))" + "' EXACT");
			// findArchives(extractionLocation, cmd);
			// DOpus.Output("Moving " + item.name + "  to: " + dest);
		}
		// DOpus.Output("'" + scriptCmdData.func.args.to + "'");

	} else {
		DOpus.Output("No destination was provided.");
	}
}

function move(folder, cmd, dest) {
	cmd.ClearFiles();
	cmd.AddFile(folder);
	cmd.SetDest(dest);
	cmd.Runcommand("Copy MOVE");
}

function findArchives(root, cmd) {
	if (root != null) {
		// DOpus.Output(root);

		var folderEnum = DOpus.FSUtil.ReadDir(root);
		while (!folderEnum.complete) {
			var item = folderEnum.Next();
			if (item.is_dir) {
				findArchives(item.realpath, cmd)
			} else {
				if(isArchive(item)){
					cmd.AddFile(item);
					cmd.Runcommand("Copy EXTRACT");
				}
			}
		}
	}
}

function isArchive(item) {
	var regex = /\.(?:rar|zip)$/;
	if (regex.test(item.name)) {
		return true;
	} else {
		return false;
	}
}

function extract(item, cmd) {
	if (isArchive(item)) {
		var dest = item.path + "\\" + item.name.substr(0, item.name.lastIndexOf('.'));
		cmd.ClearFiles;
		cmd.AddFile(item);
		DOpus.Output("Will extract: " + item.name + " in " + item.path);
		cmd.SetDest(item.path);
		cmd.Runcommand("Copy EXTRACT = sub");
		return dest;
	} else {
		return null;
	}
}

The item object has properties that give you the name without extension.

But the problem may be that the new file hasn't been seen by the lister yet, so it doesn't have an entry for it to select. If you run Go REFRESH first, does it work then?

(If that works, there may be a better solution, but check first in case the problem is somewhere else.)

1 Like

cmd.Runcommand("Go REFRESH");
cmd.Runcommand("Select '" + selected.item().name_stem + "' EXACT");

thanks this seems to work, thank you