GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Viewer Select - Make file display track standalone viewer

viewer
scripting
jscript
events

#1

Overview:

This script add-in makes it so the file display will track the selected file in the standalone image viewer.

This only affects the folder tab which is active when the viewer opens, and only if that tab stays in (or returns to) the same folder. So it should not get in the way of using multiple tabs, or in situations where you move into other folders.

Here's a quick video (no audio) showing the script in action:

Installation:

Requires Directory Opus 12.2 or above.

  • Download: Viewer_Select.js.txt (v1.3 09/Jan/2017)
  • Open Settings > Preferences / Toolbars / Scripts.
  • Drag Viewer_Select.js.txt to the list of scripts.
History

1.3 (09/Jan/2017):

  • Changed how the script works so that it is more reliable, and in particular works with files within archives.
  • These changes were also required to ensure the script still works with Opus 12.3.3 and above. Old versions of the script inadvertently depended on things which are being tidied up in the next Opus update.

1.2 (22/Dec/2016):

  • Uses viewer.parenttab property introduced in Opus 12.2 as a better way to find the folder tab.
  • Slightly less overhead.
  • Fixed script log error message when viewing files in archives. (May need an Opus code-change to make the script work fully there.)

1.0 (06/Sep/2016):

  • Initial version.

Script Code (JScript)

The script code from the download above is reproduced below. This is for people browsing the forum for scripting techniques. You do not need to care about this code if you just want to use the script.

// Viewer Select
// (c) 2016-2017 Leo Davidson

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.
// See https://resource.dopus.com/t/viewer-select-make-file-display-track-standalone-viewer/23320 for information about this specific script.

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
    initData.name = "Viewer Select";
    initData.version = "1.3";
    initData.copyright = "(c) 2016-2017 Leo Davidson";
    initData.url = "https://resource.dopus.com/t/viewer-select-make-file-display-track-standalone-viewer/23320";
    initData.desc = "The file display selection will track the standalone viewer's current file.";
    initData.default_enable = true;
    initData.min_version = "12.2";
    initData.vars.Set("VMapPaths", DOpus.Create.Map());
    initData.vars("VMapPaths").persist = false;
}

// Called when an event takes place in the standalone viewer
function OnViewerEvent(viewerEventData)
{
    var viewer   = viewerEventData.viewer;
    var tab      = viewer.parenttab;
    var mapPaths = Script.vars("VMapPaths").value;

    if (viewerEventData.event == "load")
    {
        if (!mapPaths.exists(viewer))
        {
            // For the first file, verify the tab contains the file we open with.
            // If it doesn't, the viewer may have been launched from outside of Opus,
            // or via a command which explicitly displays a file from a path which isn't
            // visible in the folder tab. Those situations still associate the viewer with
            // a lister/tab if one exists, and we want to leave those tabs alone.

            if (TabContainsFile(tab, viewer.current))
            {
                mapPaths(viewer) = tab.path + ""; // Store string, not Path object.
            }
            else
            {
                mapPaths(viewer) = ""; // Make a note to ignore this viewer.
            }
        }

        var path = mapPaths(viewer);
        var file = viewerEventData.item;

        // Still in the starting folder?
        if (typeof tab  != "undefined"
        &&  typeof path != "undefined"
        &&  typeof file != "undefined"
        &&  path != ""
        &&  tab.path == path)
        {

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

            cmd.AddFile(file);
            cmd.RunCommand("Select FROMSCRIPT SETFOCUS DESELECTNOMATCH");
        }

        return;
    }

    if (viewerEventData.event == "destroy")
    {
        mapPaths.erase(viewer);
        return;
    }
}

