11 Replies Latest reply on Sep 13, 2016 6:11 AM by Loic.Aigon

    Bridgetalk Execute Command

    daneJ Level 1

      Hello again,

      I have written up this here little code which uses bridgetalk to loop through each page of an InDesign document copying it and then sending it illustrator where it is pasted to a new artboard. The problem is that the commands sent to illustrator are not done at the end of each looping through, but rather at the very end of the script once all instances in the loop have been gone through. Since all paste commands are executed at the end instead of after each loop the paste command uses what ever was last copied to the clipboard so the very last page of the InDesign document is pasted on all pages in the Illustrator document.

      I am wondering if there is some sort of execute command that i can add to my loop that will induce illustrator to execute each loop after it happens rather than all loops together at the end?

      Here is my code.

      Any Thoughts?

       

      Thanks and Best Regards,

      Dane

       

      var inDoc = app.activeDocument;

      starter()

      for ( var i = 0, ii = inDoc.pages.length; i < ii; i++ ){

          var myPage = inDoc.pages.item(i);

          thisThing = 0

          var thisThing = myPage.pageItems.everyItem();

          app.select(thisThing);

          app.copy(thisThing);

          Paste(thisThing);

      };

       

      function starter() {

          var bt = new BridgeTalk

          bt.target = "illustrator";

          bt.body =  "var illDoc = app.activeDocument; illDoc.artboards.add ([-7650,0,-6808,-595]);";

          bt.send();

      };

       

      function Paste(thisThing) {

          var bt = new BridgeTalk;

          bt.target = "illustrator";

          bt.body =  'var illDoc = app.activeDocument; var abIdx = illDoc.artboards.getActiveArtboardIndex (); var abActive = illDoc.artboards[abIdx]; var newBoardTop = abActive.artboardRect[2]  + 25; var newBoardWidth = abActive.artboardRect[2] + 842 + 25;'

                          + 'illDoc.artboards.add ([newBoardTop,0,newBoardWidth,-595]); app.paste(); app.executeMenuCommand( "group" ); var curGroup = illDoc.layers[0].groupItems[0]; var posRefTop = newBoardTop + 56.69; var posRefLeft = -51;'

                          + 'curGroup.position = [posRefTop, posRefLeft];';

          bt.send();    

      };

        • 1. Re: Bridgetalk Execute Command
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Why not passing through a PDF export and AI reimport ?

           

          var main = function() {
            var doc = app.properties.activeDocument, shortName,
            files, file, pages, page, arr = [];
          
            if (!doc ) return;
          
            shortName = doc.name.replace ( /\.indd$/i,"" );
            fo = Folder( Folder.temp+"/"+shortName );
            !fo.exists && fo.create();
            files = fo.getFiles ( function ( f ) { return /\.pdf/i.test (f.name) } );
          
          
            while ( file = files.pop() ) {
            file.remove();
            }
          
            pages = doc.pages.everyItem().getElements().reverse();
          
            while ( page = pages.pop() ) {
            file = File ( fo+"/"+shortName+"_"+page.name+".pdf" );
            doc.exportFile ( ExportFormat.PDF_TYPE, file);
            arr [ pages.length ] = file.fsName;
            }
          
          
            placeFiles ( arr );
          }
          
          
          function placeFiles (arrayOfFiles ) {
            var aiScript = "("+placeFilesInAI.toSource().replace(/(^\(|\)$)/g, '' )+")(\""+arrayOfFiles.join("##")+"\")";
            var bt = new BridgeTalk;
            bt.target = "illustrator";
            bt.body = aiScript;
          
            bt.send();
          }
          
          
          function placeFilesInAI ( files ) {
            var doc = app.documents.length? app.activeDocument : app.documents.add(),
            url, placed, ab, abs,
            files =files.split("##");
            var rect = [-7650,0,-6808,-595], gap = 942;
            while ( url = files.pop() ) {
          
            abs = doc.artboards;
            rect[0]+=gap;
            rect[2]+=gap;
          
            ab = abs.add(rect);
            abs.setActiveArtboardIndex ( doc.artboards.length-1 );
            placed = doc.placedItems.add(); placed.file = File ( url );
            placed.left = rect[0];
            placed.top = rect[1];
            placed.embed();
            }
          }
          
          
          
          
          main();
          

           

           

          FWIW

           

          Loic

          Ozalto | Productivity Oriented - Loïc Aigon

          • 2. Re: Bridgetalk Execute Command
            Laubender Adobe Community Professional & MVP

            Hi Loic,

            just tested your script with InDesign CS6 v8.1.0 and Adobe Illustrator CS6 v16.2.1 on Mac OSX 10.10.3.

             

            Your PDF export function is exporting all pages in the document for every single iteration step in the pages loop.
            I think, you have to use app.pdfExportPreferences.pageRange to limit this to one page per PDF.

             

            Just tested this and it's working as expected. However, Illustrator CS6 seems to be a bit unresponsive after executing the script.
            Switching between some open apps windows will resolve this.

             

            Regards,
            Uwe

            1 person found this helpful
            • 3. Re: Bridgetalk Execute Command
              Loic.Aigon Adobe Community Professional

              I think, you have to use app.pdfExportPreferences.pageRange

              Indeed. An omission of mine

              • 4. Re: Bridgetalk Execute Command
                Laubender Adobe Community Professional & MVP

                And Loic, thank you for the eye-opener how to work with BridgeTalk.

                The "("+function.toSource() method is very cool.


                And also the idea with the doc.placedItems.add() method with Illustrator.

                 

                Of course one could debate copy/paste vs. PDF-Export to Illustrator where Illustrator is showing all objects as individual items on the artboards, because with the pdf method all desired objects are nested inside the pdf's page frame one cannot avoid.

                But at least it is working. And the nested items can be extracted afterwards.

                 

                The other try with sending the BridgeTalk inside a loop did not work. Don't know why.

                Maybe something how the clipboard is handled between two Adobe apps?

                 

                Best,
                Uwe

                • 5. Re: Bridgetalk Execute Command
                  Loic.Aigon Adobe Community Professional

                  And Loic, thank you for the eye-opener how to work with BridgeTalk.

                  The "("+function.toSource() method is very cool.

                  Thanks. However it seems it can be even simplier:

                  var myFunc = function() {
                  
                    alert( app.documents.length );
                  
                  };
                  
                  
                  eval("("+myFunc+')()')
                  

                   

                  And also the idea with the doc.placedItems.add() method with Illustrator.

                  One of the weirdest AI scripting objects. You don't place files but create placedItem instance then link it to a file

                   

                  Of course one could debate copy/paste vs. PDF-Export to Illustrator

                  IMHO, there will be loss at some point so one way or the other is very much the same to me…The only true argument here might be that copy/pasting may slow the process dramatically as it would overload the memory of both apps (maybe what corrupt the loop after all).

                   

                  sending the BridgeTalk inside a loop did not work. Don't know why.

                  I think BridgeTalk is a great concept yet delicate. More than PDF vs Copy/Pasting, my idea was to minimize the use of the BridgeTalk object.

                   

                  Loic

                  • 6. Re: Bridgetalk Execute Command
                    Laubender Adobe Community Professional & MVP

                    Hi Loic,

                    the "loss" you are talking about will be bigger when using the copy/paste method, if some page items contain no fill color and stroke weight is zero. At least with the combination of InDesign CS6 and Illustrator CS6. Items like that cannot be pasted to Illustrator. Even when grouped or nested (pasted inside) other graphic frames in InDesign.

                     

                    Uwe

                    • 7. Re: Bridgetalk Execute Command
                      daneJ Level 1

                      Hi Loic

                      I'll be frank. Converting to PDF did not occur to me because your coding is way out of my league.

                      I have been dissecting your code for the last 2 hours and i have learned a huge amount. Perhaps your use of array is normal, I wouldnt know, but it seemed really skillful. and as Uwe pointed out "("+function.toSource() was very clever.

                      Anyway thanks a great deal for your help.

                       

                      Best Regards,

                      Dane

                      • 8. Re: Bridgetalk Execute Command
                        Loic.Aigon Adobe Community Professional

                        Hi daneJ

                         

                        Don't let the script length fool you. It's basically a two step script. The first part intends to output PDFs from pages (given we set pageRange as Uwe pointed out) onto a folder. The second part is the Illustrator script that will place those generated PDF file into artboards.

                        The only bone is to pass the files paths array to the the AI script but it's not far away from what you did. It just tries to be as straight as possible.

                        Loic

                        • 9. Re: Bridgetalk Execute Command
                          Loic.Aigon Adobe Community Professional

                          Laubender

                           

                          the "loss" you are talking about will be bigger when using the copy/paste method

                          I presume so but the PDF option will have some side effects like possible conversions. It would be maybe useful to use a pdf preset that would favor not using a PDF norm nor a PDF version prior to the latest.

                           

                          FWIW

                           

                          Loic

                          • 10. Re: Bridgetalk Execute Command
                            Laubender Adobe Community Professional & MVP

                            Hm. Maybe PDF/X-4 could be the right preset for this.

                             

                            With the copy/paste method we also have to check the Illustrator file for its used color management policy. Otherwise a message could inform you (and halt the script's execution), that the used color space or the current rendering intent is not equal to the source of the copied stuff. And we all know, that Illustrator can only have one color space per document where InDesign could export a mix of RGB and CMYK. I wish Adobe would come to grips with that and would support all color spaces InDesign is supporting with an Illustrator file as well.

                             

                            Regards,

                            Uwe

                            • 11. Re: Bridgetalk Execute Command
                              Loic.Aigon Adobe Community Professional
                              (and halt the script's execution),

                              Unless thos warnings would override the settings, you can disable alerts just as with InDesign:

                               

                              app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;