sort to individual folder 1.0b

All things .jsx

Moderator: Paul Tuersley

Post Reply
vidjuheffex
Posts: 18
Joined: May 6th, 2012, 2:11 pm

hey all,
I have written a new (simple) script. The purpose of this script is to take selected items in your project window and place them in their own folder named after themselves.
It available on http://vidjuheffex.com/scripts/

I had an issue with the first uploaded version of the script:

Code: Select all

app.beginUndoGroup("Sort To Individual Folders");
var mySelectedItems = [];

for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).selected){
    mySelectedItems[mySelectedItems.length] = app.project.item(i);
    }
    else{
        alert("Please select an item(s) in the project bin!");
        break;
        };
    
}

for (var i = 0; i < mySelectedItems.length; i++){
    var mySelection = mySelectedItems[i];
    var compFolder = app.project.items.addFolder(mySelection.name);
    mySelection.parentFolder = compFolder;
}
app.endUndoGroup;
this seemed to be working in CS5.5 (windows 7 64bit) but when I came to work (CS5 boxes) selecting layers and clicking the script would always launch the alert box, the user would have to click it like 20 times and then the script would work?

The current solution is:

Code: Select all

app.beginUndoGroup("Sort To Individual Folders");
var mySelectedItems = [];

for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).selected === true){
    mySelectedItems[mySelectedItems.length] = app.project.item(i);
    };

  }

for (var i = 0; i < mySelectedItems.length; i++){
    var mySelection = mySelectedItems[i];
    var compFolder = app.project.items.addFolder(mySelection.name);
    mySelection.parentFolder = compFolder;

}

app.endUndoGroup;
The obvious difference is that there are no alerts anymore, I also added an '==' comparison to the first 'for' loop (this was what I thought would fix the bug but when I had this and the else{} it was still bugging out.) I left it in since otherwise no comparison was being made.

How can I get the alerts back, I think I've pin pointed where the problem is, just not the solution. Any help would be greatly appreciated thanks!
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

I'd do it like this:

Code: Select all

{
	var mySelectedItems = app.project.selection;

	if (mySelectedItems.length > 0){
		app.beginUndoGroup("Sort To Individual Folders");
		for (var i = 0; i < mySelectedItems.length; i++){
    			var mySelection = mySelectedItems[i];
    			var compFolder = app.project.items.addFolder(mySelection.name);
    			mySelection.parentFolder = compFolder;
		}
		app.endUndoGroup;
	}else{
        	alert("Please select an item(s) in the project bin!");
	}
}
Dan
vidjuheffex
Posts: 18
Joined: May 6th, 2012, 2:11 pm

Thanks Dan! If you don't mind, I have a few questions in regards to that solution.

The first variable:

Code: Select all

var mySelectedItems = app.project.selection;
Does this mean I do not have to loop through to find the selected items, but can merely summon them and their value?


Also:

Code: Select all

var mySelection = mySelectedItems[i];
             var compFolder = app.project.items.addFolder(mySelection.name);
This bit. In my previous code, I passed the selected items into an array so that the number of layers wouldn't change while i is counting. Why does this not matter here?

I see what you did with the undo groups, and that makes sense.

I have updated my code and I will re-upload it to my site, but really for me the most important things is understanding the "why" of my early bugs.

Thanks again!
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

>Does this mean I do not have to loop through to find the selected items, but can merely summon them and their value?

That's correct.

>Why does this not matter here?

It's pretty much equivalent to your loop. mySelectedItems is an array of items selected at the time the script launched.

Dan
vidjuheffex
Posts: 18
Joined: May 6th, 2012, 2:11 pm

Thanks, makes sense!
Post Reply