Can I make the Viewer Panel Open/Close, when a media file is Selected/Deselected?

Go to the resources of the button i shared earlier and edit the dialog line (opacity="0")

<dialog fontsize="8" height="14" lang="english" opacity="0" width="54">

But with this you wont be able to exit. You would need a way more complex logic (like a custom command running the inivisible dialog and one command to change some variable to exit it).

it's about the standalone viewer.

Thanks, the script button is done.
The viewer pane version:

//Function : Viewer pane Auto Open Close
//Modified : 2022-12-29
//Version  : 1.3
//(Source: https://resource.dopus.com/t/can-i-make-the-viewer-panel-open-close-when-a-media-file-is-selected-deselected/42698/11)



function OnClick(clickData)
{
	var tab = clickData.func.sourcetab;
	var cmd = clickData.func.command;
	var dlg = clickData.func.Dlg;
	
	if(!DOpus.Vars.Exists("ViewerAutoOpenClose"))                                                 //(if the variable does not exist, then)
	{
	  DOpus.Vars.Set("ViewerAutoOpenClose") = 1;                                                  //Set the variable
	  if(DOpus.listers.lastactive.viewpane == 0) {cmd.RunCommand("Set VIEWPANE=on");}             //Open if the viewer pane is closed
	} else {                                                                                      //Else, if the variable exists, then
	  DOpus.Vars.Delete("ViewerAutoOpenClose");                                                   //Delete variable to end the script
	  if(DOpus.listers.lastactive.viewpane == 1) {cmd.RunCommand("Set VIEWPANE=off");}            //Close if the viewer is open
	}

	dlg.detach = true;
	dlg.template = "dialog1";
    dlg.title = "OnSelect";
    dlg.create();
    //dlg.show();
	dlg.opacity = 0;   // Make the dialog invisible
    dlg.watchtab(tab, "select");

	var msg;
	do
	{
		msg = dlg.GetMsg();
		
		if(!DOpus.Vars.Exists("ViewerAutoOpenClose"))                                                        //If the variable does not exist, then
		{
		if(DOpus.listers.lastactive.viewpane == 1) {cmd.RunCommand("Set VIEWPANE=off");}                     //If the viewer pane is open, close the viewer pane
		break;                                                                                               //End the script
		}
		
	    if (!msg.result) 
			break;

		var event = msg.event;   //Get event
		//var control = msg.control;
		var value = msg.value;

		//Log("Event: " + event + ", Control: " + control + ", Value: " + value);

		if(event == "tab" && value == "select")                                                              //If there is a selection event
		{
			tab.Update();                                                                                    //Update
			if(tab.selstats.selfiles > 0)                                                                    //If the selected file > 0
			{
				var item = tab.selected_files(0);                                                            //Get the first file selected
				if(item.metadata == "image" || item.metadata == "video")                                     //If it is an image or video, then
				{
				    if(DOpus.listers.lastactive.viewpane == 0)                                               //Open if the viewer pane is closed
					{
					cmd.RunCommand("Set VIEWPANE=on");
					}
				} else {cmd.RunCommand("Set VIEWPANE=off");}                                                 //If it is not an image or video, close the viewer panel
			}
			else if(tab.selstats.selfiles == 0 && DOpus.listers.lastactive.viewpane == 1 || msg == 0)        //Closes the viewer panel if no file is selected and the viewer pane is open
			{
				cmd.RunCommand("Set VIEWPANE=off");
			}
		} else if (value == "close") break
	}
	while(msg);
}


/*
function Log(msg, e)
{
	DOpus.output(String(msg), e || false);
}
*/

Viewer pane auto open close.dcf (6.7 KB)

Now I know :smiley:

The standalone viewer version.

//Function : Standalone Viewer Auto Open Close
//Modified : 2022-12-29
//Version  : 1.3
//(Source: https://resource.dopus.com/t/can-i-make-the-viewer-panel-open-close-when-a-media-file-is-selected-deselected/42698/11)



function OnClick(clickData)
{
	var tab = clickData.func.sourcetab;
	var cmd = clickData.func.command;
	var dlg = clickData.func.Dlg;
	
	if(!DOpus.Vars.Exists("ViewerAutoOpenClose"))                                                 //(if the variable does not exist, then)
	{
	  DOpus.Vars.Set("ViewerAutoOpenClose") = 1;                                                  //Set the variable
	  if(DOpus.viewers.lastactive == 0) {cmd.RunCommand("Show POS 1280,0 SIZE 960,1080");}        //Open if the standalone viewer is closed
	} else {                                                                                      //Else, if the variable exists, then
	  DOpus.Vars.Delete("ViewerAutoOpenClose");                                                   //Delete variable to end the script
	  if(DOpus.viewers.lastactive != 0) {DOpus.viewers.lastactive.Command("close");}              //Close if the standalone viewer is open
	}

	dlg.detach = true;
	dlg.template = "dialog1";
    dlg.title = "OnSelect";
    dlg.create();
    //dlg.show();
	dlg.opacity = 0;   // Make the dialog invisible
    dlg.watchtab(tab, "select");

	var msg;
	do
	{
		msg = dlg.GetMsg();
		
		if(!DOpus.Vars.Exists("ViewerAutoOpenClose"))                                                        //If the variable does not exist, then
		{
		if(DOpus.viewers.lastactive != 0) {DOpus.viewers.lastactive.Command("close");}                       //If the standalone viewer pane is open, close the standalone viewer
		break;                                                                                               //End the script
		}
		
	    if (!msg.result) 
			break;

		var event = msg.event;   //Get event
		//var control = msg.control;
		var value = msg.value;

		//Log("Event: " + event + ", Control: " + control + ", Value: " + value);

		if(event == "tab" && value == "select")                                                              //If there is a selection event
		{
			tab.Update();                                                                                    //Update
			if(tab.selstats.selfiles > 0)                                                                    //If the selected file > 0
			{
				var item = tab.selected_files(0);                                                            //Get the first file selected
				if(item.metadata == "image" || item.metadata == "video")                                     //If it is an image or video, then
				{
				  if(DOpus.viewers.lastactive != 0) {cmd.RunCommand('Show FILE="'+item+'"');                 //If the standalone viewer is open, pass the current file
				    } else {cmd.RunCommand('Show "'+item+'" POS 1280,0 SIZE 960,1080');}                     //Else, pass the current file and open the standalone viewer at the specified coordinates and size
				} else {DOpus.viewers.lastactive.Command("close");}                                          //If it is not an image or video, close the standalone viewer
			}
			else if(tab.selstats.selfiles == 0 && DOpus.viewers.lastactive != 0 || msg == 0)                 //If no file is selected and the standalone viewer is open, close the standalone viewer
			{
				DOpus.viewers.lastactive.Command("close");
			}
		} else if (value == "close") break
	}
	while(msg);
}


/*
function Log(msg, e)
{
	DOpus.output(String(msg), e || false);
}
*/

Standalone Viewer auto open close.dcf (6.9 KB)

Glad i could help. May i ask you about your workflow/usecase for that? I dont see a reason for always closing the viewer when unselecting. I mean opening the first file in a windowed viewer seems fine. But then i simply could go back to the mainwindow. And reopening might take longer than having it open in the background and just displaying the new selection.
For going through images i make use of leo's viewer select script and two buttons in lister and standalone viewer with the same shortcut for viewing the selected image or selecting the viewed image (bringing the other window to the top).

No, I don't need these, I'm just learning scripting. :smiley:

@WKen
Gaaaah-- THANK you @WKen! This is exactly what I was looking for, and it works so well! I have one last question for you... As of now, it appears that I have to click the button to activate this each time I start up Directory Opus. Do you know if there's a way to pre-activate or automatically activate it, so I wouldn't have to do so manually? Although, if not, the fact that you've created this per my request is already more than I was expecting! I appreciate this big time.

@Felix.Froemel
Regarding my use-cases, I have Portrait Oriented monitors, so horizontal real-estate is in short supply!
Since I browse a mixture of images and other files regularly, it's such a hassle to toggle the Viewer Pane open and closed every time I need to glance at a picture or two.

But this, at least for me, is a game-changer! I've been hoping for the ability to auto open/close the Viewer Pane based on what's selected, for years. And finally, Directory Opus... and WKen, have saved me!

Not sure if it will work for you, I'm a script beginner.