function TabContainsFile(tab, item)
{
    // Workaround to avoid error if no valid file is passed.

    if (typeof tab  == "undefined"
    ||  typeof item == "undefined")
    {
        return false;
    }

    // Simple test is usually enough. Is the tab showing the folder the file is in?
    // (It's possible the file is hidden, but that would be weird in this context, so we ignore that.)

    if (DOpus.FSUtil.ComparePath(DOpus.FSUtil.Resolve(tab.path), item.path))
    {
        return true;
    }

    // To work in collections, libraries and flat view, we need to go through the actual list of files.
    // This could be slow as we don't currently have a quicker way than looping through the files.

    var itemPathString = item + "";

    // It'll usually be a selected file if the viewer opened via double-click. Try them first.

    for (var eItems = new Enumerator(tab.selected_files); !eItems.atEnd(); eItems.moveNext())
    {
        // Compare the path strings, not the item objects.
        if ((eItems.item() + "") == itemPathString)
        {
            return true;
        }
    }

    for (var eItems = new Enumerator(tab.files); !eItems.atEnd(); eItems.moveNext())
    {
        // Compare the path strings, not the item objects.
        // Skip selected files as we already checked them.
        if (!eItems.item().selected && (eItems.item() + "") == itemPathString)
        {
            return true;
        }
    }

    return false;
}

Alternative:

If you don't want the folder tab's selection to follow the viewer all the time, but want to be able to quickly select the viewer's current file on-demand, create a viewer toolbar button or hotkey which runs this command:

Show VIEWERCMD=selectfile

That will select the viewer's current file in the window that launched the viewer (or open a new window if there isn't one).

The alternative command does not require the script at all.


Video Thumbnail Contact Sheet Column Idea
File List issues when using Thumbnails
Image Viewer Request
VIEWERCMD to select currently viewed file minus extension?
Standalone Image Viewer Auto Starts Slideshow When Resuming Focus
Selected file after cycling through files with viewer
Standalone viewer crash
Have file selection follow image in separate viewer
JScript Engine is broken on my machine
Image Viewer Suggestion
Image Viewer Suggestion
ViewerEventData event "load" doesn't detect mouse wheel changes
#2

Excellent, thanks.
Note that script doesnt work for me if images are in librarie.


#3

I'd like a toggle in viewer preferences "Keep viewer in sync with the launching lister".


#4

Root post updated with v1.2, fairly minor changes:

1.2 (22/Dec/2016):

  • Uses viewer.parenttab property introduced in Opus 12.2 as a better way to find the folder tab.
  • Slightly less overhead.
  • Fixed script log error message when viewing files in archives. (May need an Opus code-change to make the script work fully there.)

#5

To make things easier to read, I've cleaned up the thread and removed completed discussions & thanks etc. (appreciated!), consolidating any extra info into the root post.

I noticed I hadn't really responded to these two comments, which got a bit lost in the others:

Is that still the case for you? I checked libraries while updating the script/post, and they seem to work OK for me.

There is an issue with archives, which Kundal reported, and I've added a workaround so it no longer triggers an error message. (Script still doesn't work in them, but won't cause problems in them either. I think we need to do a code change on the Opus side to make the script work fully within archives.)

For a toggle within Preferences, you can toggle the script itself under Preferences / Toolbars / Scripts.

You could also have a variable and a button which toggles it, and have the script look at the variable to see if it should do anything or not, if you wanted a toggle button/hotkey in the viewer or lister toolbars/menus.


#6

Root post updated with v1.3. It's recommended you update the script, if you use it. The old version will stop working in Opus 12.3.3 and above.

  • 1.3 (09/Jan/2017):
    • Changed how the script works so that it is more reliable, and in particular works with files within archives.
    • These changes were also required to ensure the script still works with Opus 12.3.3 and above. Old versions of the script inadvertently depended on things which are being tidied up in the next Opus update.

#7

3 posts were split to a new topic: Reuse existing viewer window


Reuse existing viewer window
#8

Came here because the version I used threw errors in the console all the time, it still was v1.0.
v1.3 does not anymore, thank you! o)

Took the chance to update by using (updated, yet unreleased) ScriptWizard, 1st real life test passed!
The tidy version information you provide here make this possible, thanks for that as well! o)


#9

I'm running DO v12.6 (script requires min_version = "12.2" so I assume it should be OK) but I'm not getting any interaction between the "View in Directory Opus" viewer window and the lister window.

Using the "Viewer Pane" it seems to work as expected.

IE, selecting other items in the lister does not update the viewer…

The “viewer pane” works just fine though…


#11

The script makes the file display track the file currently displayed by the standalone viewer window, not the other way around.


#12

8 posts were split to a new topic: JScript Engine is broken on my machine