Article View: adobe.scripting.indesign
Article #21046Find/Change by list
From: Olav Kvern
Date: Mon, 13 May 2002 17:55
Date: Mon, 13 May 2002 17:55
7 lines
3925 bytes
3925 bytes
John Jordan, on the Blueworld InDesign Talk mailing list, asked about applying a given format to a series of specific strings. The following example does this by finding strings from a list and then applying a character style to the strings. Note that we can't effectively use wildcards ("^?", "^$") in this type of find/change operation, as we have to supply a "change to" string to get the operation to work, and we can't use the wildcard for that.
Private Sub Command1_Click() Rem Find/change by list Rem This script applies a given character style ("SmallCaps") to all capital letters Rem surrounded by parentheses ("(A)", "(B)", etc.) and roman numerals followed by a period Rem ("I.", "II.", etc.--up to "X."). Rem To use this script, select a text frame or text and run the script. Dim myInDesign As InDesign.Application Dim myStory As InDesign.Story Set myInDesign = CreateObject("InDesign.Application.2.0") Set mySelection = myInDesign.Selection Select Case TypeName(mySelection.Item(1)) Case "TextFrame" Set myStory = mySelection.Item(1).ParentStory Case "Text", "InsertionPoint" Set myStory = mySelection.Item(1).ParentTextFrame.ParentStory Case Else GoTo myDisplayError End Select Rem Clear the existing find/change preferences myInDesign.FindPreferences = idNothing myInDesign.ChangePreferences = idNothing Set myDocument = myStory.Parent Rem The following creates the character style if it does not already exist. Rem In this case, you can redefine the character style later to give it the Rem formatting you want. If IsObject(myDocument.CharStyles.Item("SmallCaps")) = False Then Set myCharStyle = myDocument.CharStyles.Add myCharStyle.Name = "SmallCaps" Else Rem The character style already exists, so get a reference to it. Set myCharStyle = myDocument.CharStyles.Item("SmallCaps") End If Rem Set the character style of the change preferences to the "SmallCaps" character style. myInDesign.ChangePreferences.CharacterStyle = myCharStyle Rem The following loops take care of formatting the characters. myStringArray = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z") For myCounter = 0 To UBound(myStringArray) myString = "(" & myStringArray(myCounter) & ")" Set myFoundItems = myStory.Search(myString, False, True, myString) Next myStringArray = Array("AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN", "OO", "PP", "QQ", "RR", "SS", "TT", "UU", "VV", "WW", "XX", "YY", "ZZ") For myCounter = 0 To UBound(myStringArray) myString = "(" & myStringArray(myCounter) & ")" Set myFoundItems = myStory.Search(myString, False, True, myString) Next Rem Set "WholeWord" to true for the roman numerals. myStringArray = Array("I.", "II.", "III.", "IV.", "V.", "VI.", "VII.", "VIII.", "IX.", "X.") For myCounter = 0 To UBound(myStringArray) Set myFoundItems = myStory.Search(myStringArray(myCounter), True, True, myStringArray(myCounter)) Next Rem Set the find/change preferences back to their default state. myInDesign.FindPreferences = idNothing myInDesign.ChangePreferences = idNothing Exit Sub myDisplayError: MsgBox "Please select some text or a text frame and try again." End Sub
Thanks,
Ole
Technical Details
Message-ID:
<ef87fb9.-1@WebX.aaQKaf7vad4>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!newsfeed01.sul.t-online.de!t-online.de!WebXShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f939a,436d612f4d917081,start X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2002-05-13 11:07:40 PST From: "Olav Kvern" <okvern@adobe.com> Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!newsfeed01.sul.t-online.de!t-online.de!WebX Newsgroups: adobe.scripting.indesign Subject: Find/Change by list Message-ID: <ef87fb9.-1@WebX.aaQKaf7vad4> Date: Mon, 13 May 2002 10:55:51 -0700 Lines: 81 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit