Search and replace discrete text within text blocks

hi all,
does anyone have a script for searching and replacing discrete text occurrences within text blocks?
ex: i want to change a mess load of “transom a-1” “transom b-1” to “panel a-1” etc.

as we all know, Rhino’s search is great for finding the textblock containing what we’re looking for, but not for replacing that one word when its contained in a larger block of text.
thanks

Hi Robert,

If I understood you correctly both “transom a-1” and “transom b-1” need to be replaced with “panel a-1”?

If that is so, try this script until more experienced users arrive:

(after you run the script, select everything, or just press “CTRL+A” and then hit “Enter”)

import rhinoscriptsyntax as rs

# filtering text objects:
guids = rs.GetObjects("Select everything")
textGuids = []
for guid in guids:
    if rs.IsText(guid): 
       textGuids.append(guid)

# getting string from text objects:
originalTexts = []
for guid in textGuids:
    originalText = rs.TextObjectText(guid)
    originalTexts.append(originalText)

# changing the text object strings:
newTexts = []
for originalText in originalTexts:
    newText = originalText.replace("transom a-1","panel a-1")
    newText = newText.replace("transom b-1", "panel b-1")
    newTexts.append(newText)

# applying the text object strings:
for i in range(len(textGuids)):
    rs.TextObjectText(textGuids[i],newTexts[i])

sorry to be unclear. in the example case I need to replace the word transom with another word. Changing the prefix or suffix in a text string for example.

In that case you just need to delete the upper row:

newText = newText.replace("transom b-1", "panel b-1")

Also in the row above the upper one:
replace the “transom a-1” and “panel a-1” with the correct words. Like so:

import rhinoscriptsyntax as rs

# filtering text objects:
guids = rs.GetObjects("Select everything")
textGuids = []
for guid in guids:
    if rs.IsText(guid): 
       textGuids.append(guid)

# getting string from text objects:
originalTexts = []
for guid in textGuids:
    originalText = rs.TextObjectText(guid)
    originalTexts.append(originalText)

# changing the text object strings:
newTexts = []
for originalText in originalTexts:
    newText = originalText.replace("transom a-1","panel a-1")   # REPLACE YOUR WORDS HERE
    newTexts.append(newText)

# applying the text object strings:
for i in range(len(textGuids)):
    rs.TextObjectText(textGuids[i],newTexts[i])

Hi Carvecream- here’s a Rhinoscript that might help- pretty old but it seems to work OK.

To use the script, extract and save the .rvb file from the attached zip archive, then drag and drop the saved rvb over an open Rhino V4 or v5 window. This will load the script, set it up to load on startup in the future and register the alias

ReplaceText

that will run the script much like a regular command. An alias can be typed or added to a toolbar button or keyboard shortcut (F-key).

ReplaceText.zip (1.4 KB)

-Pascal

thank you both.
Pascal that one is very handy. Side note question, why is it that I can’t run some scripts from Monkey? This one for example only runs when activated from the command line.

Hi Carvecream - to run directly from the editor, or when loaded, there needs to be a call into the subroutine right in the script. If you put

Call ReplaceText() right above

Sub ReplaceText()

in the script it will run from the editor but make sure you comment it out before saving or the script will try to run every time Rhino starts if you have drag-n-dropped it.

-Pascal

ok thank you. someday I will tackle learning Rhinoscript.