8 Replies Latest reply on Apr 23, 2008 7:54 AM by (Robert_Tkaczyk)

    Replace tags with images

      Hi <br />I'm tryng to build a vb script that replace text tag with images. <br />Tags are like <%img=c:\logo.jpg%>. I need to replace the tag with the image c:\logo.jpg. <br />I try this (It didn't work). Can somebody help me? <br />thanks <br /> <br />On Error Resume Next <br /> <br />Set myInDesign = CreateObject("InDesign.Application.CS3") <br />Set myDocument = myInDesign.ActiveDocument <br /> <br />Set myTextFrames = myDocument.TextFrames <br />'Get the total number of textframes <br />myTextFrameTotal = myTextFrames.Count <br /> <br />REM : replace tag <%img=c:\logo.jpg%> image with image c:\logo.jpg <br />'Setup a loop through all textframes <br />For myloop = 1 To myTextFrameTotal <br /> <br /> Set myTextFrame = myTextFrames.Item(myloop) <br /> <br /> myCharacterCount = myTextFrame.Characters.Count <br /> If myCharacterCount &gt; 0 Then <br /> <br /> id1= InStr(1, myTextFrame.Texts.Item(1).Contents, "<%img="  ,1)<br />        If  id1 >0  Then        <br />             id2= InStr(1,  myTextFrame.Texts.Item(1).Contents, "%>" ,1) <br /> If id1 &gt;0 Then <br /> <br /> REM: image filename <br /> myFile = trim(Mid(myTextFrame.Texts.Item(1).Contents, id1+6, id2-id1-6) ) <br /> REM: set tag to "" <br /> myTextFrame.Texts.ItemByRange(id1, id2).Item(1).Contents = "" <br /> <br /> REM myTextFrame.Fit idFitOptions.idProportionally <br /> REM myTextFrame.Fit idFitOptions.idContentToFrame <br /> REM: insert image file <br /> myTextFrame.InsertionPoints.Item(1).Place (myFile) <br /> <br /> end if <br /> end if <br /> end if <br /> <br />Next 'Process the next text frame in text frames <br /> <br />if Err.Number&gt;0 then MsgBox Err.Description + ", " &amp; Err.Source <br /> <br />REM myPub.SaveAs ("") <br />REM myPub.Close <br />REM myInDesign.Quit <br />REM Set myDocument = Nothing <br />REM Set myInDesign = Nothing
        • 1. Re: Replace tags with images
          Level 1
          first - move line with error checking up - before NEXT command

          and what error do you have ??

          robin

          --
          www.adobescripts.com
          • 2. Re: Replace tags with images
            Level 1
            Hi Robert <br />Thanks for your help. <br /> <br />I get this message error: <br />"Topic not optional. Vbscript error run time." <br /> <br />The problem seems to be with instruction: <br /> myTextFrame.Texts.ItemByRange(id1, id2).Item(1).Contents = "" <br />and <br /> myTextFrame.InsertionPoints.Item(1).Place (myFile) <br /> <br />Image is inserted but NON in the tag position and tag postion is not emptied. <br /> <br />I do not understand if "id1, id2" for ItemByRange function are simple integer or must be a kinf of object. <br /> <br />On Error Resume Next <br /> <br />Set myInDesign = CreateObject("InDesign.Application.CS3") <br />Set myDocument = myInDesign.ActiveDocument <br /> <br />Set myTextFrames = myDocument.TextFrames <br />'Get the total number of textframes <br />myTextFrameTotal = myTextFrames.Count <br /> <br />msgbox myTextFrameTotal <br /> <br />REM : replace tag <%img=c:\logo.jpg%> image with image c:\logo.jpg <br />'Setup a loop through all textframes <br />For myloop = 1 To myTextFrameTotal <br /> <br /> Set myTextFrame = myTextFrames.Item(myloop) <br /> <br /> myCharacterCount = myTextFrame.Characters.Count <br /> If myCharacterCount &gt; 0 Then <br /> <br /> id1= InStr(1, myTextFrame.Texts.Item(1).Contents, "<%img="  ,1)<br />    If  id1 >0  Then        <br />       id2= InStr(1,  myTextFrame.Texts.Item(1).Contents, "%>" ,1) <br /> If id2 &gt;0 Then <br /> <br /> REM: image filename <br /> myFile = trim(Mid(myTextFrame.Texts.Item(1).Contents, id1+6, id2-id1-6) ) <br /> <br /> msgbox myFile <br /> <br /> REM: set tag to "" <br /> myTextFrame.Texts.ItemByRange(id1, id2).Item(1).Contents = "" <br /> <br /> REM myTextFrame.Fit idFitOptions.idProportionally <br /> REM myTextFrame.Fit idFitOptions.idContentToFrame <br /> myTextFrame.InsertionPoints.Item(1).Place (myFile) <br /> <br /> end if <br /> end if <br /> end if <br /> <br /> if Err.Number&gt;0 then MsgBox Err.Description + ", " &amp; Err.Source <br /> <br />Next 'Process the next text frame in text frames <br /> <br />REM myPub.SaveAs ("") <br />REM myPub.Close <br />REM myInDesign.Quit <br />REM Set myDocument = Nothing <br />REM Set myInDesign = Nothing
            • 3. Re: Replace tags with images
              Level 1
              > myTextFrame.Texts.ItemByRange(id1, id2).Item(1).Contents = ""

              you can't use id1 and id2 as long type
              they need to be for example Character type, so you need to write this:
              myTextFrame.Texts.ItemByRange( _
              myTextFrame.Characters.item(id1), _
              myTextFrame.Characters.item(id2)). _
              Item(1).Contents = ""

              maybe long/int type works for JavaScript - but it doesn't work for VB

              do your TextFrames are threaded as Story or stand alone ?
              and what else is in TextFrames ?? only FileName or some other texts ?

              robin

              --
              www.adobescripts.com
              • 4. Re: Replace tags with images
                Level 1
                Hi Robert <br />Thanks again and sorry for my bad English. <br /> <br />I'm new to Indesign. I don't understand completely the question: <br />"TextFrames are threaded as Story or stand alone" <br /> <br />I have a document with some text frames and inside the frames there are several paragraphs. <br />Each paragraphs can contain special tags "<%img= c:\image.jpg%>" to be replaced with images. <br />As sample: <br /> <br />TABLE <%img= c:\table.jpg%> OF CONTENTS <br />Section Contents Page <br />1.1 Condensing unit specifications 2 <br />1.2 Indoor units <%img= c:\unit.jpg%> specifications 6 <br /> <br />Using <br />myTextFrame.Texts.ItemByRange( myTextFrame.Characters.item(id1) , myTextFrame.Characters.item(id2+1)).Item(1).Contents = "" <br />do the job replacing the tag, eg.: <br /> <br />TABLE <%img= c:\table.jpg%> OF CONTENTS <br />become <br />TABLE OF CONTENTS <br /> <br />But image "c:\table.jpg" is not placed where the tag begin. <br />The image is placed at the top left border of the textframe. <br /> <br />I think that the function <br />myTextFrame.InsertionPoints.Item(1).Place (myFile) <br />is not correctly valorized with the point where the tag begin. <br /> <br />PS: <br />You can give me some advice on a good book on programming Indesign in vbscript or jscript
                • 5. Re: Replace tags with images
                  Level 1
                  Hi Robin <br /> <br />with <br /> <br />Set myFoundTag = myTextFrame.Texts.ItemByRange( myTextFrame.Characters.item(id1) , myTextFrame.Characters.item(id2+1)).Item(1) <br />myFoundTag.Place myFile <br /> <br />I don't know if is the best way but it seems works. <br /> <br />Another question now is how to set the vertical alignment of the image? <br /> <br />The complete script that seems work and that can be useful for other people is: <br /> <br />REM: -------------------------------------------------------------- <br />REM : replace tags <%img=c:\image.jpg%> with image c:\logo.jpg <br />REM: -------------------------------------------------------------- <br /> <br />On Error Resume Next <br /> <br />Set myInDesign = CreateObject("InDesign.Application.CS3") <br />Set myDocument = myInDesign.ActiveDocument <br /> <br />Set myTextFrames = myDocument.TextFrames <br />myTextFrameTotal = myTextFrames.Count <br /> <br />'Loop through all text frames <br />For myloop = 1 To myTextFrameTotal <br /> <br /> Set myTextFrame = myTextFrames.Item(myloop) <br /> <br /> myCharacterCount = myTextFrame.Characters.Count <br /> If myCharacterCount &gt; 0 Then <br /> <br /> id1= InStr(1, myTextFrame.Texts.Item(1).Contents, "<%img="  ,1)<br />    If  id1 >0  then        <br />      id2= InStr(1,  myTextFrame.Texts.Item(1).Contents, "%>" ,1) <br /> If id2 &gt;0 then <br /> REM: get image filename <br /> myFile = trim(Mid(myTextFrame.Texts.Item(1).Contents, id1+6, id2-id1-6) ) <br /> <br /> REM: Use the ItemByRange to get a reference to the entire tag. <br /> Set myFoundTag = myTextFrame.Texts.ItemByRange( myTextFrame.Characters.item(id1) , myTextFrame.Characters.item(id2+1)).Item(1) <br /> <br /> REM: Replacethe tag with the specified image. <br /> myFoundTag.Place myFile <br /> end if <br /> end if <br /> end if <br /> <br /> if Err.Number&gt;0 then MsgBox Err.Description + ", " &amp; Err.Source <br /> <br />Next 'Process the next text frame in text frames <br /> <br />REM myPub.SaveAs ("filename") <br />REM myPub.Close <br />REM myInDesign.Quit <br />REM Set myDocument = Nothing <br />REM Set myInDesign = Nothing
                  • 6. Re: Replace tags with images
                    Level 1
                    > I'm new to Indesign. I don't understand completely the question:
                    > "TextFrames are threaded as Story or stand alone"

                    TextFrame is container for text
                    Story can have one or more linked TextFrames

                    > Set myFoundTag = myTextFrame.Texts.ItemByRange( _
                    > myTextFrame.Characters.item(id1) , _
                    > myTextFrame.Characters.item(id2+1)).Item(1)
                    > myFoundTag.Place myFile

                    this is the best way ;)

                    > Another question now is how to set the vertical alignment of the image?

                    you can assign ObjectStyle to placed file:

                    Set myPlacedFile = myFoundTag.Place myFile
                    Call myPlacedFile.ApplyObjectStyle(myObjStyle,True)

                    and change properties of ObjectStyle globally for all placed files

                    other way - you can change BaselineShift of parent of placed image - this parent is Character

                    if you will use ObjectStyle - TextWrap don't work for lines BEFORE line where image is anchored and for line where image is anchored

                    robin

                    --
                    www.adobescripts.com
                    • 7. Re: Replace tags with images
                      Level 1
                      Hi Robin
                      I try ObjectStyle but I get error:
                      "Property or Object method non supported"

                      REM: style1 is defined as ObjectStyles into the document
                      REM: Get a reference to "style1"
                      Set myObjStyle = myDocument.ObjectStyles.Item("style1")
                      If Err.Number <> 0 Then
                      Set myObjStyle = myDocument.ObjectStyles.Add
                      myObjStyle.Name = "style1"
                      Err.Clear
                      End If

                      inside the LOOP:

                      REM: Use the ItemByRange method to reference the entire tag.
                      Set myFoundTag = myTextFrame.Texts.ItemByRange( myTextFrame.Characters.item(id1) , myTextFrame.Characters.item(id2)).Item(1)

                      REM: Replace the tag with the specified image.
                      Set myPlacedFile = myFoundTag.Place(myFile)

                      REM: Apply myObjStyle to image
                      myPlacedFile.ApplyObjectStyle myObjStyle, True
                      If Err.Number <> 0 Then
                      msgbox "1: " + Err.Description & vbCR & "source = " & Err.Source
                      Err.Clear
                      end if
                      • 8. Re: Replace tags with images
                        Level 1
                        if in this line:

                        myPlacedFile.ApplyObjectStyle myObjStyle, True

                        you need to write this line like this:

                        call myPlacedFile.ApplyObjectStyle(myObjStyle, True)


                        but I think you have error becuse this line:

                        Set myPlacedFile = myFoundTag.Place(myFile)

                        is wrong, you need to write:

                        Set myPlacedFile = myFoundTag.Place(myFile).Item(1)

                        because in CS3 when you place item(s) - you always get collection - even if you place only one file - you get one element collection

                        robin
                        --
                        www.adobescripts.com