7 Replies Latest reply on Jun 15, 2013 9:23 AM by Ben_Lindsey

    Creating Multiple Artboards

    Ben_Lindsey

      I thought I'd ask this question here seeing as how everything I've learned about scripting has come from the knowledge everyone shares here (granted, I've only been at this a couple weeks now).

       

      I've searched and searched and have been trying for three days to figure this out but can't.  I've studied the scripts at http://forums.adobe.com/message/5082308#5082308 and tried implementing them but to no avail.

       

      Basically, I have a document with multiple layers, all containing only one item each.  Every placedItem is a tiff, all having the same dimensions.  There could be either 4 or 6 of them, no more, no less.  I'd like to split them up onto multiple artboards but whenever I try, I keep getting multiple boards put in the same location, e.g., stacked on top of one another.  I can't seem to figure out the gridByRow command.

       

      So far, I'm using the following script (that I found here) to build the file, which I've altered to fit my needs.

       

       

      function getFolder() {
                return Folder.selectDialog('Please select the folder to be imported:', Folder('~'));
      }
      
      
      function importFolderAsLayers(selectedFolder) { 
                // if a folder was selected continue with action, otherwise quit
                var myDocument;
      
      
                if (selectedFolder) {
                          myDocument = app.documents.add();
      
                          var firstImageLayer = true;
                          var newLayer ;
                          var thisPlacedItem;
      
                          // create document list from files in selected folder
                          var imageList = selectedFolder.getFiles();
      
                          for (var i = 0; i < imageList.length; i++) {
                                    // open each document in file list
                                    if (imageList[i] instanceof File) {
                                              // get the file name
                                              var fName = imageList[i].name;
                                              // check for supported file formats
                                              //if( (fName.indexOf(".eps") == -1) ) {
                                              if( (fName.indexOf("Base.tif") == -1) && (fName.indexOf("White.tif") == -1) && (fName.indexOf("Process%20Yellow.tif") == -1) && (fName.indexOf("Process%20Cyan.tif") == -1) && (fName.indexOf("Process%20Magenta.tif") == -1) && (fName.indexOf("Process%20Black.tif") == -1)) {
                                                        // skip unsupported formats
                                                        continue;
                                              } else {
                                                        if( firstImageLayer ) {
                                                                  newLayer = myDocument.layers[0];
                                                                  firstImageLayer = false;
                                                        } else {
                                                                  newLayer = myDocument.layers.add();
                                                        }
                                                 // Give the layer the name of the image file
                                                 newLayer.name = fName.substring(0, fName.indexOf(".") );
      
                                                 // Place the image on the artboard
                                                 thisPlacedItem = newLayer.placedItems.add()
                                                 thisPlacedItem.file = imageList[i];
      
      
                                              }
                                    }
                          }
      
      

       

      What I'd like to do at this point is spread these layers out to multiple artboards, which have been made based on the number of layers in the file.

       

      3 days!  I'm pulling my hair out.  Can someone help?  Using CS5.

        • 1. Re: Creating Multiple Artboards
          CarlosCanto Adobe Community Professional & MVP

          welcome to scripting, what's the part you're having trouble understanding?

           

          your script above is missing a couple of closing brackets and the function calls, that sorted, there's no code to create the rest of the artboards...

           

          ...here's a working version, for testing purposes, this script places ALL TIF images in a selected folder, comment out the dummy line (line 45) I created and uncomment you original line below it.

           

          // http://forums.adobe.com/thread/1231519?tstart=0
          // add images in a folder (one artboard per image) 
          
          
          var selectedFolder = getFolder ();
          importFolderAsLayers (selectedFolder);
          
          
          function getFolder() {
                    return Folder.selectDialog('Please select the folder to be imported:', Folder('~'));
          }
          
          function importFolderAsLayers(selectedFolder) { 
              // if a folder was selected continue with action, otherwise quit
              var myDocument;
          
          
          
          
              if (selectedFolder) {
                          // create document list from files in selected folder
                  var imageList = selectedFolder.getFiles('*.tif');
                  var fileCount = imageList.length;
                  var spacing = 10;
                  
                  // create the document with as many artboards as images selected
                  myDocument = app.documents.add(
                      DocumentColorSpace.RGB,
                      undefined,
                      undefined,
                      fileCount,
                      DocumentArtboardLayout.Row,
                      spacing);
                                                                          
          
          
                  var firstImageLayer = true;
                  var newLayer ;
                  var thisPlacedItem;
          
          
          
          
                  for (var i = 0; i < fileCount; i++) {
                      // open each document in file list
                      if (imageList[i] instanceof File) {
                          // get the file name
                          var fName = imageList[i].name;
                          // check for supported file formats
                          //if( (fName.indexOf(".eps") == -1) ) {
                          
                          if (fName=='o') { // dummy line, uncomment this line and comment line below to place all images in a folder
                         // if( (fName.indexOf("Base.tif") == -1) && (fName.indexOf("White.tif") == -1) && (fName.indexOf("Process%20Yellow.tif") == -1) && (fName.indexOf("Process%20Cyan.tif") == -1) && (fName.indexOf("Process%20Magenta.tif") == -1) && (fName.indexOf("Process%20Black.tif") == -1)) {
                                    // skip unsupported formats
                                    continue;
                          } else {
                                    if( firstImageLayer ) {
                                              newLayer = myDocument.layers[0];
                                              firstImageLayer = false;
                                    } else {
                                              newLayer = myDocument.layers.add();
                                    }
                             // Give the layer the name of the image file
                             newLayer.name = fName.substring(0, fName.indexOf(".") );
          
          
                             // Place the image on the artboard
                             var ab = myDocument.artboards[i];
                             myDocument.activeView.centerPoint = getCenterPoint(ab); // get center of artboard coordinates [x,y];
                             thisPlacedItem = newLayer.placedItems.add()
                             thisPlacedItem.file = imageList[i];
                          }
                      }
                  }
              }
          }
          
          
          // get center point of supplied object, returns a point (x,y),  object is a Document, Artboard, or a single pageItem
          function getCenterPoint(object) {
              type = object.typename;
              //alert(type);
          
          
              switch (type) {
                  case "Document":
                      var bounds = object.artboards[object.artboards.getActiveArtboardIndex()].artboardRect;
                      break;
                      
                  case "Artboard":
                      var bounds = object.artboardRect;
                      break; 
                      
                  default:
                      var bounds = object.visibleBounds;
                      
              }
          
          
              //var bounds = path.geometricBounds; // left, top, right, bottom
              var x = bounds[0]+(bounds[2]-bounds[0])/2; // left + width/2
              var y = bounds[3]+(bounds[1]-bounds[3])/2; // bottom + height/2
              return pathCtrPoint = Array(x,y); // [50, 50] for example 
          }
          
          1 person found this helpful
          • 2. Re: Creating Multiple Artboards
            Ben_Lindsey Level 1

            Thank you so much for replying!  I've implemented the code you've provided and can't thank you enough.  I have ran your rewritten script and upon placing the files, the artboards are all there but the files are much larger than the artboards.  As a result, all of the placedItems overlap one another.

             

            Is there a way to set the visibleBounds to the same size as the placedItems and then give them a buffer around the sides of about 3.5cm? 

             

            Thanks again for your expertise!

            • 3. Re: Creating Multiple Artboards
              CarlosCanto Adobe Community Professional & MVP

              you're welcome,

               

              you can change the "buffer" in line 20

               

              var spacing = 10;
              

               

              for now, you can change the width and height of the artboards in line 26 and 27, where it says "undefined", change it to your needs.

               

                      // create the document with as many artboards as images selected
                      myDocument = app.documents.add(
                          DocumentColorSpace.RGB,
                          undefined,
                          undefined,
                          fileCount,
                          DocumentArtboardLayout.Row,
                          spacing);
              

               

              to make the artobard the same size as the images from the beginning it would need a different approach, to resize the artboards after the images have been placed you could use the document method

              fitArtboardToSelectedArt()
              
              1 person found this helpful
              • 4. Re: Creating Multiple Artboards
                Muppet Mark Level 5

                Hum… Im not sure I get what you want… Stacking in layers one way to work… artboards another way… do you really need to do both…?

                 

                There are plenty of options of how to go about this… Do the files already exist in an open document or are you wanting to add to new one…?

                • 5. Re: Creating Multiple Artboards
                  Ben_Lindsey Level 1

                  Thanks again, Carlos!  I will attempt to make those changes and give you an update once I do.  I seriously can't thank you enough for your troubles!

                   

                  Update: I've implemented your script and made the suggested adjustments.  Since the images I'm working with will vary in size from 2" wide to up to 17" wide, I think it would be better to add the artboards after the images have been placed.  I've come up with somewhat of a solution below but am still left with a few steps out of my very limited abilities.

                   

                  Hum… Im not sure I get what you want… Stacking in layers one way to work… artboards another way… do you really need to do both…?

                   

                  There are plenty of options of how to go about this… Do the files already exist in an open document or are you wanting to add to new one…?

                  MuppetMark,

                   

                  Here's a run down of what I'm trying to do and how I've gone about approaching it.  This all tails on the backend of a few scripts I've created for Photoshop that will export a handful of tiffs to a single folder.  There could be anywhere from 4 to 6 tiffs and those tiffs could range in size from job to job.  However, per each job, they will all be the same size, meaning that for job 1, every tiff in the folder will be identical in dimension and for job 2, they could all share a different dimension.  The artwork itself is created for tees and could differ dramatically in size, i.e., from 2" wide to 15" wide.

                   

                  Here's where I am so far with my script.

                   

                  importFolderAsLayers( getFolder() ); - Get's all files I need into the AI doc onto individual layers - 1 tiff per layer.

                  renameLayers(); - Rename layers to A, B, C, D, E, F depending on their contents (need these stacked a certain way)

                  sortLayers(); - Sort layers stacking order alphabetically

                  renameLayersAgain(); - Change layer names back to reflect their contents, e.g., Black, Cyan, Magenta, etc...

                   

                  Everything above is working nicely.

                   

                  From here, I would like to add as many artboards as there are layers, the artboardRect being dictated by the size of the placed tiffs.  I'd then like to loop through the layers and translate each pageItem along the X axis to fit inside the artboards.  After quite a bit of fiddling, I can get the artboards created based on how many layers there are but not based on the pageItem's size and I don't believe I've even come close to figuring out the translate math needed to move the pageItems.

                   

                  Any help is greatly appreciated.

                   

                  Message was edited by: Ben_Lindsey

                  • 6. Re: Creating Multiple Artboards
                    Muppet Mark Level 5

                    There are a handfull of ways you could go about scripting this… Here is an example of how I would go about it…

                     

                    I avoid EPS's files where ever possible so this works with TIFF's

                     

                    As is… it expects all image files to be the same size ( it only tests the first one )

                     

                    You could test each if the size varies… I've not included the layer sorting but that is easy stuff…

                     

                     

                    #target illustrator
                    
                    getImages();
                    
                    /*
                              Get files from user folder selection
                    */
                    function getImages() {
                      
                              var inFold, files, size;
                      
                              inFold = Folder.selectDialog( 'Wheres the TIFF files?' );
                      
                              if ( inFold != null ) {
                      
                                        files = inFold.getFiles( /\.tif$/i );
                      
                                        if ( files.length > 0 ) {
                      
                                                  loadXMPLibrary();
                      
                                                  size = getImageXMP( files[ 0 ] );
                      
                                                  unloadXMPLibrary();
                      
                                                  placeImages( files, size );
                      
                                        };
                      
                              } else { return; }
                      
                    };
                    
                    /*
                              The main AI document process
                    */
                    function placeImages( list, size ) {
                      
                              app.coordinateSystem =
                              CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
                      
                              var i, abd, cm, doc, item, lay;
                      
                              cm = 28.3464567;
                      
                              doc = app.documents.add(
                      
                                        DocumentColorSpace.CMYK,
                                        size[ 0 ] * 72 + 7*cm,
                                        size[ 1 ] * 72 + 7*cm,
                                        list.length,
                                        DocumentArtboardLayout.Row,
                                        72
                      
                              );
                      
                              item = doc.placedItems.add();
                      
                              item.file = list[ 0 ];
                      
                              item.position = [ 3.5*cm, -3.5*cm ];
                      
                              doc.layers[ 0 ].name = list[ 0 ].name.match( /(.*)\.[^\.]+$/ )[1];
                      
                              doc.artboards[ 0 ].name = doc.layers[ 0 ].name;
                      
                              for ( i = 1; i < list.length; i++ ) {
                      
                                        lay = doc.layers.add();
                      
                                        lay.name = list[ i ].name.match( /(.*)\.[^\.]+$/ )[1];
                      
                                        doc.artboards.setActiveArtboardIndex( i );
                      
                                        doc.artboards[ i ].name = lay.name;
                      
                                        item = doc.placedItems.add();
                      
                                        item.file = list[ i ];
                      
                                        item.position = [ 3.5*cm, -3.5*cm ];
                      
                              };
                      
                    };
                    
                    /*
                              Get the XMP properties…
                    */
                    function getImageXMP( filePath ) {
                      
                              var height, width, res, xmpFile, xmp;
                      
                              xmpFile = new XMPFile( filePath.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ );
                                         
                              xmp = xmpFile.getXMP();
                      
                              res = xmp.getProperty( 'http://ns.adobe.com/tiff/1.0/', 'XResolution' );
                      
                              res = res.toString().split( '/' );
                      
                              res = res[0] / res[1];
                      
                              height = xmp.getProperty( 'http://ns.adobe.com/tiff/1.0/', 'ImageLength' );
                      
                              height = height / res;
                      
                              res = xmp.getProperty( 'http://ns.adobe.com/tiff/1.0/', 'YResolution' );
                      
                              res = res.toString().split( '/' );
                      
                              res = res[0] / res[1];
                      
                              width = xmp.getProperty( 'http://ns.adobe.com/tiff/1.0/', 'ImageWidth' );
                      
                              width = width / res;
                      
                              return [ width, height ];
                      
                    };
                    
                    /*
                              Load the XMP External Object Library
                    */
                    function loadXMPLibrary() {
                    
                    
                              if ( !ExternalObject.AdobeXMPScript ) {
                      
                                        try {
                      
                                                            ExternalObject.AdobeXMPScript = new ExternalObject( 'lib:AdobeXMPScript' );
                      
                                                            return true;
                    
                    
                                        } catch(e) { return false; };
                      
                              };
                    
                    
                    };
                    
                    /*
                              Unload the XMP External Object Library
                    */
                    function unloadXMPLibrary() {
                    
                    
                              if ( ExternalObject.AdobeXMPScript ) {
                    
                    
                                        try {
                    
                    
                                                  ExternalObject.AdobeXMPScript.unload();
                      
                                                  ExternalObject.AdobeXMPScript = undefined;
                      
                                                  return true;
                    
                    
                                        } catch(e) { return false; };
                    
                    
                              };
                    
                    
                    };
                    
                    • 7. Re: Creating Multiple Artboards
                      Ben_Lindsey Level 1

                      Amazing! Thank you so much for that! I am truly grateful for your help on this, both you and Mr. Canto. Brilliant!