Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Inserting an image after findGrep

Community Expert ,
Feb 25, 2010 Feb 25, 2010

Hello All,

I have a some square-bracked text in my document. Inside each set of square brackets is the name of an EPS file that I want to insert inline at that location. So, for example:

This is a test for inline images.[Image1.eps] This is where the image will go. Insert [Image2.eps]

I am using this to locate the "tags":

var oDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = "\\[([\\w]+\\.eps)\\]";
app.changeGrepPreferences.changeTo = "";

var oText = oDoc.findGrep(true);

Now that I have an array of text objects in oText, I need to know how to get the location at the end of each one, so I can place the image after the "]" character. After I place the images, I will delete the bracketed text.

So, my basic question is, given an item in oText, how do I get the location at the end of it? Thank you very much.

Rick Quatro

TOPICS
Scripting
670
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 25, 2010 Feb 25, 2010

Have you tried Kasyan nice script?? If not please try

http://www.kasyan.ho.com.ua/place_images.html

Thanks

Mac

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 25, 2010 Feb 25, 2010
LATEST

Below script comes with indesign_cs4_scripting_guide_scripts. It will also help you.

//ReplaceTextWithGraphic.jsx

//An InDesign CS4 JavaScript

//

//Replaces each instance of a string with a specified graphic file.

main();

function main(){

          mySetup();

          mySnippet();

          myTeardown();

}

function mySetup(){

     //Create an example document.

     var myDocument = app.documents.add();

     myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

     myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

     //Create a text frame on page 1.

     var myTextFrame = myDocument.pages.item(0).textFrames.add();

     //Set the bounds of the text frame.

     myTextFrame.geometricBounds = myGetBounds(myDocument, myDocument.pages.item(0));

     //Fill the text frame with placeholder text.

     var myString = "This is an example paragraph.\r<graphic: \/c\/test.tif>\rThis is a paragraph following an inline graphic.\r<graphic: \/c\/test.tif>\rThis is another paragraph.";

     myTextFrame.contents = myString;

}

function mySnippet(){

     //<fragment>

     var myInlineGraphic, myX1, myY1, myX2, myY2, myFoundItem;

     var myRegExp = "(?i)<graphic\\:\\s?(.*)\\s?>"

     var myDocument = app.documents.item(0);

     //Clear the grep find/change preferences.

     app.findGrepPreferences = NothingEnum.nothing;

     app.changeGrepPreferences = NothingEnum.nothing;

     app.findGrepPreferences.findWhat = myRegExp;

     //Search the document.

     var myFoundItems = myDocument.findGrep(true);

     //Clear the find/change preferences after the search.

     app.findGrepPreferences = NothingEnum.nothing;

     app.changeGrepPreferences = NothingEnum.nothing;

     if(myFoundItems.length != 0){

          for(var myCounter = 0; myCounter < myFoundItems.length; myCounter ++){

               //A potential problem comes up here: what if the graphic is too big

               //to fit on the page? What if the text is overset? There are no

               //foolproof solutions that can handle all of the possible problems

               //that can arise; your best bet is to adjust your scripts to deal

               //with the specific layouts of specific documents rather than to try to

               //create a single function that can handle all situations for all

               //possible documents.

               //

               //In this case, we simply make the frame a set size and place the graphic

               //into the frame, then fit the graphic to the frame proportionally, and

               //then fit the frame to the resized graphic.

               myFoundItem = myFoundItems[myCounter];

               myX1 = myFoundItem.characters.item(0).horizontalOffset;

               myY1 = myFoundItem.characters.item(0).baseline;

               myX2 = myX1 + 72;

               myY2 = myY1 + 72;

               myFileName = myFoundItem.contents.replace(/<graphic\:\s(.*)\s?>/i, "$1");

               myFrame = myFoundItem.insertionPoints.item(0).rectangles.add();

               //Recompose the text after adding the inline frame.

               myFoundItem.parent.recompose();

               myFrame.geometricBounds = [myY1, myX1, myY2, myX2];

               try{

                    myInlineGraphic = myFrame.place(File(myFileName))[0];

                    myFrame.fit(FitOptions.proportionally);

                    myFrame.fit(FitOptions.frameToContent);

               } catch(myError){}

          }

     }

     //Now replace the tags.

     app.findGrepPreferences.findWhat = myRegExp;

     app.changeGrepPreferences.changeTo = "";

     myDocument.changeGrep();

     //Clear the find/change preferences after the search.

     app.findGrepPreferences = NothingEnum.nothing;

     app.changeGrepPreferences = NothingEnum.nothing;

     //</fragment>

}

function myTeardown(){

}

function myGetBounds(myDocument, myPage){

     var myPageWidth = myDocument.documentPreferences.pageWidth;

     var myPageHeight = myDocument.documentPreferences.pageHeight

     if(myPage.side == PageSideOptions.leftHand){

          var myX2 = myPage.marginPreferences.left;

          var myX1 = myPage.marginPreferences.right;

     }

     else{

          var myX1 = myPage.marginPreferences.left;

          var myX2 = myPage.marginPreferences.right;

     }

     var myY1 = myPage.marginPreferences.top;

     var myX2 = myPageWidth - myX2;

     var myY2 = myPageHeight - myPage.marginPreferences.bottom;

     return [myY1, myX1, myY2, myX2];

}

Shonky

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines