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

How can I find-change only in the master pages?

Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi, everyone

How can I find-change only in the master pages?

for instance:

I want to change 2013 to 2014 but only in the master pages.

in all the opening files' master pages.

How can I do that?

Teetan

TOPICS
Scripting
1.2K
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

Mentor , Jul 27, 2014 Jul 27, 2014

Hi Teetan,

To define a goal is more than half way of creating script.

Check this:

app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "grow numbers on masters");

function main() {

  catchOption = app.findChangeGrepOptions.includeMasterPages;

  app.findChangeGrepOptions.includeMasterPages = true;

  app.findGrepPreferences = changeGrepPreferences = null;

  app.findGrepPreferences.findWhat = "\\b\\d+\\b";

var

  mTarget = app.documents.everyItem().masterSpreads.

...
Translate
Mentor ,
Jul 27, 2014 Jul 27, 2014

Hi,

First use find(), after this filter results and finally change those one you need to.

There is no direct option.

Jarek

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Thank you jarek

I will use synchronize in a book

Teetan

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
Enthusiast ,
Jul 27, 2014 Jul 27, 2014

I would do it in this way:

// Your settings

// ---

var SEARCH_PATTERN = '2013';

var CHANGE_PATTERN = '2014';

// Set SEARCH

// ---

app.findTextPreferences = app.changeTextPreferences = null;

app.findChangeTextOptions.includeMasterPages = true;

app.findTextPreferences.findWhat = SEARCH_PATTERN;

app.changeTextPreferences.changeTo = CHANGE_PATTERN;

// Variables

// ---

var allDocs = app.documents,

    nDocs = allDocs.length,

    nFounds = [],

    counter = 0;

   

// SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS

// ---

for ( var i = 0; i < nDocs; i++ ) {

    var curDoc = app.documents,

    mPages = curDoc.masterSpreads.everyItem().pages.itemByRange(0, -1),

    target = mPages.textFrames.everyItem().paragraphs.everyItem();

    target.changeText();

    nFounds.push(target.changeText().length);

}

app.findTextPreferences = app.changeTextPreferences = null;

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

    counter = counter + nFounds;

}

   

alert( counter + " expressions have been changed." );


– Kai

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
Mentor ,
Jul 27, 2014 Jul 27, 2014

Hi,

I was to quick with my 1st answer, Kai is right.

changeText() method can be used with various targets. So if you prepare a target first - there is a direct option.

Jarek

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
Enthusiast ,
Jul 27, 2014 Jul 27, 2014

Hi,

I realize, that here is no need for itemByRange.

So the target line could be writen shorter (no itemByRange needed):

target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();

– Kai

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi, Jarek and Kai

I try the write script by my self like this but not work:

//---------------------------------------------------------------------------------------------------------

app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Indent Paragraphs"); 

function main(){

var _selection = app.selection[0]; 

for ( i = 1; i < app.documents.length; i++) {    

  myDoc = app.documents;    

app.findGrepPreferences = changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "\\b20+\\b"; 

var found = app.selection[0].findGrep(); 

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

        app.findGrepPreferences = changeGrepPreferences = null; 

        app.findGrepPreferences.findWhat = "\\d+"; 

        var foundnum = found.findGrep(); 

        var newnum = parseInt(foundnum[0].contents) + 1; 

        app.changeGrepPreferences.changeTo = newnum.toString(); 

        found.changeGrep(); 

         } 

     }

}

//----------------------------------------------------------------------------------------------------------

how can fix it?

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi,

How can I combine the two scripts idea into one?

Teetan

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
Mentor ,
Jul 27, 2014 Jul 27, 2014

@Teetan

Which numbers are you going to grow?

- each found on master pages

- only the one started with "20"

- is it posible to match a number 2099 there?

Jarek

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi, I try again

changed it into this:

but not error

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Find-change num"); 

function main(){

    for ( var i = 0; i < nDocs; i++ ) { 

        var curDoc = app.documents

        mPages = curDoc.masterSpreads.everyItem().pages.itemByRange(0, -1), 

        target = mPages.textFrames.everyItem().paragraphs.everyItem(); 

app.findGrepPreferences = changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "\\b20+\\b"; 

var found = app.selection[0].findGrep(); 

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

        app.findGrepPreferences = changeGrepPreferences = null; 

        app.findGrepPreferences.findWhat = "\\d+"; 

        var foundnum = found.findGrep(); 

        var newnum = parseInt(foundnum[0].contents) + 1; 

        app.changeGrepPreferences.changeTo = newnum.toString(); 

        found.changeGrep(); 

         } 

     }

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Teetan

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi,

Hi, Jarek and Kai

what I want to change has growing requested, and I wish it has an undo possible.

thank you

Teetan

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi

Jarek

may be 2099

Teetan

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
Enthusiast ,
Jul 27, 2014 Jul 27, 2014

Hi,

I changed my current version in the meantime a bit, since the counter did show the wrong results, if you start the script a second time.

// Your settings

// ---

var SEARCH_PATTERN = '2013';

var CHANGE_PATTERN = '2014';

