Add current viewer file to selection in lister tab

This script/button enables you to add the current displayed file in viewer to the selection in the tab that shows the containing folder. You can compare this to flagging/marking files but just with selecting those files. So you can create for example a best of selecion of your images.

<?xml version="1.0"?>
<button backcol="none" display="both" hotkey="alt+S" label_pos="right" textcol="none">
	<label>Add to selection in lister</label>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>function OnClick(clickData)</instruction>
		<instruction>	var currentViewer = DOpus.viewers.lastactive;</instruction>
		<instruction>	var currentDisplayedFile = currentViewer.current;</instruction>
		<instruction>	var containingTab = FindTabForPath(currentDisplayedFile.path);</instruction>
		<instruction>	var selectedFiles = containingTab.selected_files;</instruction>
		<instruction>	</instruction>
		<instruction>	var cmd = DOpus.Create.Command();</instruction>
		<instruction>	cmd.SetSourceTab(containingTab);</instruction>
		<instruction />
		<instruction>	cmd.ClearFiles();</instruction>
		<instruction>	cmd.AddFile(currentDisplayedFile);</instruction>
		<instruction>	cmd.AddFiles(selectedFiles);</instruction>
		<instruction>	cmd.RunCommand(&quot;Select FROMSCRIPT EXACT MAKEVISIBLE&quot;);</instruction>
		<instruction />
		<instruction />
		<instruction>//return tab that shows path otherwise open new</instruction>
		<instruction>function FindTabForPath(path)</instruction>
		<instruction>	DOpus.NewCommand.RunCommand(&quot;Go \&quot;&quot; + path + &quot;\&quot; NEWTAB=findexisting&quot;);</instruction>
		<instruction>	DOpus.listers.update();</instruction>
		<instruction>	var listers = DOpus.listers;</instruction>
		<instruction>	var l;</instruction>
		<instruction>	for(l = 0; l &lt; listers.count; l++)</instruction>
		<instruction>	{</instruction>
		<instruction>		var lister = listers(l);</instruction>
		<instruction>		var tabs = lister.tabs;</instruction>
		<instruction>		var t;</instruction>
		<instruction>		for(var t = 0; t &lt; tabs.count;t++)</instruction>
		<instruction>		{</instruction>
		<instruction>			var tab = tabs(t);</instruction>
		<instruction>			if(tab.path + &quot;&quot; == path + &quot;&quot;) //WTF, but didnt find another working approach</instruction>
		<instruction>			{</instruction>
		<instruction>				return tab;</instruction>
		<instruction>			}</instruction>
		<instruction>		}</instruction>
		<instruction>	}</instruction>

Add to selection in lister.dcf (2.4 KB)

If someone has a better solution for this bad line of code please let me know.

if(tab.path + "" == path + "")

I tried several approaches but this was the only one that worked🙄

You could find the containing tab with

var containingTab = currentViewer.parenttab;

If it is not an object, the tab does not exist.

a better solution for this bad line of code

Only alternative I can think of is using String() instead of adding empty quotes. Technically the same, so it's up to personal preference.

You could use the DOpus.FSUtil.ComparePath method. That will also cover some other things, like case-insensitivity and (if I remember correctly) treating \ and / as equivalent.

What you already have is probably fine, though. I usually use the same + "" method to coerce objects to strings in JScript.

1 Like

Thanks. Sounds like what i prefer to use. @Leo another question: do you know why i cant edit my original post (first entry of this topic)? Id like to update the code.

I tried this already and it didnt work for me but i guess i did something wrong (no not-null-check). Ill give it another try.

I used if(new String(...) == new String(...)) not sure if this is the same. I think ill go with DOpus.FSUtil.ComparePath()

You should be able to now.

No, it's not:

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

    DOpus.Output(typeof tab.path);
    DOpus.Output(typeof (tab.path + ''));
    DOpus.Output(typeof String(tab.path));
    DOpus.Output(typeof new String(tab.path));

But if you use parenttab you won't need either :wink:

1 Like

Thanks for that example :slight_smile:

Tried var containingTab = currentViewer.parenttab; but then the not null check fails, because type of containingTab then is number with the value 0. Should that somehow mean 0 == null, so checking for 0 instead of null? (Fun fact: in german 0 translates to Null, should that be a hint? :smiley: )

Checking typeof containingTab == 'object' should work.

1 Like

thanks that helped