Confirm multi-open (Add-In)

Description:
This script Add-In will make Directory Opus ask you to confirm before opening more than X (configurable, 20 is default) selected items by keyboard Enter key.


I was inspired to make this script Add-In when it happened that I had about 200 high-resolution HD images selected and accidentally pressed Enter (workflow sequence derailed) and then chaos ensued: IrfanView (fastest image viewer for Windows) instances started multiplying and soon the computer became unresponsive (mouse froze) from so much load (RAM was quickly filled and paging-fest started) for several long minutes (I didn't want to reset the computer so I waited).

Now such things shall never happen again!

Installation

  1. download ConfirmMultiopen.js.txt (attachment below)
  2. in your Directory Opus go to Settings > Preferences... then Toolbars > Scripts
  3. now simply drag & drop downloaded ConfirmMultiopen.js.txt to the Preferences window, and that's it!

Usage
There is nothing special you need to do, after installation this Add-In is immediately active. See Changing Settings below to configure it to your needs.

Changing Settings
You can configure this Add-In by going to Settings > Preferences... then Toolbars > Scripts and then selecting Confirm multi-open Add-In in the list and clicking its Configure button.

  • Threshold setting: for example if threshold is 20 you'll get prompted to confirm (screenshot above) in the event that more than 20 selected files/folders are to be opened at once. You can set it to any value, even 0.

  • Excluded extensions setting: list of name endings (you are not limited to pure extensions) which will not count toward the threshold limit (remove the example extensions from the list before adding your real ones).

  • Focus button setting: lets you change which button (Proceed or Cancel) is to be focused when confirmation prompt is triggered. Default is Proceed so when confirmation dialog pops-up you can just press Enter again to proceed.

Uninstalling
You can manage Add-Ins in Directory Opus Preferences; Settings > Preferences... then Toolbars > Scripts.

Download
Version 1.1b (Maintenance; removed no-longer-maintained ScriptWizard support.)
ConfirmMultiopen.js.txt (7.7 KB)

Older versions

Version 1.1 (Added "Excluded extensions" feature. Opus version 11.10.2 or higher is required.)
ConfirmMultiopen.js.txt (9.76 KB)

Version 1.0 no longer available.

Script Code

The script code from the latest version is reproduced here for reference:

// Confirm multi-open
//
//
// This is a script for Directory Opus.
// See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.


// Called by Directory Opus to initialize the script
function OnInit(initData) {
	initData.name = "Confirm multi-open";
	initData.desc = "Ask before opening more than X selected items by keyboard Enter key.";
	initData.copyright = "bytespiller 2014-2015";
	initData.version = "1.1b";
	initData.default_enable = true;
	initData.min_version = "11.10.2";

	// settings & defaults
	initData.config_desc = DOpus.Create.Map();
	var option_name = "";

	option_name = "Excluded extensions";
	initData.Config[option_name] = initData.Config[option_name] = DOpus.NewVector(".mp3 (example)", ".flac (example)", "_comic_page.jpg (example)");
	initData.config_desc(option_name) = "Selected items with these extensions (name endings) will not count toward the threshold limit. No wildcards or regex.";

	option_name = "Focus button";
	initData.Config[option_name] = DOpus.NewVector(0, "Proceed", "Cancel");
	initData.config_desc(option_name) = "A button on the multi-open confirmation dialog to have initial keyboard focus.";

	option_name = "Threshold";
	initData.Config[option_name] = 20;
	initData.config_desc(option_name) = "Multi-open confirmation dialog is triggered on attempt to open more than this amount of items.";
}

// Called when a file or folder is double-clicked
function OnDoubleClick(doubleClickData) {
	if (doubleClickData.mouse != "none") { // ensure we react only when *not* triggered by mouse (such as middle double-click)
		doubleClickData.call = false;
		return false;
	}

	return handleOnDoubleClick(doubleClickData); // from docs: "If you return True, the double-click will be cancelled and the file will not be opened. If you return False the double-click will be allowed to continue (this is the default)."
}


// ********************
// * Custom functions *
// ********************

String.prototype.endsWith = function(suffix) { // http://stackoverflow.com/a/2548133
	return this.indexOf(suffix, this.length - suffix.length) !== -1;
}

function getExcludedItemsCount(selected) {
	var count = 0;
	var enum_selected_items = new Enumerator(selected);

	for (; !enum_selected_items.atEnd(); enum_selected_items.moveNext()) {
		var selected_item = enum_selected_items.item();
		var enum_excluded_extension = new Enumerator(Script.Config["Excluded extensions"]);

		for (; !enum_excluded_extension.atEnd(); enum_excluded_extension.moveNext()) {
			var excluded_extension = enum_excluded_extension.item();
	    	if (String(selected_item).endsWith(excluded_extension)) {
	    		count ++;
	    		break;
	    	}
	    }

	}

	return count;
}

function handleOnDoubleClick(doubleClickData) {
	if ((doubleClickData.Tab.selstats.selitems - getExcludedItemsCount(doubleClickData.Tab.selected)) > Script.Config["Threshold"] && askConfirmation(doubleClickData.tab) == 0) {
		doubleClickData.cont = false; // Tells Dopus to stop processing remaining selected files
		return true; // Cancel current (first) file (i.e., tells Dopus that *we* have handled this file and it doesn't have to do anything else with it)
	}
	else {
		doubleClickData.call = false; // Tells Dopus to continue without calling this script for remaining selected files
		return false; // Dopus proceeds to handle (current and) remaining selected files
	}
}

function askConfirmation(Tab) {
	var dlg = Tab.Dlg;

	dlg.title =  "Opening Multiple Items";
	dlg.icon = "information";
	dlg.message = Tab.selstats.selitems + " selected items are to be opened.";

	dlg.buttons = "Proceed|Cancel";
	dlg.defid = Script.Config["Focus button"] == 0;

	return dlg.Show(); // from docs: "If a dialog has more than one button then by definition the last (right-most) button is the "cancel" button and so this will return index 0."
}
2 Likes

is it possible to disable the response to double-click by middle mouse button?

I've checked the Opus scripting docs and it appears there is currently no support for Add-Ins to find out which mouse button triggered the OnDoubleClick event.

@Leo, Jon: is it possible for you to add some kind of mouse button index property to the DoubleClickData object (similar to "qualifiers" for keys)?
Also while we're at it, is it possible to implement support for Add-Ins to detect whether the OnDoubleClick was triggered by keyboard Enter/Return key?

+1

..o)

This is now implemented/fixed in new version of this Add-In (v1.0.2). You can find download in the first post which I've edited.
Note that updated Opus version 11.10.2 or higher is required.


:opusicon: Thanks to GPSoft for adding the mouse property to the DoubleClickData object in new Opus beta making this possible! :thumbsup:

Minor update v1.0.3 - ScriptWizard support was added.
You can find the download link of this newer 1.0.3 version in the first post above.

jsys.. can you hear me? o)

Does the OnClickData.call feature work for you? I noticed in a similar script, that it doesn't matter wether I set it to true/false.
OnDoubleClick() will be called for all the other items as well, I installed your script test-wise and it's the same. The confirmation comes up again, if you have chosen to continue, which is not the way it's meant to work I guess.

I've checked and this doesn't happen on my current version of Opus which is 11.13.

I now see here that this is happening to you in the latest beta and for txt files. Now, does it happen for any kind of files (such as pdf, which I've tried) and does it happen for folders too?

It is not related to a specific filetype I think. I use the current beta. Strange thing is, a test-script provided by leo also fails for me. It might be something related to other scripts I have running, though I cannot remember having much else using OnDoubleClick(), will need to investigate more. Thanks for taking the time to try and respond! o)

Nice script. I have one suggestion. Could you include an exception list, so we could exclude, for example, MP3 files?

Good idea! I will add this feature.

Feature update 1.1: added Excluded extensions setting. You can now specify list of name endings (so you are not limited to pure extensions) which will not count toward the threshold limit (you can/should remove the example extensions from the list before adding your real ones). This behaves like using asterisk (*) wildcard prefix (no wildcards or regex is actually supported).

I did some testing (actually more testing than coding :grin: ) and it seems it works fine. Let me know if any bugs are discovered.

You can find the download link of this newer 1.1 version in the first post above.

It works perfectly here. Thanks, that was quick! :slight_smile:

Maybe the plugin could use some sort of whitelist or blacklist? Initially i have suggested this feature, because adding MP3s to foobar shouldn't trigger the confirm action. But what if i would not want xrecode (some format conversion tool) to open multiple instances, as it would do now? Of course i know, that i shouldn't open lots of files in xrecode, making it a minor problem, but maybe it's a good feature idea anyway?

Thanks for the feedback and suggestions so far!

Determining which tool would open the selected files sounds tricky (maybe it's not?), and anyway it seems to me that this feature would stray outside the scope of this particular add-in which is intented to be as simple (and general) as possible :slight_smile:

No problem. Eventually i found a setting in xrecode to prevent multiple instances.

I have yet to experience such an accident, but it might be a good safety precaution. Thank you.

It good to see sign of life here.