// Set SEARCH

// ---

app.findTextPreferences = app.changeTextPreferences = null;

app.findChangeTextOptions.includeMasterPages = true;

app.findTextPreferences.findWhat = SEARCH_PATTERN;

app.changeTextPreferences.changeTo = CHANGE_PATTERN;

// Variables

// ---

var allDocs = app.documents,

    nDocs = allDocs.length,

    nFounds = [],

    counter = 0;

 

// SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS

// ---

for ( var i = 0; i < nDocs; i++ ) {

    var curDoc = app.documents,

    target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem(); 

    var allFounds = target.changeText();

 

    for (var n = 0; n < allFounds.length; n ++) {

        var curFound = allFounds;

        if (curFound.length != 0) {

            counter += curFound.length;

        }

    }

}

app.findTextPreferences = app.changeTextPreferences = null;

alert( counter + " expressions have been changed." );

Teetan, I have no idea what you try to do with your second script. Please provide clear steps (1,2,3) and maybe a screenshot before / after.

And: Realize that 'undo' is available only per document. So if you have 20 docs open, you must undo in every document, even if we spent a doScript-Function!

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi,

Kai

sorry I confuse you in some way

now I clarify it

1)  I want to find-change the digit only in all the opening files' master

2)  the digit has growing requested by years, I mean this year I find 2013 change to 2014 and next year I have to chan 2014 change to 2015

and finally I wish the script has UndoModes.ENTIRE_SCRIPT function

Regard

Teetan

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi Kai

this script can find digit then change +1

//---------------------------------------------------------------------------------------------

app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Indent Paragraphs"); 

function main(){

var _selection = app.selection[0]; 

app.findGrepPreferences = changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "aged \\d+"; 

var found = app.selection[0].findGrep(); 

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

        app.findGrepPreferences = changeGrepPreferences = null; 

        app.findGrepPreferences.findWhat = "\\d+"; 

        var foundnum = found.findGrep(); 

        var newnum = parseInt(foundnum[0].contents) + 1; 

        app.changeGrepPreferences.changeTo = newnum.toString(); 

        found.changeGrep(); 

    } 

app.findGrepPreferences = changeGrepPreferences = null; 

 

//-----------------------------------------------------------------------------------------------------

I get this idea, but I want it only work on master pages

Regard

Teetan

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi Kai

