Forums > MaxMSP

Umenu autopopulate – always alphabetical order?


Jun 27 2011 | 1:35 pm

Is there any way to autopopulate in order of date created?


Jan
Jun 27 2011 | 11:13 pm

If you put this JavaScript code into a [js] and send it the message "read path/to/the/folder" it will output all files by date modified/created ascending.

function read(path) {
	var folder = new Folder(path);
	var allFiles = new Array();
	var i = 0;
	 while (!folder.end) {
	 var d = new Date(
			folder.moddate[0],
			folder.moddate[1],
			folder.moddate[2],
			folder.moddate[3],
			folder.moddate[4],
			folder.moddate[5],
			0);
                // sort out hidden files and folders
                // and write others into an array
		if (folder.filename && folder.filetype!="fold") {
			allFiles[i] = {name: folder.filename, date : Number(d)};
			i++;

		}
		folder.next();
	 }
         folder.close();
         // sort the array by date asc
	 allFiles.sort(
		function(a, b){
			return a.date-b.date;
		}
	);
        // and finally send the file names to outlet
	for(var j=0;j<allFiles.length; j++) {
           outlet(0, allFiles[j].name);
        }

}

If you want to change the order to descending change the line:
"return a.date-b.date;"

into:
"return b.date-a.date;"

jan

Jun 28 2011 | 7:43 am

Wow – thank you so much for sharing this.

Awesome!


Jan
Jun 28 2011 | 8:10 am

;) !

Nov 21 2015 | 4:28 pm

Hi,
It’s an old thread, but I’m interested in this js.
When I send "read path…" I get: "no read function"
??
did something change in js since 2011 ?

Thanks,

Yoann


Jan
Nov 21 2015 | 5:18 pm

For some reasons the last lines are missing – also it was written on Windows, just realized that hidden files on Mac were not filtered out. this should work:

function read(path) {
	var folder = new Folder(path);
	var allFiles = [];
	while (!folder.end) {
		var d = new Date(
			folder.moddate[0],
			folder.moddate[1],
			folder.moddate[2],
			folder.moddate[3],
			folder.moddate[4],
			folder.moddate[5],
			0
		);
        // sort out hidden files and folders
        // and write others into an array
		if (
			folder.filename
			&& folder.filetype !="fold"
			&& folder.filename.indexOf(".") != 0
		) {
			allFiles.push({ "name": folder.filename, date : d.getTime()});
		}
	
		folder.next();
	 }
     folder.close();
     // sort the array by date asc
	allFiles.sort(
		function(a, b){
			return a.date-b.date;
		}
	);
    // and finally send the file names to outlet
	for(j in allFiles) {
		outlet(0, allFiles[j]["name"]);
	}
}
Nov 22 2015 | 7:57 am

it’s working.

Thanks !!


Jan
Nov 22 2015 | 8:52 am

.. just saw that the old post used back-ticks for code formatting and they were parsed wrongly. changed them to pre-tags…

Viewing 8 posts - 1 through 8 (of 8 total)

Forums > MaxMSP