Autosave layouts after folder change, tab or lister closing

Autosave layouts after folder change, tab or lister closing, save the second layout after 10 changes, and save the third layout after 50 changes.
Autosave-Layouts(v1.4.1).js.txt (2.3 KB)

Summary
// Autosave layouts after folder change, tab or lister closing
// (c) 2023 Ken

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.



// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "Autosave-Layouts";
	initData.version = "1.4.1";
	initData.copyright = "(c) 2023 Ken";
	initData.url = "https://resource.dopus.com/t/automatically-save-layouts-after-folder-changes-or-when-tab-are-closed/45033";
	initData.desc = "Autosave layouts after folder change, tab or lister closing, save the second layout after 10 changes, and save the third layout after 50 changes.";
	initData.default_enable = true;
	initData.min_version = "12.0";
}



// Called when Directory Opus starts up
function OnStartup(startupData)
{
    Script.vars.Set('fdrChangeNotCount') = 1;   // Avoid triggering count when starting Opus
	DOpus.Delay(30000);
	Script.vars.Delete('fdrChangeNotCount');
}

// Called after a new folder is read in a tab
function OnAfterFolderChange(afterFolderChangeData)
{
    Autosave()
}

// Called when a tab is closed
function OnCloseTab(closeTabData)
{
    Autosave()
}

// Called when a Lister is closed
function OnCloseLister(closeListerData)
{
    Autosave()
}

function Autosave() {
    if (Script.vars.Exists('fdrChangeNotCount'))   // Avoid triggering count when starting Opus
	    return

    var cmd = DOpus.Create().Command;
    if (Script.vars.Exists('folderChangeCount')) {
	    var folderChangeCount = Script.vars.Get('folderChangeCount');
		switch (folderChangeCount) {
	        case 10:
      		   cmd.RunCommand('Prefs LAYOUTNAME="Autosaved-Layout2" LAYOUTSAVE');
			   Script.vars.Set('folderChangeCount') = folderChangeCount+1;
			   break
		    case 50:
      		   cmd.RunCommand('Prefs LAYOUTNAME="Autosaved-Layout3" LAYOUTSAVE');
			   Script.vars.Set('folderChangeCount') = 0;
			   break
		    default:
			   cmd.RunCommand('Prefs LAYOUTNAME="Autosaved-Layout" LAYOUTSAVE');
		       Script.vars.Set('folderChangeCount') = folderChangeCount+1;
			   if (folderChangeCount > 50)
			      Script.vars.Set('folderChangeCount') = 1;
		}
    } else
		Script.vars.Set('folderChangeCount') = 1;
}
3 Likes

thanks!

OnCloseTab doesn't seem to work as it should?

try to reproduce: open 3 tabs, close one, "crash" dopus (kill process in task manager)
restart dopus: 3 tabs show up including the closed one

now open 4 tabs, close 2 tabs, crash, restart: 3 tabs show up
like OnCloseTab is lagging one event behind?

It looks like the script can't change that behavior, the script just runs the command to save the layout.

1 Like