Expressions/Scripts/Presets

Moderator: Paul Tuersley

 
vidjuheffex
Topic Author
Posts: 18
Joined: Sun May 06, 2012 2:11 pm

sort to individual folder 1.0b

Mon May 14, 2012 10:03 am

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:
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:

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: 319
Joined: Sat Jun 26, 2004 10:01 am
Location: Folsom, CA
Contact:

Re: sort to individual folder 1.0b

Mon May 14, 2012 4:41 pm

I'd do it like this:

{
   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
Topic Author
Posts: 18
Joined: Sun May 06, 2012 2:11 pm

Re: sort to individual folder 1.0b

Mon May 14, 2012 9:58 pm

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

The first variable:
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:
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: 319
Joined: Sat Jun 26, 2004 10:01 am
Location: Folsom, CA
Contact:

Re: sort to individual folder 1.0b

Tue May 15, 2012 10:52 pm

>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
Topic Author
Posts: 18
Joined: Sun May 06, 2012 2:11 pm

Re: sort to individual folder 1.0b

Tue May 15, 2012 11:00 pm

Thanks, makes sense!

Who is online

Users browsing this forum: No registered users and 3 guests