can you help correct this script:

        app.doScript('main()', ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Find-change digit" );          

function main(){ 

// Set SEARCH 

// --- 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2017"; 

app.changeTextPreferences.changeTo = "2018"; 

 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2015"; 

app.changeTextPreferences.changeTo = "2016"; 

 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2014"; 

app.changeTextPreferences.changeTo = "2015"; 

 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2013"; 

app.changeTextPreferences.changeTo = "2014"; 

 

// Variables 

// --- 

var allDocs = app.documents, 

    nDocs = allDocs.length, 

    nFounds = [], 

    counter = 0; 

   

// SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS 

// --- 

for ( var i = 0; i < nDocs; i++ ) { 

    var curDoc = app.documents

    target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();   

    var allFounds = target.changeText(); 

   

    for (var n = 0; n < allFounds.length; n ++) { 

        var curFound = allFounds

        if (curFound.length != 0) { 

            counter += curFound.length;

                }

        } 

    } 

 

app.findTextPreferences = app.changeTextPreferences = null; 

 

alert( counter + " expressions have been changed." );

Teetan

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
Mentor ,
Jul 27, 2014 Jul 27, 2014

Hi Teetan,

To define a goal is more than half way of creating script.

Check this:

app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "grow numbers on masters");

function main() {

  catchOption = app.findChangeGrepOptions.includeMasterPages;

  app.findChangeGrepOptions.includeMasterPages = true;

  app.findGrepPreferences = changeGrepPreferences = null;

  app.findGrepPreferences.findWhat = "\\b\\d+\\b";

var

  mTarget = app.documents.everyItem().masterSpreads.everyItem().textFrames.everyItem(),

  mFound = mTarget.findGrep(),

  mConFound = [],

  len = mFound.length;

  if (len && mFound[0].constructor.name == "Array")

       while (len-->0)

            mConFound = mConFound.concat(mFound[len]);

  len = mConFound.length;

  while (len-->0) {

       mConFound[len].contents = String(Number(mConFound[len].contents) + 1);

  }

  app.findChangeGrepOptions.includeMasterPages = catchOption;

}

Jarek

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi Jreak,

it's what I want

thank you so much

appreciate

Teetan

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi Jreak

Can you tell me

this script can only work on:

//---------------------------------------------------------------------------------------------------

app.doScript('main()', ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Find-change digit" );          

function main(){ 

// Set SEARCH 

// --- 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2017"; 

app.changeTextPreferences.changeTo = "2018"; 

 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2015"; 

app.changeTextPreferences.changeTo = "2016"; 

 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findChangeTextOptions.includeMasterPages = true; 

app.findTextPreferences.findWhat = "2014"; 

app.changeTextPreferences.changeTo = "2015"; 

 

//========================================================

app.findTextPreferences = app.changeTextPreferences = null;    // only work on this part

app.findChangeTextOptions.includeMasterPages = true;           // the rest are unfunction

app.findTextPreferences.findWhat = "2013";                            // can you tell me why?

app.changeTextPreferences.changeTo = "2014";                     //  can you make it happen, in case of find something not a digit

//============================================================

 

// Variables 

// --- 

var allDocs = app.documents, 

    nDocs = allDocs.length, 

    nFounds = [], 

    counter = 0; 

   

// SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS 

// --- 

for ( var i = 0; i < nDocs; i++ ) { 

    var curDoc = app.documents

    target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();   

    var allFounds = target.changeText(); 

   

    for (var n = 0; n < allFounds.length; n ++) { 

        var curFound = allFounds

        if (curFound.length != 0) { 

            counter += curFound.length;

                }

        } 

    } 

 

app.findTextPreferences = app.changeTextPreferences = null; 

 

alert( counter + " expressions have been changed." );

}

//---------------------------------------------------------------------------------------------------

regard

Teetan

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
Enthusiast ,
Jul 27, 2014 Jul 27, 2014

Jarek, your version ist much shorter, than my script, great

I have this in the meantime:

if ( app.documents.length > 0 ) {

    if ( app.scriptPreferences.version >= 6 ) {

        app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Search in Masterpages" );       

    }

    else {

        main();

    }

} else {

    alert ( "No documents open!" );

}

function main() {

    // Your settings

    // ---

    var SEARCH_PATTERN = '\\b201[4-6]\\b';

    // Set SEARCH

    // ---

    app.findGrepPreferences = app.changeGrepPreferences = null;

    app.findChangeGrepOptions.includeMasterPages = true;

    app.findGrepPreferences.findWhat = SEARCH_PATTERN;

    // Variables

    // ---

    var allDocs = app.documents,

        nDocs = allDocs.length,

        nFounds = [],

        counter = 0;

      

    // SEARCH ONLY MASTERPAGES IN ALL OPEN DOCS

    // ---

    for ( var i = 0; i < nDocs; i++ ) {

        var curDoc = app.documents,

        target = curDoc.masterSpreads.everyItem().pages.everyItem().textFrames.everyItem();  

        var allFounds = target.findGrep();

      

        for ( var n = 0; n < allFounds.length; n ++ ) {

            var framesFound = allFounds;

            if ( framesFound.length > 0 ) {

                 for ( var j = framesFound.length-1; j >= 0 ; j-- ) {

                            var curFound = framesFound;

                        var newNumber = (parseInt( curFound.contents) +1).toString();

                        curFound.contents = newNumber;                                                  

                } // for

            counter += framesFound.length;

            } // if

        } // for

    } // for

    app.findGrepPreferences = app.changeGrepPreferences = null;

    alert( counter + " expressions have been changed." );

} // main

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
Explorer ,
Jul 27, 2014 Jul 27, 2014

Hi, Kai

what if find-change something not a digit, but only in master?

Teetan

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
Enthusiast ,
Jul 27, 2014 Jul 27, 2014

Teetan, first of all, after Jareks code, I correct my version a bit. Nevertheless my version is a bit longer than Jareks, because I check some things more, have a alert at the end and I’m not able to write such short scripts as Jarek (as a advanced beginner I have always problems with while loops …).

The trick ist the line nr. 16 SEARCH_Pattern. Here you can enter the right expression. E.g. if you want to find 2013 - 2016 change [4-6] to [3-6]. However: The script assumes, that you want to grow up numbers +1. So if you want to change other things on the masterpages, all those scripts from today won’t work and must be modified > new goals >> new scripts 😉

if ( app.documents.length > 0 ) {

    if ( app.scriptPreferences.version >= 6 ) {

        app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Search in Masterpages" );      

    }

    else {

        main();

    }

} else {

    alert ( "No documents open!" );

}

function main() {

    // Your settings

    // ---

    var SEARCH_PATTERN = '\\b201[4-6]\\b';

    app.findGrepPreferences = app.changeGrepPreferences = null;

    app.findChangeGrepOptions.includeMasterPages = true;

    app.findGrepPreferences.findWhat = SEARCH_PATTERN;

    var target = app.documents.everyItem().masterSpreads.everyItem().textFrames.everyItem();         

    var allFounds = target.findGrep();

 

    var counter = 0;

 

    for ( var n = 0; n < allFounds.length; n ++ ) {

        var framesFound = allFounds;

        if ( framesFound.length > 0 ) {

            for ( var j = framesFound.length-1; j >= 0 ; j-- ) {

                var curFound = framesFound;

                curFound.contents = String( Number( curFound.contents) + 1 );                                                 

            }

        counter += framesFound.length;

        } // if

    } // for

    app.findGrepPreferences = app.changeGrepPreferences = null;

    alert( counter + " expressions have been changed." );

} // main

but not today. Sunny wheather. Let’s go outside .

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
Explorer ,
Jul 27, 2014 Jul 27, 2014
LATEST

thanks

Kai

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