Script to test for a single folder being selected

I want to create a subfolder if a single folder is selected otherwise create a folder in the file display.

I thought the following code might work:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.deselect = false;

	if (clickData.func.sourcetab.selected_dirs.count == 1)
		clickData.func.command.RunCommand('@Set FolderName {dlgstringS|Create a New Subfolder within {sourcepath}.\n\nPlease enter the new subfolder name.|New Subfolder}');
		clickData.func.command.RunCommand('CreateFolder "{filepath}{$FolderName}');
		clickData.func.command.RunCommand('Go REFRESH=expanded,source,state');
	else
		clickData.func.command.RunCommand('CreateFolder');	
}

How can I fix it. TIA

You're missing { ... } around your code blocks.

I don't understand your answer. What do I add or remove from the script code?

This would let you call the built-in Create New Folder dialog, but with the source pointing to the selected dir. It will also expand the folder if needed.
Note that it'll notify you via the status bar if the source is the selected dir.
You can take out the MULTI arg if you want to.

function OnClick(clickData) {
	var cmd = clickData.func.command;
	cmd.deselect = false;
	var tab = clickData.func.sourcetab;
	var create_subdir = tab.stats.seldirs === 1;
	cmd.Clear();
	if (create_subdir) {
		var dir = tab.selected_dirs(0);
		cmd.SetSource(dir);
		tab.Notify('status', 'Creating folders in "' + dir + '"...', 3000);
	};
	if (cmd.RunCommand('CreateFolder MULTI') && create_subdir) {
		cmd.AddFile(dir);
		DOpus.Output('Expanding "' + dir + '"');
		cmd.RunCommand('GO EXPANDBRANCH=script')
	};
}
1 Like

Javascript basics.

Your code is effectively this:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.deselect = false;

	if (clickData.func.sourcetab.selected_dirs.count == 1)
	{
		clickData.func.command.RunCommand('@Set FolderName {dlgstringS|Create a New Subfolder within {sourcepath}.\n\nPlease enter the new subfolder name.|New Subfolder}');
	}

	clickData.func.command.RunCommand('CreateFolder "{filepath}{$FolderName}');
	clickData.func.command.RunCommand('Go REFRESH=expanded,source,state');

	else // Has no matching if!

	clickData.func.command.RunCommand('CreateFolder');	
}

It should be like this:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.deselect = false;

	if (clickData.func.sourcetab.selected_dirs.count == 1)
	{
		clickData.func.command.RunCommand('@Set FolderName {dlgstringS|Create a New Subfolder within {sourcepath}.\n\nPlease enter the new subfolder name.|New Subfolder}');
		clickData.func.command.RunCommand('CreateFolder "{filepath}{$FolderName}');
		clickData.func.command.RunCommand('Go REFRESH=expanded,source,state');
	}
	else
	{
		clickData.func.command.RunCommand('CreateFolder');	
	}
}

That's the most obvious problem, although there may be some others as well. For example, the 'CreateFolder "{filepath}{$FolderName}' looks like it's missing a " near the end.

@errante
Thank you for this very informative code. I appreciate it very much as I am trying fitfully to learn Opus javascript. Could you point me to some documentation on basic Opus javascript that would help me to understand your code? Thanks again for your time.

You'll find the references here:

For basic Javascript guides, I guess google will help in finding basics tutorials.
If you've already been used to code in languages such as Java, Python, C++, ... you can straight jump into references.
Note that MSDN pages (which are describing a more advanced version of Javascript, using some more functions/methods than the ones available in Windows JScript) are using bits of code that can be compiled on the fly from the web page. Interesting when you want to test a specific methods with your inputs/values.
Of course, MSDN pages are NOT aware of the Opus object model.

Not really, unfortunately. I don't think the scripts-addins I publish here are useful for learning scripting either.

To learn the basic rules of (old) JavaScript, the content linked above will work.
To apply it to Opus, you'd need to check the manual and abstract the information by applying the basics you learned in the previous step.

Years ago when I was also starting with scripting like you're now, I remember I used to rely on examples/scripts I saw on this forum, like the ones @Steve posted. @lxp also used to publish basic code, simple and easy to understand.

Other than that, what I can tell you is that, like with anything, you get better over time and with practice :grinning_face:.

This is the same code but with comments :

/*
Must-read:

Tab object : https://docs.dopus.com/doku.php?id=reference:scripting_reference:scripting_objects:tab
Command object :https://docs.dopus.com/doku.php?id=reference:scripting_reference:scripting_objects:command
TabStats object : https://docs.dopus.com/doku.php?id=reference:scripting_reference:scripting_objects:tabstats
CreateFolder Command : https://docs.dopus.com/doku.php?id=reference:command_reference:internal_commands:createfolder
Go Command : https://docs.dopus.com/doku.php?id=reference:command_reference:internal_commands:go
*/
function OnClick(clickData) {
	var cmd = clickData.func.command; //get a pre-filled Command object
	cmd.deselect = false;
	var tab = clickData.func.sourcetab;
	var create_subdir = tab.stats.seldirs === 1; // tab.stats.seldirs is the same as tab.selected_dirs.count, but a bit better
	cmd.Clear(); //clear all files from the Command object
	if (create_subdir) { //do this part only if there's a single folder selected
		var dir = tab.selected_dirs(0); //get the selected folder
		cmd.SetSource(dir); //set the source path for the Command object, so any folder created later will use it as source
		tab.Notify('status', 'Creating folders in "' + dir + '"...', 3000); //set a notify text in the status bar
	};
//this will run the 'CreateFolder MULTI' command in all cases
	if (cmd.RunCommand('CreateFolder MULTI') && create_subdir) {
        //only do this part if there was a single folder selected and the previous command ran successfully
		cmd.AddFile(dir); // add the single folder to the Command object, so we can act upon
		DOpus.Output('Expanding "' + dir + '"');
		cmd.RunCommand('GO EXPANDBRANCH=script') //expand the single folder selected
	};
}

Here is an alternative to the Microsoft documentation, a bit easier to follow JScript Online Viewer | Rapise | Inflectra