Guest User

Grouper Script

a guest
Nov 5th, 2014
177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.beginUndoGroup("Grouper");
  2.  
  3. var activeItem = app.project.activeItem;
  4.  
  5. var xmin;
  6. var xmax;
  7. var ymin;
  8. var ymax;
  9.  
  10. //If focus is on Layers
  11. if ((activeItem != null) && (activeItem instanceof CompItem)){
  12.  
  13.         selectedLayers = activeItem.selectedLayers;
  14.        
  15.         if(selectedLayers.length>0) {
  16.  
  17.             minIndex = selectedLayers[0].index;
  18.             xmin = xmax =  selectedLayers[0].property("Position").value[0];
  19.             ymin = ymax =  selectedLayers[0].property("Position").value[1];
  20.            
  21.             activeItem.layers.addNull();
  22.             var newNull = activeItem.layer(1);
  23.             newNull.name = "Group Null";
  24.  
  25.             var ctr= 0;
  26.             var minIndex;
  27.  
  28.             for each (lay in selectedLayers){
  29.                     ctr++;
  30.                     minmaxX( lay.property("Position").value[0] );
  31.                     minmaxY( lay.property("Position").value[1] );
  32.             }
  33.             xpos = ((xmax-xmin)/2)+xmin;
  34.             ypos = ((ymax-ymin)/2)+ymin;
  35.             newNull.property("Position").setValue([xpos,ypos]);
  36.  
  37.             for each (lay in selectedLayers){
  38.                     lay.parent = newNull;
  39.                     if(lay.index<minIndex){minIndex = lay.index;}
  40.             }
  41.             myLay = activeItem.layers[minIndex]
  42.             if(myLay.index>1){newNull.moveAfter(myLay);}
  43.         }
  44. }
  45.  
  46. // If focus is on Project Window
  47. else {
  48.         var mySelectedItems = [];
  49.         for (var i = 1; i <= app.project.numItems; i++){
  50.           if (app.project.item(i).selected)
  51.             mySelectedItems[mySelectedItems.length] = app.project.item(i);
  52.         }
  53.  
  54.         var myFolder = app.project.items.addFolder("Untitled Group");
  55.        
  56.         var tempFolder = mySelectedItems[0].parentFolder;
  57.         var sameFolder = true;
  58.  
  59.         for each(item in mySelectedItems){
  60.             if(item.parentFolder!=tempFolder){sameFolder = false;}
  61.         }
  62.    
  63.         if(sameFolder){
  64.             for each(item in mySelectedItems){
  65.                 item.parentFolder = myFolder;item.selected=false;
  66.             }
  67.             myFolder.parentFolder = tempFolder;
  68.         } else {
  69.             for each(item in mySelectedItems){
  70.                 item.parentFolder = myFolder;item.selected=false;
  71.             }
  72.         }
  73.         myFolder.selected=true;      
  74. }
  75.  
  76. // Placement functions
  77. function minmaxX(xIn){
  78.         if(xIn>xmax){ xmax = xIn; }
  79.         if(xIn<xmin){ xmin = xIn; }
  80. }
  81.  
  82. function minmaxY(yIn){
  83.         if(yIn>ymax){ ymax = yIn; }
  84.         if(yIn<ymin){ ymin = yIn; }      
  85. }
RAW Paste Data