Article View: adobe.scripting.indesign
Article #19505Re: Replace quoted text to italic without quotation marks
From: Olav Kvern
Date: Thu, 12 Dec 2002 20:01
Date: Thu, 12 Dec 2002 20:01
105 lines
5320 bytes
5320 bytes
Robert--
Here's an example:
Private Sub Command1_Click()
Rem Format text between quotation marks (and delete the quotation marks)
Dim myInDesign As InDesign.Application
Dim myDocument As InDesign.Document
Set myInDesign = CreateObject("InDesign.Application.2.0")
Rem ----------
Rem Create an example document.
Set myDocument = myInDesign.Documents.Add
Set myCharStyle = myDocument.CharStyles.Add
myCharStyle.Name = "Italic"
myCharStyle.FontStyle = "Italic"
Set myTextFrame = myDocument.Pages.Item(1).TextFrames.Add
myTextFrame.GeometricBounds = myGetBounds(myDocument.Pages.Item(1), myDocument)
myTextFrame.TextContents = "This is paragraph 1. It contains ""quoted"" text." & vbCr & "This is another paragraph containing ""quotation marks""."
Set myStory = myTextFrame.ParentStory
Rem Specify a string for the start of the quotation (in this example, an open double quote)
myStartString = "^{"
Rem Specify a string for the end of the quotation (in this example, an closing double quote)
myEndString = "^}"
myReplaceQuotedText myStory, myStartString, myEndString, myCharStyle
End Sub
Rem Function for finding a story by its unique ID.
Private Function myFindStory(myStoryID, myDocument)
For myCounter = 1 To myDocument.Stories.Count
If myDocument.Stories.Item(myCounter).ID = myStoryID Then
Set myFindStory = myDocument.Stories.Item(myCounter)
Exit Function
End If
Next
End Function
Rem Function for adding the end index of a range to a TextItem in the TextItems collection
Private Sub mySetEndIndex(myStoryID, myEndIndex, myTextItems)
Set myPriorTextItem = myTextItems.Item(1)
Set myNextTextItem = myTextItems.Item(1)
For Each myTextItem In myTextItems
If myTextItem.StoryID = myStoryID Then
If myTextItem.StartIndex < myEndIndex And myTextItem.StartIndex > myPriorTextItem.StartIndex Then
Set myPriorTextItem = myTextItem
End If
End If
Next
myPriorTextItem.EndIndex = myEndIndex
End Sub
Rem Subroutine that formats text between myStartString and myEndString.
Private Sub myReplaceQuotedText(myStory, myStartString, myEndString, myCharStyle)
Dim myTextItems As New TextItems
Set myDocument = myStory.Parent
Set myInDesign = myDocument.Parent
Rem Clear the find/change preferences
myInDesign.FindPreferences = idNothing
myInDesign.ChangePreferences = idNothing
myInDesign.FindPreferences.FindText = myStartString
For myCounter = 1 To myDocument.Stories.Count
Set myFoundItems = myDocument.Stories.Item(myCounter).Search
For myFoundItemCounter = 1 To myFoundItems.Count
Set myFoundItem = myFoundItems.Item(myFoundItemCounter)
myStoryID = myFoundItem.Parent.ID
myStartIndex = myFoundItem.Characters.Item(1).Index
myEndIndex = myFoundItem.Characters.Item(-1).Index
myTextItems.Add myStoryID, myStartIndex, myEndIndex
Next
Next
myInDesign.FindPreferences.FindText = myEndString
For myCounter = 1 To myDocument.Stories.Count
Set myFoundItems = myDocument.Stories.Item(myCounter).Search
For myFoundItemCounter = 1 To myFoundItems.Count
Set myFoundItem = myFoundItems.Item(myFoundItemCounter)
myEndIndex = myFoundItem.Characters.Item(-1).Index
myStoryID = myFoundItem.Parent.ID
mySetEndIndex myStoryID, myEndIndex, myTextItems
Next
Next
Rem Next, walk through the collection (backwards) and format the text
For myCounter = myTextItems.Count To 1 Step -1
Set myTextItem = myTextItems.Item(myCounter)
Set myStory = myFindStory(myTextItem.StoryID, myDocument)
Set myStartCharacter = myStory.Characters.Item(myTextItem.StartIndex)
Set myEndCharacter = myStory.Characters.Item(myTextItem.EndIndex)
Set myRange = myStory.Characters.ItemByRange(myStartCharacter, myEndCharacter).Item(1)
myRange.CharacterStyle = myCharStyle
myEndCharacter.Delete
myStartCharacter.Delete
Next
End Sub
Next, using the Class Wizard, create a new object. Call it "TextItem" and give it three properties: StartIndex, EndIndex, and StoryID. All three properties are of the default value type Variant.
Next, again using the Class Wizard, create a new collection object. Call it "TextItems" and make it contain the TextItem object type.
Save both .cls files.
I can post the code for the class files, but I'd rather not. It's long, and it's entirely generated by the Class Wizard.
Thanks,
Ole
Technical Details
Message-ID:
<1de6f307.5@WebX.la2eafNXanI>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!news.osn.de!newsfeed.freenet.de!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebXReferences:
<1de6f307.-1@WebX.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f939a,8d45903beb021fdc X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2002-12-12 12:08:05 PST From: "Olav Kvern" <okvern@adobe.com> Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!news.osn.de!newsfeed.freenet.de!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX Newsgroups: adobe.scripting.indesign Subject: Re: Replace quoted text to italic without quotation marks Message-ID: <1de6f307.5@WebX.la2eafNXanI> Date: Thu, 12 Dec 2002 12:01:00 -0800 References: <1de6f307.-1@WebX.la2eafNXanI> Lines: 116 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit