Expanded Folders Supported Functions

I adapted some of the most common functions to support expanded folders and take them into account (execute them over the expanded folder instead of the sourcetab path).

  • Create New File
function sanitizeFolderName(input) {
    // Define a regular expression to match invalid characters
    var invalidChars = "\\/:*?\"<>|";
    var sanitized = "";
	
    // Loop through each character in the input string
    for (var i = 0; i < input.length; i++) {
        var char = input.charAt(i);
        // Add the character to the result if it's not invalid
        if (invalidChars.indexOf(char) === -1) {
            sanitized += char;
        }
    }
	
    return sanitized;
}


function createFile(filePath) {
    try {
        // Create a Path object
        var path = DOpus.FSUtil.NewPath(filePath);
		
        // Open the file for writing (mode "w" creates or overwrites the file)
        var file = DOpus.FSUtil.OpenFile(path, "w");
		
        // Check if the file was successfully opened
        if (!file) {
			DOpus.Output("ERROR -1. Failed to create the file: " + filePath);
            return -1;
        }
		
        // Optionally, write some content to the file
        //file.Write("This is a test file created in Directory Opus.\n");
		
        // Close the file
        file.Close();

		DOpus.Output("File successfully created: " + filePath);
        return 0;
		
    } catch (e) {
        // Handle any errors
		DOpus.Output("ERROR -2. An error occurred: " + e.message);
        return -2;
    }
}


function OnClick(clickData)
{
	DOpus.ClearOutput();
	var cmd = clickData.func.command;
	// --------------------------------------------------------
	
// Get the focused item path
	var tab = clickData.func.sourcetab;
	var focused_item = tab.GetFocusItem();
	var focused_path;
	// If the focused item is a dir, also take it into account
	if (focused_item.is_dir) {
		focused_path = focused_item.realpath;
	} else {
		focused_path = focused_item.path;
	}
	DOpus.Output("focused_path: " + focused_path);
	// --------------------------------------------------------
	
// Ask the new file name
		var subFolderName = "";
		var dlg = clickData.func.Dlg;
		dlg.title = "Create New File";
	    dlg.message = "Enter the name name of the file you want to create:";
	    dlg.buttons = "OK|Cancel";
	    dlg.icon = "question";
		dlg.max = 256;
		dlg.defvalue = '';
		dlg.select = true;
		dlg.window = clickData.func.sourcetab; // Attach dialog to the current tab
	    
		dlg.Show();
		var user_input = dlg.input;
		
		if (dlg.result == 0 || user_input == "")
			return;
		
		// Remove invalid characters from user_input
		sanitized_user_input = sanitizeFolderName(user_input);
		if (sanitized_user_input == "") {
			dlg.message = "The entered file name: '" + user_input + "' is invalid.";
		    dlg.buttons = "OK";
		    dlg.icon = "error";
			dlg.max = undefined;
			dlg.defvalue = undefined;
			dlg.select = undefined;
			dlg.Show();
			
			return;
		}
		if (user_input != sanitized_user_input) {
			dlg.message = "The invalid characters were removed from the entered file name.";
		    dlg.buttons = "OK";
		    dlg.icon = "info";
			dlg.max = undefined;
			dlg.defvalue = undefined;
			dlg.select = undefined;
			dlg.Show();
		}
	// --------------------------------------------------------
	
// File extension
	var new_file_path = DOpus.FSUtil.Resolve(focused_path);
	new_file_path.Add(sanitized_user_input);
	// Default extension is .txt
	if (new_file_path.ext == "") {
		new_file_path.ext = ".txt"
	}
	DOpus.Output("new_file_path: " + new_file_path);
	// --------------------------------------------------------
	
// Valid path?
	var dopusPath = DOpus.FSUtil.Resolve(new_file_path);
    if (dopusPath == null) {
        return "The path is invalid.";
    }
    // --------------------------------------------------------
// File exists?
	if (new_file_path.is_dir) {
        return "The path is valid and points to an existing folder.";
    } else if (new_file_path.is_file) {
        return "The path is valid and points to an existing file.";
    } else {
        //return "The path is valid but does not exist.";
// Create file
		var result = createFile(new_file_path);
		if (result != 0) {
			clickData.func.command.Dlg.Request("Error creating file", "OK", "File Creation");
		}
// Select file
// The problem is that DOpus Select command doesn't accept the full path,
// neither relative path from sourcetab.path. It only accepts file names,
// so if there are multiple files with the same name in other expanded folders,
// they also get selected
		cmd.RunCommand('Select NONE');
		cmd.RunCommand('Select "' + new_file_path.filepart + '" SETFOCUS');
    }

}
  • Create Folder
function OnClick(clickData)
{
	DOpus.ClearOutput();
	var cmd = clickData.func.command;
	// --------------------------------------------------------
	
// Get focues item path
	var tab = clickData.func.sourcetab;
	var focused_item = tab.GetFocusItem();
	var focused_path;
	// If the focused item is a dir, also take it into account
	if (focused_item.is_dir) {
		focused_path = focused_item.realpath;
	} else {
		focused_path = focused_item.path;
	}
	DOpus.Output("focused_path: " + focused_path);
	// --------------------------------------------------------

// Get the relative path from sourcetab.path onwards
	var focused_relative_path = DOpus.FSUtil.Resolve(focused_path);
	focused_relative_path.ReplaceStart(tab.path + "\\", "");
	DOpus.Output("focused_relative_path: " + focused_relative_path);
	// --------------------------------------------------------	

// Call CreateFolder Window
	cmd.RunCommand('CreateFolder "' + focused_relative_path + '\\New Folder" ASK');

}
  • Move Selected Items to New Folder
function sanitizeFolderName(input) {
    // Define a regular expression to match invalid characters
    var invalidChars = "\\/:*?\"<>|";
    var sanitized = "";
	
    // Loop through each character in the input string
    for (var i = 0; i < input.length; i++) {
        var char = input.charAt(i);
        // Add the character to the result if it's not invalid
        if (invalidChars.indexOf(char) === -1) {
            sanitized += char;
        }
    }
	
    return sanitized;
}

function OnClick(clickData)
{
	// Check if any items are selected
    if (clickData.func.sourcetab.selected.count > 0) {
        // Get the first selected file
        var selectedItem = clickData.func.sourcetab.selected(0);
        // Get the path of the selected item
        var selectedPath = selectedItem.path;
        // Display the path
        DOpus.Output("Focused selected file path: " + selectedPath);

		//Ask the new sub folder name
		var subFolderName = "";
		var dlg = clickData.func.Dlg;
		dlg.title = "Move items to sub folder";
	    dlg.message = "Enter the name name of the folder you want to move the selected items to:";
	    dlg.buttons = "OK|Cancel";
	    dlg.icon = "question";
		dlg.max = 256;
		dlg.defvalue = '';
		dlg.select = true;
		dlg.window = clickData.func.sourcetab; // Attach dialog to the current tab
	    
		dlg.Show();
		var user_input = dlg.input;
		
		if (dlg.result == 0 || user_input == "")
			return;
		
		// Remove invalid characters from user_input
		sanitized_user_input = sanitizeFolderName(user_input);
		if (sanitized_user_input == "") {
			dlg.message = "The entered folder name: '" + user_input + "' is invalid.";
		    dlg.buttons = "OK";
		    dlg.icon = "error";
			dlg.max = undefined;
			dlg.defvalue = undefined;
			dlg.select = undefined;
			dlg.Show();
			
			return;
		}
		if (user_input != sanitized_user_input) {
			dlg.message = "The invalid characters were removed from the entered folder name.";
		    dlg.buttons = "OK";
		    dlg.icon = "info";
			dlg.max = undefined;
			dlg.defvalue = undefined;
			dlg.select = undefined;
			dlg.Show();
		}

		// Concat the input with the current folder path
		var newFolderPath = DOpus.FSUtil.Resolve(selectedPath + "\\" + sanitized_user_input);
		DOpus.Output("Move items to: " + newFolderPath);

		// Move the items
		cmd.RunCommand('Copy MOVE CREATEFOLDER "' + newFolderPath + '" QUEUE=none');

		// Select new folder
		cmd.RunCommand('Select NONE');
		cmd.RunCommand('Select "' + sanitized_user_input + '" SETFOCUS');
	}
	
}

  • Archive
Copy ARCHIVE=.7z,comp:best TO="{filepath|..}"
  • Archive as Separate Files
Copy ARCHIVE=.7z,single,comp:best TO={filepath|..}
  • Extract
Copy EXTRACT TO={filepath|..}
  • Extract Smart
function OnClick(clickData)
{
	DOpus.ClearOutput();
	// --------------------------------------------------------
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection
	// --------------------------------------------------------
	var dlg = clickData.func.Dlg;

	var factory = DOpus.Create();
	var date = factory.Date();
	// --------------------------------------------------------
	var merge = false;
	
	var archive_extensions = [".7z", ".iso", ".rar", ".zip"];
	var extension_match = false;
	var archive_basename;
	// For each selected item
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext()) {
		// File
		if (eSel.item().is_dir == false) {
			DOpus.Output("    Extensão = " + eSel.item().ext);
			// Archive?
			for (var i = 0; i < archive_extensions.length; i++) {
			    if (eSel.item().ext === archive_extensions[i]) {
			        extension_match = true;
			        break;  // Exit the loop early if a match is found
			    }
			}
			// Archive
			if (extension_match) {
				archive_basename = eSel.item().name_stem;
				
				subfolder = eSel.item().path + "\\" + archive_basename;
				
				if (DOpus.FSUtil.Exists(subfolder)) {
					// Warn the user that there is already a folder with the same name
					var answer;
					dlg.window = clickData.func.sourcetab;
				    dlg.message = 'Watch out\r\n\r\n' + eSel.item().RealPath + '\r\n\r\nFolder "' + archive_basename + '" already exists.';
				    dlg.title = "Warning";
				    dlg.buttons = "&Merge|&Cancel"; 
				    dlg.icon = "warning";
				    answer = dlg.show();

					// Merge
					if (answer == 1) {
						merge = true;

						// Temp folder
						merge_folder = subfolder + ' (' + date.Format("D#yyyy-MM-dd  T#hh-mm-ss") + ')';
					}
					// Cancel
					else {
						continue;
					}
				}
				
				if (merge) {
					// Extract to temp folder
					extract_to = merge_folder;
				}
				else {
					// Extract to final folder
					extract_to = subfolder;
				}
				cmd.RunCommand('Copy "' + eSel.item().RealPath + '" EXTRACT TO "' + extract_to + '"');

				// 1 folder only?
				var move_items_to_parent = true;
				var count_folders = 0;
				var move_from;
				var folderEnum = DOpus.FSUtil.ReadDir(extract_to);
				while (!folderEnum.complete) {
					var folderItem = folderEnum.Next();
					if (folderItem.is_dir) {
						count_folders += 1;
						move_from = folderItem.RealPath;
					
						// 2+ folders
						if (count_folders > 1) {
							move_items_to_parent = false;
							break;
						}
					}
					else {
						move_items_to_parent = false;
						break;
					}
				}
				// 1 folder only
				if (move_items_to_parent) {
					cmd.RunCommand('Copy "' + move_from + '\\*" MOVE TO "' + extract_to + '"');

					cmd.RunCommand('Delete "' + move_from + '" RECYCLE');
				}

				// Merge
				if (merge) {
					// Move from temp to final folder
					cmd.RunCommand('Copy "' + merge_folder + '\\*" MOVE TO "' + subfolder + '"');

					// Delete temp folder
					cmd.RunCommand('Delete "' + merge_folder + '" RECYCLE');
				}
			}
		}
	}
}

4 Likes