GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Button: Random Shuffle

PHPBB_IMPORT_WARNING CODE_NEAR_LI

Requires Directory Opus 12 or above.

[ul][li]Overview:

This script-button shuffles the files and folders in the tab into a random order each time you click it.

Files and folders are mixed together.

Right-clicking the button resets things back to normal.

You might use this for generating music playlists, or similar.


[/li]
[li]Installation:

Download Shuffle.dcf, enter Settings > Customize mode, and drag it to a toolbar. Then click OK in the Customize window.
(See here for more detailed instructions.)

Shuffle.dcf (2.75 KB)

[/li]
[li]History:

[ul][li]1.0 (04/Sep/2016): Initial version.[/li][/ul]

[/li]
[li]Script code:

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.

(JScript)

[code]function OnClick(clickData)
{
var vecNewOrder = DOpus.Create.Vector();
vecNewOrder.reserve(clickData.func.sourcetab.all.count);
for (var eItems = new Enumerator(clickData.func.sourcetab.all); !eItems.atEnd(); eItems.moveNext())
{
vecNewOrder.push_back(eItems.item().name);
}

var currentIndex = vecNewOrder.size;
var temporaryValue;
var randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex)
{
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
	vecNewOrder.exchange(currentIndex, randomIndex);
}

clickData.func.Command.RunCommand("Set MANUALSORT=On");
var sortOrder = clickData.func.sourcetab.format.manual_sort_order;
sortOrder.SetOrder(vecNewOrder);
clickData.func.Command.RunCommand("Select FIRST SETFOCUS");
clickData.func.Command.RunCommand("Select SHOWFOCUS");
clickData.func.Command.RunCommand("Select NONE");

}[/code]

The right-click action runs a simple non-script command:

Set MANUALSORT=off Select FIRST SETFOCUS Select SHOWFOCUS Select NONE[/li][/ul]

Nice!

Could you add an option for shuffle either files or folders only?

+1 to Sasa's request

I would also appreciate an option to shuffle only files (or only folders) depending on the situation

@Leo: Just to let you know, it works great on Local drives but on network shares it fails to Reset - the folder contents remain shuffled.

It seems fine on network drives here. (I also tried a non-NTFS drive and that was OK as well.)