🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: adobe.scripting.indesign
6 messages
6 total messages Started by Olav Kvern Mon, 13 May 2002 17:55
Find/Change by list
#21046
Author: Olav Kvern
Date: Mon, 13 May 2002 17:55
7 lines
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
Re: Find/Change by list
#21064
Author: John Jordan
Date: Mon, 13 May 2002 23:45
17 lines
1632 bytes
Ole,  

Thanks a million! I got it working! (well sort of, see below.) I would NEVER have been able to do it without the sample you provided here. 

Now, I have one small problem left. A lot of the test questions have the following for answers (C) and (D) -- 

Both A and B Neither A nor B 

I got it all working, including the "B" in the above, but I can't figure out how to do the "A" -- if I tell it to do it on "[space]A[space]" it will also do it on a lot of sentences that start with "A." 

I'm thinking that I will need another loop that searches for "Both A" and "Neither A" and then changes just the rightmost character of each string. 

Am I thinking this through correctly?  

Has anyone told you what a gold mine you are? 

Oh, and I haven't figured out how to get the script into the InDesign palette yet. I'm running it from Visual Basic window. But I'll deal with that after I figure out this last issue.
Re: Find/Change by list
#21065
Author: Olav Kvern
Date: Tue, 14 May 2002 00:07
17 lines
2265 bytes
John-- 

Sure. This is a little bit trickier, because we first find the phrases we're looking for, then we tell the found text objects (the result of the search) to search themselves for the characters we want to change. Add this bit following the line "Rem Set the find/change preferences back to their default state." 

Set myFoundItems = myStory.Search("Both A and B", False, False) For myCounter = 1 To myFoundItems.Count     Rem Now tell each found item to search for "A" and change its formatting.     Set myFoundItem = myFoundItems.Item(myCounter)     myFoundItem.Search "A", True, True, "A"     myFoundItem.Search "B", True, True, "B" Next Set myFoundItems = myStory.Search("Neither A nor B", False, False) For myCounter = 1 To myFoundItems.Count     Rem Now tell each found item to search for "A" and change its formatting.     Set myFoundItem = myFoundItems.Item(myCounter)     myFoundItem.Search "A", True, True, "A"     myFoundItem.Search "B", True, True, "B" Next  

Once you've got the script working the way you want, create an executable file--you do this by choosing "make" from the VB file menu. This creates an application--but the application, or a shortcut to it--in the Script folder and you'll be able to start it from inside InDesign. 

Let me know if this works for you! 

re: "Has anyone told you what a gold mine you are?" 

I'm hoping Adobe will, at my next job performance review.<g> 

Thanks, 

Ole
Re: Find/Change by list
#21071
Author: John Jordan
Date: Tue, 14 May 2002 16:01
1 lines
868 bytes
OK, got the script working perfectly. But "Make" is grayed out. I saved the Project and the script runs without errors. What else must I do?
Re: Find/Change by list
#21072
Author: Olav Kvern
Date: Tue, 14 May 2002 17:07
7 lines
999 bytes
John-- 

Hmm. I think you can compile with VB5 CCE, but I'm not sure. While I find out, why don't you send me the project--I'll compile it for you and send back the executable. 

Thanks, 

Ole
Re: Find/Change by list
#21074
Author: John Jordan
Date: Tue, 14 May 2002 19:55
1 lines
885 bytes
OK, got the script working perfectly. But "Make" is grayed out. I saved the Project and the script runs without errors. What else must I do?
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads