Page 1 of 2

Adding up the Length column

Posted: 17 Jul 2014 20:43
by aurumdigitus
In working with MP3's have a List view set up that shows the Length column as well as some other relevant data.

Question: Is there any feature of XY that will sum the Lengths shown to give a grand total?

Re: Adding up the Length column

Posted: 17 Jul 2014 21:11
by TheQwerty
Nothing built-in but you can use Report + Eval to calculate the length.

Code: Select all

"Calculate Length"
	// Retrieve the lengths as a mathematical equation.
	// You may need to adjust the '{prop:*Length}' for your system.
	Global $G_DURATION = Report('+{prop:*Length}', 0 /*use 1 for selection instead*/, 0);

	// Remove items which didn't have a length.
	$G_DURATION = FormatList($G_DURATION, 'tue', '+');

	// Expand 'hh:mm:ss' to 'hh*60*60+mm*60+ss' so we calculate total seconds.
	$G_DURATION = RegexReplace($G_DURATION, '(\d\d):(\d\d):(\d\d)', '$1*60*60+$2*60+$3');

	// Do the math.
	$G_DURATION = eval($G_DURATION);

	Sub '_formatDuration';

	Echo $G_DURATION;

"Format Duration in Seconds : _formatDuration"
	Global $G_DURATION;
	$Secs = $G_DURATION;

	// Just stealing Don's code since I don't feel like doing this right now:
	// Src: http://www.xyplorer.com/xyfc/viewtopic.php?p=57847#p57847
	// format duration given in seconds
	$DAYS_PER_YEAR = 365.25;
	$HOURS_PER_DAY = 24;
	$MINS_PER_HOUR = 60;
	$SECS_PER_MIN = 60;
	$duration = "";

	$Mins = $Secs \ $SECS_PER_MIN;
	$Hours = $Mins \ $MINS_PER_HOUR;
	$Days = $Hours \ $HOURS_PER_DAY;
	$Years = $Days \ $DAYS_PER_YEAR;
	If ($Years > 0) {
		$duration = $Years . "y";
	}
	$Days = $Days % $DAYS_PER_YEAR;
	If ($Days > 0) {
		$duration = $duration . " " . $Days . "d";
	}
	$Hours = $Hours % $HOURS_PER_DAY;
	If ($Hours > 0) {
		$duration = $duration . " " . $Hours . "h";
	}
	$Mins = $Mins % $MINS_PER_HOUR;
	If ($Mins > 0) {
		$duration = $duration . " " . $Mins . "min";
	}
	$Secs = $Secs % $SECS_PER_MIN;
	If ($Secs > 0) {
		$duration = $duration . " " . $Secs . "sec";
	}

	$G_DURATION = $duration;

Re: Adding up the Length column

Posted: 17 Jul 2014 21:49
by aurumdigitus
Thank you for the code that came with such celerity!

This must be crucial: // You may need to adjust the '{prop:*Length}' for your system. Do not comprehend and thus unable to implement the change. Instead, when XYS is run am presented with the screen shot below.

Moral - Some days you're the windshield, other days you're the bug. :cry:

Re: Adding up the Length column

Posted: 17 Jul 2014 22:22
by TheQwerty
aurumdigitus wrote:This must be crucial: // You may need to adjust the '{prop:*Length}' for your system. Do not comprehend and thus unable to implement the change. Instead, when XYS is run am presented with the screen shot below.
That error shouldn't be related to that comment.

If you see a length value after running the following script with your music files selected you don't need to mess with that line:

Code: Select all

Text Report("{prop:*Length}<crlf>", 1);
Otherwise, you need to figure out the correct Property name or index.

Your error is about it not finding the script to format the duration, so give the attached file a try instead.
Duration.xys
(1.59 KiB) Downloaded 228 times

Re: Adding up the Length column

Posted: 18 Jul 2014 00:22
by klownboy
Worked great for me (also USA...as far as "length" prop working). This may come in handy. It looks like the only prerequisite is to have an mp3 folder selected such that the files are shown in the list panel (i.e., doesn't seem to matter if any, one, or all are actually selected which is also nice). Might be nice for a trigger. Of course each mp3 file already has one trigger "click" to play them.
Thanks TheQwerty for the script and aurumdigitus fot the idea / request.
Ken

Re: Adding up the Length column

Posted: 18 Jul 2014 01:04
by TheQwerty
It just uses report to retrieve the lengths so you can change it to only report on the selected items or an item list generated through other means.

Or you could change it to use folderreport and recursively retrieve it for a folder and all its children.

I'm not sure how slow that would be and you might discover some limitations in the length of the generated string which gets evaled.

Re: Adding up the Length column

Posted: 18 Jul 2014 01:41
by klownboy
I think it's good just the way it is. Most people would have one album (or should I say CD) in a folder and typically they'd want to know the length of a particular mp3 song(s) or entire the CD. Yes, I just noticed you used "0" for the "itemlist" with report for all current items in the list. Thanks again.

Re: Adding up the Length column

Posted: 18 Jul 2014 14:50
by aurumdigitus
@ TheQwerty - To use the tag line from an old 1970's Xerox commercial, "It's a miracle". Duration.xys worked perfectly first time right out of the box. Thanks to you for knowing what magical incantation to perform. :appl:

@ klownboy - it provides a certain measure of satisfaction that others find these little brainstorms of mine useful. :D

Life is funny. I am able to make almost anyone comprehend why long chain carbon polymers make use of SP3 hybridization for their covalent bonding. But writing a decent script - nope. :cry:

Opps - update

Posted: 18 Jul 2014 15:05
by aurumdigitus
Bug report! Have discovered that after initial run the script does not reset. It keeps reporting the same time as the first calculation.

Re: Opps - update

Posted: 18 Jul 2014 15:24
by TheQwerty
aurumdigitus wrote:Bug report! Have discovered that after initial run the script does not reset. It keeps reporting the same time as the first calculation.
Not sure how that can even be possible since the script doesn't store anything.

Re: Adding up the Length column

Posted: 18 Jul 2014 15:53
by aurumdigitus
Only course of action at the moment seems to be to give up on this quest.

Perhaps it should be mentioned that this desktop (and its software) have certain anomalies that ADMIN has never been able to explain or fix. An especially vexing one is that Floating Preview never has been able to display PDF's.
Some days you ride the elevator; other days you get the shaft.

Re: Adding up the Length column

Posted: 18 Jul 2014 18:29
by admin
aurumdigitus wrote:Only course of action at the moment seems to be to give up on this quest.

Perhaps it should be mentioned that this desktop (and its software) have certain anomalies that ADMIN has never been able to explain or fix. An especially vexing one is that Floating Preview never has been able to display PDF's.
Some days you ride the elevator; other days you get the shaft.
:veryconfused:

Take a rest, man...

Re: Adding up the Length column

Posted: 18 Jul 2014 19:49
by klownboy
aurumdigitus, just curious, after you take your "rest", how are you running the script? :) Are you clicking on a mp3 folder in the tree pane such that the songs are displayed in the list pane and then selecting "load script file..." under scripting? Or you can temporarily assign the script to a CTB left or right click to run it. When you run the script that way, what happens and what happens when you then change folders by clicking a different one in the tree and rerun the script? Still the same total time?

Re: Adding up the Length column

Posted: 19 Jul 2014 16:32
by aurumdigitus
@ klownboy - Approach is to select the MP3's in list pane then run script. Have done so from a CTB alone, a CTB that contains a list of favorite scripts and from Scripting | Run Script. No matter which method continue to get varying results.

Tried the different modifications you suggested; no improvement.

Glad it is working for others but have come to believe poltergeist are at work here, the same ones inhibiting PDF displays. :roll:

Re: Adding up the Length column

Posted: 19 Jul 2014 16:58
by klownboy
If you'd rather select item (mp3 files), try changing the "0" to "1" in this line (the 4th line) to see if you get better results.

Code: Select all

Global $G_DURATION = Report('+{prop:*Length}', 0 );
I'm not sure why there's a second "0" after the comment in that line of TheQwerty's original code ?? I may be all wet but, it doesn't appear to be correct according to the help syntax (maybe an extra "0" got in there). The above line works for me when used as I explained in my previous post. If you want to highlight files change the "0" to "1".