13 Replies Latest reply on Nov 7, 2012 8:47 AM by MF99

    Import Object Library Items through scripting, is it possible?

    MF99

      Hi All

       

      I produce various tickets using the data merge function in Indesign CS5. It works really well for me. On each ticket there is some variable data which also need importing, as it stands I set these up in a Library File, then save each item as an EPS and then put the relevant data to import this EPS in the cell on the spreadsheet. This works for me, but I would prefer not to have the hassle of having to save each Library Object as an EPS. Is there a simple was when doing the Data Merge for a script to also action the input of the relevant Library Object. I can easily assign a number on the spreadsheet that corrulates to the specific library object I require on each merge. So if the merge required Library Item '3' I would assign a cell in the spreadsheet with the number '3' and so on.

      Many thanks, Mike

        • 1. Re: Import Object Library Items through scripting, is it possible?
          MatthewMariani Level 1

          What is the nature of the assets in the library? Is each one a single group of page items?

          • 2. Re: Import Object Library Items through scripting, is it possible?
            MF99 Level 1

            Hi Matthew

             

            Yes they are, basically it is different offers for tickets, so say 500 tickets in total with a combination of 20 offers. Each ticket will take one offer. The offers are to large to incorporate into each spreadsheets and data merge. So I assign each offer a unique number and then this is also what the Object is called. If the spreadsheet asks for Offer '3' I then place from the Object Library offer '3'. I have since found a way where I can save each Offer as an EPS file and then I can get each offer to data merge, but would prefer if there was a way to automate the import direct from the library as the Data Merge happens, thus saving me the hassle of saving each one as an EPS.

             

            Cheers

             

            Mike

            • 3. Re: Import Object Library Items through scripting, is it possible?
              MatthewMariani Level 1

              Hi Mike,

               

              Sounds like a two-step procedure would work. Set up merge so unique identifer appears in its own frame with a unique paragraph or character style applied which is used for this type of text only. Run merge. Then run script that finds all the identifiers and replaces them with the appropriate assets from the library.  I think that would work.

              • 4. Re: Import Object Library Items through scripting, is it possible?
                MF99 Level 1

                Hi Matthew

                 

                That makes perfect sense, there is just one drawback, I should have mentioned that I have never used scripting before, I understand the logic of it, but I would not know where to begin in writing a script for this procedure...

                 

                Mike (Embarassed)

                • 5. Re: Import Object Library Items through scripting, is it possible?
                  MatthewMariani Level 1

                  See if this works...

                   

                   

                  #target "InDesign" 
                  //Assumes each library asset is a single group and library assets are named with unique integer values of one to three digits
                  //each text offer identifier is in its own textframe, which is large enough to hold the library assets.
                  //Warning: all text styled with the paragraph style "offer_id" will be deleted by the script
                  
                  if ( app.documents.count() < 1 ) { alert( "No InDesign document is open." ); exit(); }
                  if ( app.libraries.count() != 1 ) { alert( "You must have one InDesign library file open." ); exit(); }
                  var lib, sourceFile, found, i, ipt, offer;
                  lib = app.libraries[0];
                  clearFCPs();
                  app.findGrepPreferences.findWhat = "\\d{1,3}";
                  app.findGrepPreferences.appliedParagraphStyle = "offer_id"; //offer_id is name of paragraph style applied to each unique identifier
                  found = app.documents[0].findGrep(true);
                  
                  for ( i = 0; i < found.length; i++ ) {
                      ipt = found[i].insertionPoints[0];
                      offer = lib.assets.itemByName( found[i].contents.toString() );
                      try { offer.placeAsset( ipt ); }
                      catch (e) { alert( "Asset named " + found[i].contents.toString() + " not found!" ); }
                  }
                  
                  app.findGrepPreferences.findWhat = "[^~a]";
                  app.changeGrepPreferences.changeTo = "";
                  app.documents[0].changeGrep();
                  clearFCPs();
                  
                  function clearFCPs() {
                            app.findChangeTextOptions = app.findChangeGrepOptions = NothingEnum.NOTHING;
                            app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
                            app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                  }
                  
                  
                  1 person found this helpful
                  • 6. Re: Import Object Library Items through scripting, is it possible?
                    MF99 Level 1

                    Hi Matthew

                     

                    Sorry for the delayed response, been tied up the last couple of days. Will try this and feedback, many thanks for your help!!!

                     

                    Mike

                    • 7. Re: Import Object Library Items through scripting, is it possible?
                      MF99 Level 1

                      Hi Matthew

                       

                      I copied and pasted this into Apple Script Editor, i presume that is right, at first it threw an error for your firls line // I google this and changed to -- and it went past but now it errors on the first line of code.

                       

                      When trying to save it stops between app.document on the full stop. I changed to commas , and it went past but now it stops on the first {

                       

                      I presume I should not being using Apple Script Editior to get this script into indesign.

                       

                      Sorry told you I had not used scripting before... Anymore help would be greatly appricaited but if it is going to big hassle for you don't worry. I can live with saving as separate graphics and importing through the data merge that way.

                       

                      Cheers

                       

                      Mike

                      • 8. Re: Import Object Library Items through scripting, is it possible?
                        Larry G. Schneider Adobe Community Professional & MVP

                        The above is ExtendScript (Adobe version of Javascript). Copy the above and paste into either a new window in the ESTK utility or a text editing program like Note pad or Text Edit. Save it as a plain text file with a .jsx extension. Put it in Applications/Adobe InDesign CS5//Scripts/Scripts Panel. Run it by selecting from Window/Utilities/Scripts.

                        • 9. Re: Import Object Library Items through scripting, is it possible?
                          MF99 Level 1

                          Hi Guys

                           

                          Thanks for the Tip Larry, this worked. I now have the script in place, I have set up a paragraph style offer_id and have put an interger value in it which matches one of the library assets. I run the script, no errors come up, the text with box dissappears but no library asset is placed. The library assets normally consist of of two picture frames with linked graphics and one text box, all of which are grouped together, I am not sure if this is the problem as the script is trying to pull text in?

                           

                          Cheers

                           

                          Mike

                          1 person found this helpful
                          • 10. Re: Import Object Library Items through scripting, is it possible?
                            Jump_Over Level 5

                            Hi,

                             

                            Script place assets as a anchored object ==> ipt, insertionPoint, is a target place.

                            Aren't your frames overset after script run?

                            I suggest to define objectStyle for these event to apply custom options for anchored object.

                            It will make it independent on frame size.

                             

                            rgds

                            1 person found this helpful
                            • 11. Re: Import Object Library Items through scripting, is it possible?
                              MatthewMariani Level 1

                              I think Jump_Over is correct that the frames are overset. This would happen if your library assets are too large to fit in the placeholder textbox containing the identifier for the library item in your layout. An assumption of the original script was:

                              //each text offer identifier is in its own textframe, which is large enough to hold the library assets.


                              I modified the script to resize the placed library asset to avoid the oversetting problem. The library asset should be sized to the width of the placeholder textframe while keeping the height proportional. This seems to work on InDesign 5.5. There is probably a more elegant way of doing this, if anyone else feels like chiming in.  Give this a try...

                               

                               

                              #target "InDesign" 
                              //Assumes each library asset is a single group and library assets are named with unique integer values of one to three digits
                              //each text offer identifier is in its own textframe.
                              //Warning: all text styled with the paragraph style "offer_id" will be deleted by the script
                              if ( app.documents.count() < 1 ) { alert( "No InDesign document is open." ); exit(); }
                              if ( app.libraries.count() != 1 ) { alert( "You must have one InDesign library file open." ); exit(); }
                              var lib, sourceFile, found, i, ipt, offer, zAsset, ptf, bndsTF, bndsAsset, scaleFactor, sprd;
                              var vPrefs = app.documents[0].viewPreferences.verticalMeasurementUnits;
                              var hPrefs = app.documents[0].viewPreferences.horizontalMeasurementUnits;
                              app.documents[0].viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;
                              app.documents[0].viewPreferences.horizontalMeasurementUnits= MeasurementUnits.POINTS;
                              lib = app.libraries[0];
                              clearFCPs();
                              app.findGrepPreferences.findWhat = "\\d{1,3}";
                              app.findGrepPreferences.appliedParagraphStyle = "offer_id"; //offer_id is name of paragraph style applied to each unique identifier
                              found = app.documents[0].findGrep(true);
                              for ( i = 0; i < found.length; i++ ) {
                                  ipt = found[i].insertionPoints[0];
                                  ptf = ipt.parentTextFrames[0];
                                  sprd = ptf.parentPage.parent;
                                  bndsTF = ptf.geometricBounds;
                                  offer = lib.assets.itemByName( found[i].contents.toString() );
                                  if ( !offer.isValid ) { alert( "Asset named " + found[i].contents.toString() + " not found!" );  continue; }
                                  app.documents[0].layoutWindows[0].activeSpread = sprd;
                                  zAsset = offer.placeAsset( app.documents[0] )[0];
                                  bndsAsset = zAsset.geometricBounds;
                                  scaleFactor = (bndsTF[3] - bndsTF[1]) / (bndsAsset[3] - bndsAsset[1]);
                                  zAsset.transform( CoordinateSpaces.PASTEBOARD_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, app.transformationMatrices.add( {horizontalScaleFactor: scaleFactor, verticalScaleFactor: scaleFactor, horizontalTranslation: bndsTF[1] - bndsAsset[1], verticalTranslation: bndsTF[0] - bndsAsset[0]} ) );
                               }
                              app.documents[0].viewPreferences.verticalMeasurementUnits = vPrefs;
                              app.documents[0].viewPreferences.horizontalMeasurementUnits= hPrefs;
                              app.findGrepPreferences.findWhat = "[^~a]";
                              app.changeGrepPreferences.changeTo = "";
                              app.documents[0].changeGrep();
                              clearFCPs();
                              
                              
                              function clearFCPs() {
                                        app.findChangeTextOptions = app.findChangeGrepOptions = NothingEnum.NOTHING;
                                        app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
                                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                              }
                              
                              
                              1 person found this helpful
                              • 12. Re: Import Object Library Items through scripting, is it possible?
                                MF99 Level 1

                                Morning Matthew and Jump_Over.

                                 

                                Success, this works a dream. I also discovered why I was having problems with the first script now by running this second one. Bascially there were three items in the library asset I was using to test, these were not grouped together though. Once I grouped them and replaced them in the libraray the first script also works perfectly.

                                 

                                Many thanks for all your help, this has really helped me :-)

                                 

                                Mike

                                • 13. Re: Import Object Library Items through scripting, is it possible?
                                  MF99 Level 1

                                  Hi Matthew

                                   

                                  The above works brilliantly, saving me loads of time and hassle :-)

                                   

                                  At the risk of pushing my luck as you helped me loads so far, how hard would it be for you to adapt the first script to pull in a named graphic from a location and not the library. There are times when I do these tickets that the offer information is not always fully signed off till later. I can already get the data merge to pull in graphics when running the data merge by applying a graphic field in the spreadsheet and then putting the item location and name for example: Macintosh HD:Users:freelance:Desktop:Test Pics:Graphic1.eps From seeing how this script works I can see the potential for a revised one to look at another named paragraph style 'offer_graphic' and use the text in this to automatically pull in the .eps file at a later stage if required as the long as the path and filename are already located in this text box as per the example.

                                   

                                  I completely understand if you can't do this, you have been more than helpful so far and sure you have other things you would much prefer to be doing. I also need to look at learning some basic scripting as the funtionality looks very interesting!!!

                                   

                                  All the best

                                   

                                  Mike