function OnInit(initData) {
    initData.name = 'ViewerPaneAutoOpenClose';
    initData.version = '1.3.5';
    initData.copyright = 'ViewerPaneAutoOpenClose';
    initData.url = 'https://resource.dopus.com/t/can-i-make-the-viewer-panel-open-close-when-a-media-file-is-selected-deselected/42698/11)';
    initData.desc = 'ViewerPaneAutoOpenClose';
    initData.default_enable = true;
    initData.min_version = '12.0';
	//Modified : 2023-01-03
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'ViewerPaneAutoOpenClose';
    cmd.method = 'OnViewerPaneAutoOpenClose';
    cmd.desc = 'ViewerPaneAutoOpenClose';
    cmd.label = 'ViewerPaneAutoOpenClose';
    cmd.template = '';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnViewerPaneAutoOpenClose(scriptCmdData) {

    var cmd = scriptCmdData.func.command;
    var tab = scriptCmdData.func.sourcetab;
    var dlg = scriptCmdData.func.Dlg();

    if (!tab) return;
	if(!DOpus.Vars.Exists("ViewerPaneAutoOpenClose"))      // Sets the variable if it does not exist
	{
		DOpus.Vars.Set("ViewerPaneAutoOpenClose") = 1; 
	}
	
	dlg.detach = true;
	dlg.template = "dialog1";
    dlg.title = "OnSelect";
    dlg.create();
    //dlg.show();
	dlg.opacity = 0;
    dlg.watchtab(tab, "select");

	var msg;
	do
	{
		msg = dlg.GetMsg();

	    if (DOpus.Vars.Exists("ViewerPaneAutoOpenClose"))                                        // If the variable exists, delete variable to end the script, end the script for other instances
	    {
	        DOpus.Vars.Delete("ViewerPaneAutoOpenClose");
		    var randomNum = Math.floor(Math.random() * 10);
	        if (randomNum) {DOpus.Delay(randomNum)}
	        DOpus.Vars.Set("ViewerPaneAutoOpenClose") = 1;
	    } else
	    {                                                                                     // If the variable does not exist, refresh and exit
		    if(DOpus.listers.lastactive.viewpane == 1) {cmd.RunCommand("Set VIEWPANE=off");}  // Close if the viewer is open
		    break;                                                                            // Exit
	    }

        var msgResult = msg.result;
	    if (!msgResult)
			break;

		var event = msg.event;   //Get event
		//var control = msg.control;
		var value = msg.value;

		//Log("Event: " + event + ", Control: " + control + ", Value: " + value);

		if(event == "tab" && value == "select")                                                              //If there is a selection event
		{
			tab.Update();                                                                                    //Update
			if(tab.selstats.selfiles > 0)                                                                    //If the selected file > 0
			{
				var item = tab.selected_files(0);                                                            //Get the first file selected
				if(item.metadata == "image" || item.metadata == "video")                                     //If it is an image or video, then
				{
				    if(DOpus.listers.lastactive.viewpane == 0)                                               //Open if the viewer pane is closed
					{
					   cmd.RunCommand("Set VIEWPANE=on");
					}
				} else {cmd.RunCommand("Set VIEWPANE=off");}                                                 //If it is not an image or video, close the viewer panel
			}
			else if(tab.selstats.selfiles == 0 && DOpus.listers.lastactive.viewpane == 1 || msg == 0)        //Closes the viewer panel if no file is selected and the viewer pane is open
			{
				cmd.RunCommand("Set VIEWPANE=off");
			}
		} else if (value == "close") break
	}
	while(msg);


/*
function Log(msg, e)
{
	DOpus.output(String(msg), e || false);
}
*/
}



 var cmd = DOpus.Create().Command();


// Called when a new tab is opened
function OnOpenTab(openTabData)
{
	cmd.RunCommand("ViewerPaneAutoOpenClose");
}


// Called when the source and destination are changed
function OnSourceDestChange(sourceDestChangeData)
{
	if (!DOpus.Vars.Exists("sourceDestChangedForViewerPane"))
	{
	    DOpus.Vars.Set("sourceDestChangedForViewerPane") = 1
		cmd.RunCommand("ViewerPaneAutoOpenClose");	
    } else
	{
		DOpus.Delay(200);
	    DOpus.Vars.Delete("sourceDestChangedForViewerPane");
		return
	}
}



==SCRIPT RESOURCES
<resources>
	<resource name="dialog1" type="dialog">
		<dialog fontsize="8" height="14" lang="english" opacity="0" width="54">
			<control close="0" height="14" name="button1" title="Exit" type="button" width="54" x="0" y="0" />
		</dialog>
	</resource>
</resources>

Viewer pane auto open close.js.txt (4.6 KB)

How the script works

After opening a new tab, an invisible dialog will open to monitor events in a folder tab, at this time pressing the Enter key will close the dialog and the current tab will not be monitored.
Clicking on the other DOpus lister and then clicking on the current lister will reopen the script. . .
Since invisible dialogs steal focus, the current solution is to click with the mouse, or press Alt+Tab twice after opening a new tab.

1 Like

You can move the viewer pane to the bottom, if that helps.

@WKen Not sure if it will work for you, I'm a script beginner.

Wow-- once again, THANK YOU for volunteering to go above and beyond to create this for me! I've just tested your script, and it works flawlessly! I'm beyond excited!

You can say you're a beginner all you like-- My friend, you're a pro!

@Leo You can move the viewer pane to the bottom, if that helps.

Hmmm, that's good to know that I can reposition the Viewer Pane! In my case, Wken's solution works perfectly, but I will tuck that knowledge into my belt. :grin:

Also, @lxp and @galaxyhub thank you as well for taking the time to help look into and advise me on this request! I appreciate it big time!