1 Reply Latest reply on Jan 11, 2016 6:23 PM by dsackett

    [CS 5.5] [JS] copying a graphic page item between documents

    dsackett Level 1

      Hi, I'm still learning scripting, and am wrestling with what's probably a simple problem for someone more knowledgable. The task is to copy text & graphics from a couple pages of an one document to another based on a similar template. The text frame and graphics frames are named. I was able to get the text part, but I'm stuck on how to address & paste into a graphics frame.

       

      At this point, I got the graphics to copy, but it doesn't quite work because the duplicate method only allows for a target of an array of 2 Units, Spread, Page or Layer, and the source document positions are slightly different, and copying the graphic only leaves out the frame's necessary styling. I really would like to paste into the page item, but the method for duplicating text differs. Any tips on approaching this? Should I instead select the graphic & paste it into the new document? How would I accomplish the equivalent of Paste Into?

       

      An excerpt of my code so far:

       

      var docPages = docRef.pages.everyItem().getElements();
      var cemwpsP1 = 3;
      var newWPS = createWPS(templatePath, wpsTemplateName, wpsFileName, wpsSaveFolder);
      var graphsRE = new RegExp (/graph/i);
      var textsRE = new RegExp (/body|chart/i);
      
      var srcPage = docPages[cemwpsP1];
      var wpsPage = newWPS.pages[0];
      for (i = srcPage.pageItems.length -1; i >=0; i--) {
       var pi = srcPage.pageItems[i];
       if ( pi.name.match(textsRE) ) {
       // copy the text element
       pi.texts[0].duplicate(LocationOptions.AT_BEGINNING, wpsPage.pageItems.itemByName(pi.name)); 
       }
       if ( pi.name.match(graphsRE) ) {
       // it's a graphic element
       // still stumped here! only copies to to the same coordinates on the new page...
       pi.allGraphics.duplicate(wpsPage);
       }
      }
      
      

       

       

        • 1. Re: [CS 5.5] [JS] copying a graphic page item between documents
          dsackett Level 1

          Ok, if anyone's interested, I solved it by copying graphic dimensions from the template's empty graphic frames, then nuking them and replacing with a duplicate graphic. This technique works because all page items are named similarly on the source and target pages.

           

          // step through page items on each page, checking item name matches as set in graphsRE & textsRE above & moving over to the new pages
          var srcPage = docPages[cemwpsP1];
          var wpsPage = newWPS.pages[0];
          for (i = srcPage.pageItems.length -1; i >=0; i--) {
              var pi = srcPage.pageItems[i];
              if ( pi.name.match(textsRE) ) {
                  // it's a textual element, either ta.wpsFrameName or ta.wpsChartFrameName
                  pi.texts[0].duplicate(LocationOptions.AT_BEGINNING, wpsPage.pageItems.itemByName(pi.name));    
              }
              if ( pi.name.match(graphsRE) ) {
                  // it's a graphic element, and should only contain one graphic
                  // since ID doesn't allow copying into frames (or I never found how)
                  // get its matching info from the wps template
                  wpsBounds = wpsPage.pageItems.itemByName(pi.name).geometricBounds;
                  wpsObjStyle = wpsPage.pageItems.itemByName(pi.name).appliedObjectStyle;
                  // kill the original & duplicate the graphic to wps
                  wpsPage.pageItems.itemByName(pi.name).remove(); 
                  pi.allGraphics[0].duplicate(wpsPage); 
                  // get a reference to the newly placed graphic; it'll be the only nameless one, as they are all named on the template
                  for (g = wpsPage.pageItems.length -1; g >=0; g--) {
                      if ( wpsPage.pageItems[g].name == "" ) {
                          gfc = wpsPage.pageItems[g];
                      }
                  }
                  // name, size and style
                  gfc.name = pi.name;
                  gfc.geometricBounds = wpsBounds;
                  gfc.appliedObjectStyle = wpsObjStyle;            
              }