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

Need script for find and change text

New Here ,
Aug 26, 2010 Aug 26, 2010

We have some set of texts that need to find and change in the entire InDesign CS4 document rather than doing manually. Kindly help me out to do the same successfully.

for e.g. find text - "mam" and replace with "man".

TOPICS
Scripting
8.9K
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
Advisor ,
Aug 26, 2010 Aug 26, 2010

Hey!

This will do what you want:

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;


app.findTextPreferences.findWhat = "mam";
app.changeTextPreferences.changeTo = "man";

//this will show changes count

alert(app.activeDocument.changeText().length);


app.activeDocument.changeText();

--

tomaxxi

http://indisnip.wordpress.com/

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 ,
Aug 26, 2010 Aug 26, 2010

Try below code if you have multiple texts for replace.

replaceText ("mam", "man");
replaceText ("mam1", "man1");


function replaceText (input, output)
{
     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
     app.findTextPreferences.findWhat = input;
     app.changeTextPreferences.changeTo = output;
     app.activeDocument.changeText();
     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}

Shonky

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
New Here ,
Aug 27, 2010 Aug 27, 2010

Thanks for sending us the wonderful script. I need one more help on this.

I need to find the character with the specific font and change to specific font

or

I need to find the character with the specific style and change to specific style

Please help the same to do successfully.

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
Advisor ,
Aug 27, 2010 Aug 27, 2010

Hey!

This is little extended version of script that Shonky wrote. You can search/find for text, paragraph styles, character styles, or in combination.

findChange(/*find text*/ 'mam', /*change to text*/ 'man', /*old style*/ '', /*new style*/ '', /*old char style*/ 'Bold Text', /*new char style*/ 'Extended Bold Text');

function findChange (findWhat, changeTo, findStyle, changeStyle, findChrStyle, changeChrStyle){
     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
     if(findWhat)app.findTextPreferences.findWhat = findWhat;
     if(changeTo)app.changeTextPreferences.changeTo = changeTo;
     if(findStyle)app.findTextPreferences.appliedParagraphStyle = findStyle;
     if(changeStyle)app.changeTextPreferences.appliedParagraphStyle = changeStyle;
     if(findChrStyle)app.findTextPreferences.appliedCharacterStyle = findChrStyle;
     if(changeChrStyle)app.changeTextPreferences.appliedCharacterStyle = changeChrStyle;
     app.activeDocument.changeText();
     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}

Hope it helps!

--

tomaxxi

http://indisnip.wordpress.com/

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 ,
Sep 19, 2016 Sep 19, 2016

Hi Marijan Tompa,

Can you help me?

How to find superscript?

My complete script is here:

replace ("(.+)", "Italic", "", "SRF Cyan", "", "SRF_it");

replace ("(.+)", "Italic", "", "", "", "_it");

replace ("(.+)", "Bold", "", "SRF Cyan", "", "SRF_bld");

replace ("(.+)", "Bold", "", "", "", "_bld");

//replace ("(.+)", "", "position", "", "_^$1", "");

//replace ("", "", "", "", "", "");

function replace(findWhat, fontStyle, position, fillColor, changeTo, chrStyle){

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

    if(findWhat)app.findGrepPreferences.findWhat = findWhat;

    if(fontStyle)app.findGrepPreferences.fontStyle = fontStyle;

//    if(position)app.findGrepPreferences.postion = Position.SUPERSCRIPT;

    if(fillColor)app.findGrepPreferences.fillColor = fillColor;

    if(changeTo)app.changeGrepPreferences.changeTo = changeTo;

    if(chrStyle)app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyleGroups.item("03_BodText").characterStyles.item(chrStyle);

    app.activeDocument.changeGrep();

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

    };

-Sumit
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 ,
Aug 28, 2010 Aug 28, 2010

Hey,


Use the following code:


replaceTextByFont ("mam", "man");
replaceTextByStyle ("mam", "man");


function replaceTextByStyle (input, output)
{
    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
    app.findTextPreferences.findWhat = input;
    app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("SampleFindStyle");
    app.changeTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("SampleReplaceStyle");
    app.changeTextPreferences.changeTo = output;
    app.activeDocument.changeText();
    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}

function replaceTextByFont (input, output)
{
    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
    app.findTextPreferences.findWhat = input;
    app.findTextPreferences.appliedFont = app.fonts.item("Arial Bold");
    app.changeTextPreferences.appliedFont = app.fonts.item("Arial BoldItalic");
    app.changeTextPreferences.changeTo = output;
    app.activeDocument.changeText();
    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}


Regards,

Ramkumar .P

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
Advisor ,
Aug 28, 2010 Aug 28, 2010

Hey Ramkumar!

I think it's not good idea to change applied font because it will override applied paragraph/character style. Also, "Arial Bold" needs to be "Arial\tBold" and "Arial BoldItalic" needs to be "Arial\tBold Italic". You need "\t" because it separates font family name and font style.

--

tomaxxi

http://indisnip.wordpress.com/

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 ,
Sep 19, 2016 Sep 19, 2016

Hi Ramkumar,

Please help me, how to find superscript? My complete script is here:

replace ("(.+)", "Italic", "", "SRF Cyan", "", "SRF_it");

replace ("(.+)", "Italic", "", "", "", "_it");

replace ("(.+)", "Bold", "", "SRF Cyan", "", "SRF_bld");

replace ("(.+)", "Bold", "", "", "", "_bld");

//replace ("(.+)", "", "position", "", "_^$1", "");

//replace ("", "", "", "", "", "");

function replace(findWhat, fontStyle, position, fillColor, changeTo, chrStyle){

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

    if(findWhat)app.findGrepPreferences.findWhat = findWhat;

    if(fontStyle)app.findGrepPreferences.fontStyle = fontStyle;

//    if(position)app.findGrepPreferences.postion = Position.SUPERSCRIPT;

    if(fillColor)app.findGrepPreferences.fillColor = fillColor;

    if(changeTo)app.changeGrepPreferences.changeTo = changeTo;

    if(chrStyle)app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyleGroups.item("03_BodText").characterStyles.item(chrStyle);

    app.activeDocument.changeGrep();

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

    };

Regards,

Sumit

-Sumit
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
New Here ,
Sep 23, 2016 Sep 23, 2016
LATEST

Hi,

It is very useful but it doesn't work in CS6? Help!

Thanks

cy

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