• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
11

Find and replace text by formatting

Contributor ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

Hi guys,

I need a script which find/cut the text with "Character Style 1" applied and replace the text "Character Style 2" with that cutted text. Basically, we are just moving the content from one place to another in the document, so a simple cut-paste script would do the work. Also, please make it work across multiple files. I have already created and applied the Character Styles to the two instances, so no needed any fancy options or dialog boxe.


Thanks in advance for the help!

 

Regards,

Rogerio

TOPICS
Scripting

Views

513

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

Hi @Rogerio5C09 .

As I understand it, you want to
a) Locate the ORIGIN "A" (based on the character style already applied); and
b) Move it to DESTINATION "B" (destination located by another character style).

 

So far, it seems to me that perhaps a find/replace in the GREP tab (by formatting) would solve the issue. Even if it's done manually, it's a big step forward. Although you've already gone to the trouble of applying the styles to both source "A" and destination "B"... why not just apply the correct content...?

 

Questions:
1) Is the origin A the B or are there other destinations on the way? Like: will occurrence A be sent to the first occurrence B it finds on the way or will it have to "skip" some other occurrence "B"?

 

2) On arrival, should "A" remain the same CHARACTER STYLE it already has, or should it assume the character style present at destination B?

 

3) Will the CONTENT of the destination, B, simply be replaced by the content of the arriving A?

 

I imagine your scenario should be something like this:

 

"A text <stA>which has character style applied</stA> must be located and its content moved to a destination identified by a character <stB>x</stB> in order for the text to reach its target. But... what if there's another <stB>x</stB> destination on the way? Will our traveling content stay at the first occurrence, or should it simply go elsewhere? The cruel doubt..."

 

In this example, with tags to simulate the application of character styles, I imagine it should be your scenario.... correct?

 

Can you give us a bit more context about what you need? Maybe some screenshots of your document...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

Hi @Brito Haroldo, thanks for checking!

There are only occorrences A (CS1) and B (CS2). The Character Style can remain the same. And yes, the content of the destination B will simply be replaced by A, but the thing is that I have a bunch of documents like that, so I'm looking for a way to automate it.

I'm attaching a sample indd here. Could you please take a look?

@Robert at ID-Tasker  and @brian_p_dts, feel free to give it a shoot as well 🙂

Thanks,
Rogerio

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

@Rogerio5C09, not sure if conversion went wrong - I'm still on v18 - or your document really looks like this:

RobertTkaczyk_0-1700165112606.png

 

 

OK ... after few seconds of checking your document:

RobertTkaczyk_1-1700165316125.png

RobertTkaczyk_0-1700165604714.png

 

So you want to replace text [LOGO] - on the left - with a graphic representation - on the right?

 

Maybe it would be easier / better / more convenient to link to an external file?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

If you work on a PC, it would be piece of cake for my ID-Tasker.

 

Otherwise, as you are mentioning multiple documents - what would be the key for which occurence of CharStyle2 should be replaced with CharStyle1?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

Yeah we'd need to know which specific instances of CS2 to target. Is there some identifer in the text of CS1 that we could use, or as someone else mentioned, is the first instance being replaced by the corresponding first instance? Are they in the same story or different stories?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 17, 2023 Nov 17, 2023

Copy link to clipboard

Copied

Hi everyone,

The script below is exactly what I need, however I'm having a hard time trying to make it work with multiple open documents. Can anyone take a look and run some tests with the 3 sample INDDs attached? Thx!

 

for (var d=0; d<app.documents.length; d++)
{
var doc = app.documents[d];

app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = "Character Style 1";
var found = doc.findText();
for(var i =0;i<found.length;i++)
{
    try{
        found[i].select();
     }catch(e){}
    }
app.findTextPreferences = null;
   
try {
	app.selection[0].texts[0].select();
    app.cut();
} catch (_)
{
    }

app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = "Character Style 2";
var found = doc.findText();
for(var i =0;i<found.length;i++)
{
    try{
        found[i].select();
     }catch(e){}
    }
app.findTextPreferences = null;
   
try {
	app.selection[0].texts[0].select(SelectionOptions.REPLACE_WITH);
    app.paste();
} catch (_)
{
    }
   }
alert("Done!");

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2023 Nov 17, 2023

Copy link to clipboard

Copied

I'm on my phone, but looking at the code - it's a bit strange... or am I unable to correctly pair {}??

 

Both loops are the same:

 

for(var i =0;i<found.length;i++)

{

    try{

        found[i].select();

     }catch(e){}

     }

 

but they are just going through all found results - for no reason? Always only the last result will be selected? 

 

Is it another ChatGPT generated code ?? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2023 Nov 17, 2023

Copy link to clipboard

Copied

LATEST

Yeah, just checked on my laptop in Notepad2 - this code doesn't make much sense ?!?!

RobertTkaczyk_0-1700266978402.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines