Script Button to Close Explorer and Open in Opus?

Does anybody know how I could script a button to add to my Opus toolbar? I want to make a button that finds all open Windows Explorer windows - closes them, and then opens the same path inside a new tab within Opus.

EDIT: To clarify, I do know and use the Windows Explorer replacement feature within Opus. There are a few rare occurrences where even this does not work - which is the reason I want to make a button for it.

This button will do that for you. Note that due to current limitations in Opus it will probably only work for "real" folders (C:... etc) and not virtual ones like Computer or Network.

ExplorerToTabs.dcf (2.7 KB)

The script code is shown below for anyone interested:

Function OnClick(ByRef clickData)
	dim objShell
	dim objShellWindows
	dim cmd
	
	set objShell = CreateObject("shell.application")
	set objShellWindows = objShell.Windows
	set cmd = DOpus.Create.Command
	
	for each explorer in objShellWindows
	    cmd.RunCommand("go """ & ConvertFileURL(explorer.LocationURL) & """ newtab")
		explorer.Quit
	next
	
	set cmd = nothing
	set objShellWindows = nothing
	set objShell = nothing
End Function

Function ConvertFileURL(ByVal str)
DOpus.Output(str)
	if Left(str, 5) = "file:" then
		ConvertFileURL = URLDecode(Right(str, Len(str) - 8))
	else
		ConvertFileURL = str
	end if
End Function

Function URLDecode(ByVal str)
 Dim intI, strChar, strRes
 str = Replace(str, "+", " ")
 For intI = 1 To Len(str)
  strChar = Mid(str, intI, 1)
  If strChar = "%" Then
   If intI + 2 < Len(str) Then
    strRes = strRes & Chr(CLng("&H" & Mid(str, intI+1, 2)))
     intI = intI + 2
   End If
  Else
   strRes = strRes & strChar
  End If
 Next
 URLDecode = strRes
End Function
1 Like

This could be very useful for me but unfortunately it doesn't work. Is it for specific versions of Windows? I am testing on Windows 10 with portable Opus (v12.7.1) at work but will also try it on Windows 7 when I get home.

The error message I get is:

  • 02/07/2018 14:56 Error at line 11, position 6
  • 02/07/2018 14:56 A method was called unexpectedly (0x8000ffff)

What Explorer windows do you have open?

This version will skip windows that the method calls aren't working on. (I think those are Opus windows, not Explorer ones, so you would want to skip them anyway. We have to register ourselves in that list to make certain APIs work, if Explorer Replacement is turned on.)

ExplorerToTabs_JS.dcf (1.7 KB)

I had to re-write it in JScript to get error handling, which VBScript can't do.

(decodeURI is built into JScript so there's less code, as a bonus.)

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	var objShell = new ActiveXObject("Shell.Application");
	var objShellWindows = objShell.Windows();

	for (var i = 0; i < objShellWindows.Count; ++i)
	{
		var objExplorerWindow = objShellWindows(i);
		var path;
		try {
			path = objExplorerWindow.LocationURL;
		}
		catch(e) {
			continue;
		}

		if (path.indexOf("file:///") == 0) {
			path = decodeURI(path.substring(8));
		}
		if (path.length == 0) {
			continue;
		}

		cmd.RunCommand("Go NEWTAB=findexisting PATH=\"" + path + "\"");
		objExplorerWindow.Quit();
	}
}
1 Like

Thank you very much both Jon and Leo! You guys are great, absolutely love Opus Directory by the way.
Jon I tried your solution and as blueroly says, I also get an error Line 11, Position 6. I tried it with various windows open, and even just with a single Explorer window opened at C:\ and then again at C:\Program Files. Explorer was not elevated when open either.

Leo's version worked flawlessly for me though! So I'll be using that.

I had one final thought on this though, is there a way to run commands at intervals? And if so, would it be feasible to run this command every 1 minute or so, or would that cause CPU spikes when it runs?

It's possible but probably not a great idea. What's opening all these Explorer windows? Maybe that can be dealt with instead.

I have used a few applications that cause this specifically, can't recall any by name at the moment.
I know that VeraCrypt is one of them though as I frequently use it for cloud backups, it has the option to open an Explorer window to a specified drive when a volume is mounted.


This is the code they use to call Explorer.

void OpenVolumeExplorerWindow (int driveNo)
{
	wchar_t dosName[5];
	SHFILEINFO fInfo;

	StringCbPrintfW (dosName, sizeof(dosName), L"%c:\\", (wchar_t) driveNo + L'A');

	// Force explorer to discover the drive
	SHGetFileInfo (dosName, 0, &fInfo, sizeof (fInfo), 0);

	ShellExecute (NULL, L"open", dosName, NULL, NULL, SW_SHOWNORMAL);
}

Snippet from: https://github.com/veracrypt/VeraCrypt/blob/a108db7c85248a3b61d0c89c086922332249f518/src/Common/Dlgcode.c

They need to change L"open" to NULL if they want to launch the default file manager.

FWIW I had several "real" folders open but all seems to work fine now with Leo's version. Thanks guys.

Thanks Leo! I'll file a bug report on Github and see if I can't get them to change it.

In the mean time, how could I make your script run in intervals? Taking into consideration that it's not advisable, I'd still like to know how this could be done without clicking a button to activate it. Mainly I feel it could prove useful for future scripts I might wish to make. Is there a place to implement scripts upon start up of Opus, or when a file path is open?

Why not make a button or hotkey to trigger the script?

Running scripts on timers or scripts that loop forever is a very bad idea for something like this. So bad I'm not going to spend time thinking about how to do it. :slight_smile: