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

Find and replace text by condition

Contributor ,
Oct 19, 2023 Oct 19, 2023

Hi all! Can I have a script that finds text with "Condition 1" applied and replaces it with the text from "Condition 2"? I guess it should be a simple copy-paste?

 

I have already created and applied the conditions in the document. No need any fancy options or dialog boxe.


Thanks in advance for the help!

 

Regards,

Rogerio

TOPICS
How to , Scripting
669
Translate
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

correct answers 1 Correct answer

Engaged , Nov 13, 2023 Nov 13, 2023

Hi,

 

Use the Folder object:

var fold = Folder(“some/path”);
var inddFiles =fold.getFiles(“*.indd”);
for (var i = 0; i < inddFiles.length; i++) {
var doc = app.open(inddFiles[i]);
//do stuff
}

Translate
Contributor ,
Oct 20, 2023 Oct 20, 2023

Just to clarify, there's a way to create and apply conditions to a text in InDesign via Conditional Text panel. It works like Layers, but for the text inside the frames instead - the condition marks up (by underlining or highlighting) the selected text and you can easily hide it from the document. It's very useful!

 

If we have a script for an appliedCharacterStyle, we can change it to  "appliedConditions" and I believe it should do the trick 🙂

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

You can always apply a character style to text with a condition manuallxy. Problem is only, if a text has 2 conditions. In such a case you need to create a 3rd character style which represents 2 applied conditions,

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

Thanks for checking @Willi Adelberger! In this case, each text has only 1 condition applied. Basicaly, the script needs to copy the text with "Condition 2" applied and pasted it over (overwrite) the text in the "Condition 1".

Translate
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 13, 2023 Nov 13, 2023

Hi everyone, I have managed to code it myself, but somehow the script only works when I have a single document open. Is there any way to make it work with multiple files? Here's the code:

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

var conditions = app.documents[d].conditions.item("Condition 2").visible = true;

app.findTextPreferences = null;
app.findTextPreferences.appliedConditions = ["Condition 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();
    app.cut();
} catch (_)
{
    }

app.findTextPreferences = null;
app.findTextPreferences.appliedConditions = ["Condition 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(SelectionOptions.REPLACE_WITH);
    app.paste();
} catch (_)
{
    }


 

Translate
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
Engaged ,
Nov 13, 2023 Nov 13, 2023
LATEST

Hi,

 

Use the Folder object:

var fold = Folder(“some/path”);
var inddFiles =fold.getFiles(“*.indd”);
for (var i = 0; i < inddFiles.length; i++) {
var doc = app.open(inddFiles[i]);
//do stuff
}

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Translate
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