Command: Open file using extensions or mime-types to choose program

Hello,

I would like to share the script I wrote. I wanted to have a smart "open file" command that could automatically determine the file type (binary, text, image, etc.) and choose an appropriate application for opening it.
I ended up using the Unix "file" command to determine the file's MIME type and distinguish which program should be used to open it.

You can find more details and the script in my GitHub repo:
PolarGoose/DirectoryOpus-UniversalOpen-plugin

After adding a script, failure is displayed in the script options

Thank you for your interest.
Could you give more details?
Unfortunately, the script is not easy to integrate: you need to change the path to the programs associated with a particular extension. After you do that, the script should work.
If you are still interested in making it work, please contact me.

In such cases (script requiring customization by the user to provide for instance path to certain executables), the best thing to do imo is to expose those settings so the user can modify them.

It's not much more work for you, but makes it way easier for other users, especially the ones not really into coding, since they don't have to parse your code in search for the approriate lines to modify, be aware of the need to escaoe backslashes in path, and so on.

You need to do one or two things :

  • In the OnInit function, you have to set/declare those settings using initData.config_desc. You can also provide a default value. Example below :
function OnInit(initData)
{
	initData.name = "YourScriptName";
	initData.version = "1.0";
	// (...)
	initData.min_version = "13.0";

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

	option_name = "SomePath";
	initData.Config[option_name] = "C:\\Program Files\\SomeFolder\\SomeExecutable.exe";
	initData.config_desc(option_name) = "Full Path to SomeExecutable.exe";
 )
  • [Optional] Implement OnScriptConfigChange to handle user change (to check/validate new setting for instance). Example below :
function OnScriptConfigChange(configChangeData)
{
	var someExePath = Script.Config["SomePath"];

	var fsUtil = DOpus.FSUtil;
	if (!fsUtil.Exists(someExePath)) {
		DOpus.Output(">>ERROR : Unable to locate (" + someExePath + ")");
		return;
	}
	else
		DOpus.Output(">>OK : SomeExe found (" + someExePath + ")");
}

DOpus will remember the user settings once it has been changed by user.
To access the settings, one needs to go to the Scripts management window and click on the settings wheel that displays as soon as you provide a non empty initData.config_desc.

For a full example, see Keep PC awake until one specific process ends

@PassThePeas,
Amazing. Thank you very much for the suggestion. Very reasonable. I will try to adopt it. It will work to specify a path to the File utility and configure executables for opening folders and DLLs. The rest of the configuration will have to stay inside the script so that the user can specify executables using this syntax:

var predefinedFileExtensions = [
    [ ["zip", "7z", "tar", "gz" ], "%ProgramFiles%/7-Zip/7zFM.exe"                                                         ],
    [ ["lnk"                    ], "C:/my/apps/Easy.Link.File.Viewer/Easy Link File Viewer.exe"                       ],
    [ ["msi"                    ], "C:/my/apps/lessmsi/lessmsi-gui.exe"                                               ],
    [ ["exe", "dll"             ], handleExeAndDllFiles                                                                    ],
    [ ["lnk"                    ], "C:/my/apps/Easy.Link.File.Viewer/Easy Link File Viewer.exe"                       ]]

It is indeed too complex and specific a structure for it to be manageable by standard script settings.
Nevertheless you could add to your script : a second command (something like "EditAssociations") and design an edit window which could help add/remove/edit both associations (extensions <=> executable to manage, and MimeType <=> Application to use to open).
It is definitely more work, but would open script use to non coders (and could also help you, if you often change any of these associations). So maybe be assured first that the concept interests other users.

Thank you for the proposal. I will keep it in mind.
Indeed, it is only worth doing if anyone is interested in this script.