🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #17795

Re: Ignorant greenhorn seeks charity script

#17795
From: Olav Kvern
Date: Sun, 11 May 2003 09:44
70 lines
4397 bytes
ian--

I *think* you're looking to replace a text tag you've typed in text. If you are, try something like this:



Rem PlaceGraphicAtTag 
Rem An InDesign 2.x script. 
Rem Places a graphic at locations in text defined by begin "<<" and end ">>" tags. 
Rem Optional variable declarations. Omit these if you are creating a VBScript. 
Dim myInDesign As InDesign.Application 
Dim myPath As String 
Dim myDocument As InDesign.Document 
Dim myStory As InDesign.Story 
Dim myStartItems, myEndItems, myFoundTag As Object 
Dim myCounter, myFoundCounter, myStartIndex, myEndIndex As Long 
Rem Enter the path to the folder containing the images you want to place. 
Rem (Include the trailing backslash "\".) 
myPath = "c:\" 
Set myInDesign = CreateObject("InDesign.application.2.0") 
Rem Clear the find/change preferences. 
myInDesign.FindPreferences = idNothing 
myInDesign.ChangePreferences = idNothing 
Set myDocument = myInDesign.ActiveDocument 
Rem Work through the document story-by-story. 
For myCounter = 1 To myDocument.Stories.Count 
    Set myStory = myDocument.Stories.Item(1) 
    Rem Create an object containing references to all instances of 
    Rem the "start tag" string ("<<" in this example). 
    Set myStartItems = myStory.Search("<<") 
    If myStartItems.Count > 0 Then 
        Rem Create an object containing references to all instances of 
        Rem the "end tag" string (">>" in this example). 
        Set myEndItems = myStory.Search(">>") 
        If myStartItems.Count = myEndItems.Count Then 
            Rem Iterate through the found items backwards to avoid invalidating 
            Rem text object references preceding them in the story. 
            For myFoundItemCounter = myStartItems.Count To 1 Step -1 
                Set myStartItem = myStartItems.Item(myFoundItemCounter) 
                myStartIndex = myStartItem.Characters.Item(1).Index 
                Rem Get the corresponding "end" tag. 
                Set myEndItem = myEndItems.Item(myFoundItemCounter) 
                myEndIndex = myEndItem.Characters.Item(-1).Index 
                Rem Get a reference to the first character in the "start" tag. 
                Set myStartCharacter = myStory.Characters.Item(myStartIndex) 
                Rem Get a reference to the first character in the "end" tag. 
                Set myEndCharacter = myStory.Characters.Item(myEndIndex) 
                Rem Use the ItemByRange method to get a reference to the entire tag. 
                Set myFoundTag = myStory.Characters.ItemByRange(myStartCharacter, myEndCharacter).Item(1) 
                Rem Replace the text of the tag with the specified graphic. 
                myFoundTag.Texts.Item(1).Place myPath & Mid(myFoundTag.TextContents, 3, Len(myFoundTag.TextContents) - 4) 
            Next 
        Else 
            Rem We have a mismatched tag somewhere. 
            MsgBox "One of the stories contains a mismatched tag." 
            Exit For 
        End If 
    End If 
Next 




As you can see, you might need to edit a few sections in the script to provide your own file names, paths, etc. But it should do what you want it to do.

To add this to a Visual Basic form, copy the text out of the message and paste it into a button. To create a VBScript, copy the text from the line beginning with "myPath =" and paste it into WordPad, Notepad, or other text editor. Save the file as plain text with the file extension ".vbs" Store the file (or a shortcut to it) in the Scripts folder inside your InDesign folder. To run the script, double-click the script name in the Scripts palette.

Thanks,

Ole
Technical Details
Message-ID: <1de91143.2@WebX.la2eafNXanI>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!news-out.visi.com!petbe.visi.com!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX
References: <1de91143.-1@WebX.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f939a,5bf68636565030c0
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2003-05-11 03:07:55 PST
From: "Olav Kvern" <okvern@adobe.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!news-out.visi.com!petbe.visi.com!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX
Newsgroups: adobe.scripting.indesign
Subject: Re: Ignorant greenhorn seeks charity script
Message-ID: <1de91143.2@WebX.la2eafNXanI>
Date: Sun, 11 May 2003 02:44:19 -0700
References: <1de91143.-1@WebX.la2eafNXanI>
Lines: 87
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit