Page 1 of 1

Script: To Clipboard/Show Item Relative Path/Name(s)

Posted: 20 Jun 2013 18:32
by TheQwerty
A script to copy/display the item paths relative to a user-specified root.

Providing a solution for the wish described here.


There's not much to it - just run the script and select 'Copy Relative Paths' or 'Show Relative Paths'.
It will operate on the selection in the current tab or if there is none all visible items.

The script attempts to determine the best base path and then presents the user with a list of all possible roots (with option to browse for others...) and will convert the paths for the items to be relative to the user's selection.


Enjoy!
CopyRelative.xys
(4.61 KiB) Downloaded 174 times

Code: Select all

"Copy Relative Paths : copyRelativePaths"
	Global $G_ITEMS;
	Sub('_getRelativePaths');
	CopyText $G_ITEMS;
	Unset $G_ITEMS;

	/////////////////////////////////////////////////////// end: copyRelativePaths

"Show Relative Paths : showRelativePaths"
	Global $G_ITEMS;
	Sub('_getRelativePaths');
	Text $G_ITEMS, /*width*/, /*height*/, 'Relative Paths...';
	Unset $G_ITEMS;

	/////////////////////////////////////////////////////// end: showRelativePaths


"- : _-" //---------------------------------------------------------------------


"Get Relative Paths : _getRelativePaths"
	// Determine list of items.
	Global $G_ITEMS = Get('SelectedItemsPathNames');

	if (! $G_ITEMS || 0 == GetToken($G_ITEMS, 'Count', "<crlf>")) {
		$G_ITEMS = ListPane('a', '*', '0', "<crlf>");

		if (! $G_ITEMS || 0 == GetToken($G_ITEMS, 'Count', "<crlf>")) {
			End True, "Nothing is selected or displayed.";
		}
	}

	// Get root frequency list...
	Global $G_ROOTS;
	Sub('_getRootFrequencyList');

	// Clean up list.
	$most = GetToken($G_ROOTS, 1, ';');
	$best = FormatList($G_ROOTS, 'f', "<crlf>", "$most;*");
	$rest = FormatList($G_ROOTS, 'f', "<crlf>", "!$most;*");
	Unset $most;

	$best = RegexReplace($best, '^\d+;\d+;', '');
	$rest = RegexReplace($rest, '^\d+;\d+;', '');
	$rest = FormatList($rest, 'dents', "<crlf>");

	$groupPrefix = "---:";
	$groupSuffix = ":----------------------------------------";
	$groupBest   = "$groupPrefix The Best  $groupSuffix";
	$groupRest   = "$groupPrefix The Rest  $groupSuffix";
	$groupBrowse = "$groupPrefix Browse... $groupSuffix";
	Unset $groupPrefix, $groupSuffix;

	$selectList = '';
	if ($best && 0 < GetToken($best, 'Count', "<crlf>")) { 
		$selectList = $selectList . "$groupBest<crlf>$best";
	}

	if ($rest && 0 < GetToken($rest, 'Count', "<crlf>")) { 
		$selectList = $selectList . "$groupRest<crlf>$rest";
	}

	if ($selectList == '') {
		$base = InputFolder("<curfolder>", 'Select the base root...');
	} else {
		$selectList = $selectList . "<crlf>$groupBrowse";

		// Prompt user to select base root.
		$base = '';
		while ($base == '') {
			$base = InputSelect(	'Select the base root...', $selectList, "<crlf>", 1, /*cancel*/, /*width*/, /*height*/, 'Common Roots');
		}

		// Handle special case users...
		    if ($base == $groupBest  ) { $base = GetToken($best, 1, "<crlf>"); }
		elseif ($base == $groupRest  ) { $base = GetToken($rest, 1, "<crlf>"); }
		elseif ($base == $groupBrowse) {
			$base = GetToken($best, 1, "<crlf>");
			$base = InputFolder($base, 'Select the base root...');
		}
	}
	Unset $best, $rest, $groupBest, $groupRest, $groupBrowse, $selectList;


	// Convert paths to relative paths.
	$result = '';
	foreach ($item, $G_ITEMS, "<crlf>", /*flags*/, 'Item list cannot be empty.') {
		$result = $result . ResolvePath($item, $base, 1) . "<crlf>";
	}
	Unset $item, $base;

	// Update the global var.
	$G_ITEMS = $result;
	Unset $result;

	/////////////////////////////////////////////////////// end: _getRelativePaths


"Generate Root Frequency List||4 : _getRootFrequencyList"
	Global $G_ITEMS;
	Global $G_ROOTS = '';

	// Generate list of possible roots.
	$roots = '';
	foreach ($item, $G_ITEMS, "<crlf>", /*flags*/, 'Item list cannot be empty.') {
		// UNC paths complicate things.
		$item = ReplaceList($item, '\\?\;\\', ':LUNC:;:UNC:', ';');

		// Walk up this item's path.
		$i = GetToken($item, 'Count', '\') - 1;
		while ($i > 0) {
			$root = GetToken($item, $i, '\', 't', 1);
			if ($root == '') { break; }
			$roots = $roots . "$root\<crlf>";
			$i--;
		}
	}
	$roots = ReplaceList($roots, ':LUNC:;:UNC:', '\\?\;\\', ';');
	Unset $item, $i, $root;


	// Determine most common root.
	$roots = FormatList($roots, 'tens', "<crlf>");
	$uniqueRoots = FormatList($roots, 'd', "<crlf>");

	$rootCounts = '';
	foreach ($root, $uniqueRoots, "<crlf>") {
		if ($root == '') { continue; }

		// Extract list of matching items.
		$matches = FormatList($roots, 'f', "<crlf>", "$root");
		$roots = FormatList($roots, 'f', "<crlf>", "!$root");
		
		// Get root's frequency and length.
		$count = GetToken($matches, 'Count', "<crlf>");
		$length = StrLen($root);

		$rootCounts = $rootCounts . "$count;$length;$root<crlf>";
	}
	$roots = FormatList($rootCounts, 'trend', "<crlf>");
	Unset $uniqueRoots, $rootCounts, $root, $matches, $count, $length;


	// Now $roots should be sorted by best to least likely common root.
	// It is really sorted twice:
	//		Primarily by Frequency
	//		Secondarily by Root Length
	$G_ROOTS = $roots;
	Unset $roots;

///////////////////////////////////////////////////// end: _getRootFrequencyList
[/size]

Re: Script: To Clipboard/Show Item Relative Path/Name(s)

Posted: 20 Jun 2013 18:32
by TheQwerty
*reserved*

I'll try to remember to clean this up and do all that perfect presentation and details stuff that Marco and FluxTorpedoe do so nicely, but for now I feel it's better to post it than wait till I feel like doing that. ;)

Re: Script: To Clipboard/Show Item Relative Path/Name(s)

Posted: 20 Jun 2013 18:59
by admin
I like this sub-retina messaging. :)