2 Replies Latest reply on Nov 19, 2017 6:54 PM by williamadowling

    copy and paste in new document

    karth80472045 Level 1

      Hello Everyone ,

       

      I am struggling in a script to copy art board  from source file to destination file using script

      but it is not pasting in exact place

       

      please help me anybody how to copy and paste between two different documents .

       

      Thanks and regards,

      yogesh.V

       

      var fileArray,fileArray_buffer,fileArray_save_elements,fileArray_title,fileArray_SM_name;
      var y_connec_coor, x_connec_coor,x_SM_Name_coor,y_SM_Name_coor,x_connector_position,y_connector_position;
      var One_line_SM_name_near_connector;
      var text_frame;
      
      
       var sourceDoc;
      var connector_group_near_SM_name;
      var groups;
      var error_file_name;
      var error_file_name1;
      var error_array = [];
      var error_row = 0;
      var correct_connector_number;
      var sm_name_and_coordinates;
      var sourceFolder,files,sourceFolder1;
      var is_point_text  = [];
      var point_text_line_length=[] ;
      fileArray_title = [];
      fileArray_buffer = [];
      fileArray_save_elements=[];
      fileArray_SM_name = [];
      var connector_array_inside_grp = [];
      
      
      
      
      sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files', '~' );
      sourceFolder1 = Folder.selectDialog( 'Select the folder Template files', '~' );
      var sourceFolder2 = Folder.selectDialog( 'Select the folder To store files', '~' );
      var files = sourceFolder.getFiles(/\.(svg|ai)$/i);
       if ( files.length > 0 )
           {
           //    //alert(files.length);
            
            for ( var file_iteration = 0; file_iteration < files.length; file_iteration++ )
           {
               sourceDoc = app.open(files[file_iteration]);
      sourceDoc=app.activeDocument;
      
      
      
      
      
      
      files1= sourceFolder1.getFiles(/\.(svg)$/i);
      sourceDoc1 = app.open(files1[0]);
       sourceDoc1=app.activeDocument;
      
      
      sourceDoc.activate();
      fitAll(sourceDoc);
      var left = sourceDoc.visibleBounds[0]; 
      var top = sourceDoc.visibleBounds[1]  
      var right = sourceDoc.visibleBounds[2] 
      var bottom =sourceDoc.visibleBounds[3] 
        
      
      
      sourceDoc1.activate();
      
      
      sourceDoc1.artboards[0].artboardRect = [left,top,right,bottom]; 
      fitAll(sourceDoc1);
      
      
      
      
      sourceDoc.activate();
      for(var i=0;i<sourceDoc.layers.length;i++)
      {
          sourceDoc.layers[i].hasSelectedArtwork = true;
          }
      app.copy();
        sourceDoc1.activate();
      app.paste();
      
      
      
      
          // clear any current selection  
      
      var Sel = sourceDoc1.selection;  
        
      if (Sel.length >0 ) {  
          var abIdx = sourceDoc1.artboards.getActiveArtboardIndex();  
          var actAbBds = sourceDoc1.artboards[abIdx].artboardRect;  
        
          var vBounds = Sel[0].visibleBounds;  
          vBounds_Li = vBounds[0];  
          vBounds_Ob = vBounds[1];  
          vBounds_Re = vBounds[2];  
          vBounds_Un = vBounds[3];  
        
      if (Sel.length >1 ) {      
          for (i=1; i<Sel.length ; i++) {  
              vBdsI = Sel[i].visibleBounds;  
              if( vBounds_Li > vBdsI[0] ) {vBounds_Li = vBdsI[0]};  
              if( vBounds_Ob > vBdsI[1] ) {vBounds_Ob = vBdsI[1]};  
              if( vBounds_Re < vBdsI[2] ) {vBounds_Re = vBdsI[2]};  
              if( vBounds_Un < vBdsI[3] ) {vBounds_Un = vBdsI[3]};  
              }  
        
         sourceDoc1.artboards[abIdx].artboardRect = [((vBounds_Re - vBounds_Li)/2-(actAbBds[2]-actAbBds[0])/2), 4+vBounds_Ob -((vBounds_Ob - vBounds_Un)/2+(actAbBds[3]-actAbBds[1])/2), ((vBounds_Re - vBounds_Li)/2-(actAbBds[2]-actAbBds[0])/2)+(actAbBds[2]-actAbBds[0]), vBounds_Ob-((vBounds_Ob - vBounds_Un)/2+(actAbBds[3]-actAbBds[1])/2)+(actAbBds[3]-actAbBds[1])];   
          }  
          } 
      var destFile = getTargetFile (sourceDoc.name, '.svg', sourceFolder2)
      alert(destFile);
      var exportOptions = new ExportOptionsSVG();
      var type = ExportType.SVG;
      var fileSpec = new File(destFile);
      exportOptions.embedRasterImages = true;
      exportOptions.embedAllFonts = false;
      exportOptions.fontSubsetting = SVGFontSubsetting.None;
      
      
      exportOptions.fontType=SVGFontType.SVGFONT;
      sourceDoc1.exportFile( fileSpec, type, exportOptions );
      sourceDoc1.close ();
      sourceDoc.activate();
      sourceDoc.close(SaveOptions.DONOTSAVECHANGES); 
      }
      }
      
      
        
        
      function getTargetFile(docName, ext, destFolder) {
      var newName = "";
          //   var ref_name;
             //alert(newName);
      // if name has no dot (and hence no extension),
      // just append the extension
         
           ref_name="";
      if (docName.indexOf('.') < 0) {
      newName = docName  + ext;
              ref_name = docName;
      } else {
      
      
      var dot = docName.lastIndexOf('.');
      newName += docName.substring(0, dot);
           
      newName += ext;
                   //   ref_name = docName;
                   //   alert(ref_name);
      }
      
      
      var myFile = new File( destFolder + '/' + newName );
      
      // Preflight access rights
      if (myFile.open("w")) {
      myFile.close();
      }
      else {
      throw new Error('Access is denied');
      }
      return myFile;
      }
      
      
      
      
      
      
      
      
      function calcRect(rectArray) {
          var rect = {
              rect  : rectArray.join(", "),
              topLeft    : {
                  x : rectArray[0],
                  y : rectArray[1],
              },
              bottomRight    : {
                  x : rectArray[2],
                  y : rectArray[3],
              },
          };
          rect.width  = Math.abs(rect.bottomRight.x - rect.topLeft.x);
          rect.height = Math.abs(rect.bottomRight.y - rect.topLeft.y);
          rect.center = [
              rect.topLeft.x + (rect.width / 2),
              rect.topLeft.y - (rect.height / 2)
          ];
          rect.aspectRatio = rect.width / rect.height;
          return rect;
      }
      
      
      function calcViewRect(rectArray, zoom) {
          var rect = {
              rect  : rectArray.join(", "),
              topLeft    : {
                  x : rectArray[0],
                  y : rectArray[1],
              },
              bottomRight    : {
                  x : rectArray[2],
                  y : rectArray[3],
              },
          };
          rect.width        = Math.abs(rect.bottomRight.x - rect.topLeft.x);
          rect.height       = Math.abs(rect.bottomRight.y - rect.topLeft.y);
          rect.zoom         = zoom || 1;
          rect.actualWidth  = rect.width * zoom;
          rect.actualHeight = rect.height * zoom;
          rect.aspectRatio  = rect.width / rect.height;
          return rect;
      }
      
      
      function getDocumentBounds(artboards) {
          var rect = {
              topLeft : {
                  x : null,
                  y : null,
              },
              bottomRight : {
                  x : null,
                  y : null,
              },
          };
      
      
          for (var i = 0; i < artboards.length; i++) {
              var artboardRect = calcRect(artboards[i].artboardRect);
      
      
              if (rect.topLeft.x == null) {
                  rect.topLeft.x = artboardRect.topLeft.x;
              } else {
                  if (artboardRect.topLeft.x < rect.topLeft.x) {
                      rect.topLeft.x = artboardRect.topLeft.x;
                  }
              }
      
      
              if (rect.topLeft.y == null) {
                  rect.topLeft.y = artboardRect.topLeft.y;
              } else {
                  if (artboardRect.topLeft.y > rect.topLeft.y) {
                      rect.topLeft.y = artboardRect.topLeft.y;
                  }
              }
      
      
              if (rect.bottomRight.x == null) {
                  rect.bottomRight.x = artboardRect.bottomRight.x;
              } else {
                  if (artboardRect.bottomRight.x > rect.bottomRight.x) {
                      rect.bottomRight.x = artboardRect.bottomRight.x;
                  }
              }
      
      
              if (rect.bottomRight.y == null) {
                  rect.bottomRight.y = artboardRect.bottomRight.y;
              } else {
                  if (artboardRect.bottomRight.y < rect.bottomRight.y) {
                      rect.bottomRight.y = artboardRect.bottomRight.y;
                  }
              }
          }
          rect.rect = [rect.topLeft.x, rect.topLeft.y, rect.bottomRight.x, rect.bottomRight.y];
          return rect;
      }
      
      
      function calcZoom(viewRect, documentRect, margin) {
          if (documentRect.aspectRatio > viewRect.aspectRatio) {
              return parseFloat(((viewRect.actualWidth - (2 * margin)) / documentRect.width).toFixed(2));
          } else {
              return parseFloat(((viewRect.actualHeight - (2 * margin)) / documentRect.height).toFixed(2));
          }
      }
      
      
      function fitAll(document) {
          var view         = document.views[0];
      
      
          var viewRect     = calcViewRect(view.bounds, view.zoom);
          var documentRect = calcRect(getDocumentBounds(document.artboards).rect);
      
      
          view.centerPoint = documentRect.center;
          view.zoom        = calcZoom(viewRect, documentRect, 20);
      }