Skip to main content
Inspiring
July 18, 2022
Answered

Grep search in book

  • July 18, 2022
  • 3 replies
  • 3240 views

Hi,

I have a book with 230 files in it and I wonder if it is possible to do a grep search in those files without open it.

My skill in scripting its quite low so here is my question ¿do i fight with the code to try it? or ¿is it better to drink a beer with my friends because it's not possible?

Thanks.

This topic has been closed for replies.
Correct answer Peter Kahrel

Hi Laubernder, thanks for your response.

If it's possible I don't want to open 230 files and use the find and change panel to perform the change in all the opened documents.

This the first book I have, there is another one with 180 files, so i wonder if it possible to code a script that find and change an expression in the files (without open it) of an existing book file. I assume that if it is possible find/change a single expression, i can loop an array populate with expressions to perform the find/change.


It's not even possible to open 230 documents: InDesign lets you open about 115 documents. One way to go about it is to create a simple script to do the replacement in one document, then use that script in a batch-processor like https://creativepro.com/files/kahrel/indesign/batch_convert.html

P.

3 replies

willcampbell7
Legend
July 20, 2022

Search only, or search and replace? I have this one to search and replace text or GREP on a folder of documents (not free but it has a trial).

http://www.marspremedia.com/software/indesign/find-change-multi-doc

 

William Campbell
nicoshAuthor
Inspiring
July 21, 2022

Thanks @willcampbell7 

Your script it is for "global" searches and I need to narrow down to some paragraphs with a paragraph style applied.

brian_p_dts
Community Expert
Community Expert
July 23, 2022

Below is the general flow for opening a book document and closing it. You don't need to open all files at once; just iterate one at a time. You'd need to incporate your GREP routines into the script, probably as a separate routine in the while loop. 

 

 

var bookContents = app.activeBook.bookContents;
var n = bookContents.length;
var d;
while (n--) {
    //skip docs with pending changes
    if (bookContents[n].status !== BookContentStatus.NORMAL) continue;
    //need to do your own GREP find change routine
    d = app.open(bookContents[n].fullName, false);
    d.changeGrep();
    d.close(SaveOptions.YES);
}

 

 

 

Kasyan Servetsky
Legend
July 19, 2022

Hi nicosh,

After minor changes, your script works with my batch processor.

Note: the variable called ‘g’ is the only variable that is not allowed. It’s used for exchanging data between both scripts. So, I replaced it with ‘j’. Also, I changed the font to Minion Pro and the character style to None (I am on the English version).  But ‘g.doc’ I left as it is: it’s the currently open document in the batch processor.

Here are recommended settings:

The result

My version of the script:

var i;

var j = {};
j.cajasTexto = g.doc.stories;
j.estilosParrafo = g.doc.paragraphStyles;
j.estilosCaracter = g.doc.characterStyles;
j.fuente = "Minion Pro";
j.estiloAplicado = j.estilosParrafo.itemByName("EW_Cat_03_Info_block");
j.estiloAplicadoDos = j.estilosParrafo.itemByName("EW_Cat_07_Bummer_info_block");
j.estiloAplicadoTres = j.estilosParrafo.itemByName("EW_Cat_07_Bummer_info_block_NO_RULE");
j.estiloCaracterAplicado = j.estilosCaracter.itemByName("[None]");
j.horas = [['1am', '1.00'], ['1:15am', '1.15'], ['1:30am', '1.30'], ['1:45am', '1.45'],
          ['2am', '2.00'], ['2:15am', '2.15'], ['2:30am', '2.30'], ['2:45am', '2.45'],
          ['3am', '3.00'], ['3:15am', '3.15'], ['3:30am', '3.30'], ['3:45am', '3.45'],
          ['4am', '4.00'], ['4:15am', '4.15'], ['4:30am', '4.30'], ['4:45am', '4.45'],
          ['5am', '5.00'], ['5:15am', '5.15'], ['5:30am', '5.30'], ['5:45am', '5.45'],
          ['6am', '6.00'], ['6:15am', '6.15'], ['6:30am', '6.30'], ['6:45am', '6.45'],
          ['7am', '7.00'], ['7:15am', '7.15'], ['7:30am', '7.300'], ['7:45am', '7.45'],
          ['8am', '8.00'], ['8:15am', '8.15'], ['8:30am', '8.30'], ['8:45am', '8.45'],
          ['9am', '9.00'], ['9:15am', '9.15'], ['9:30am', '9.30'], ['9:45am', '9.45'],
          ['10am', '10.00'], ['10:15am', '10.15'], ['10:30am', '10.30'], ['10:45am', '10.45'],
          ['11am', '11.00'], ['11:15am', '11.15'], ['11:30am', '11.30'], ['11:45am', '11.45'],
          ['1pm', '13.00'], ['1:15pm', '13.15'], ['1:30pm', '13.30'], ['1:45pm', '13.45'],
          ['2pm', '14.00'], ['2:15pm', '14.15'], ['2:30pm', '14.30'], ['2:45pm', '14.45'],
          ['3pm', '15.00'], ['3:15pm', '15.15'], ['3:30pm', '15.30'], ['3:45pm', '15.45'],
          ['4pm', '16.00'], ['4:15pm', '16.15'], ['4:30pm', '16.30'], ['4:45pm', '16.45'],
          ['5pm', '17.00'], ['5:15pm', '17.15'], ['5:30pm', '17.30'], ['5:45pm', '17.45'],
          ['6pm', '18.00'], ['6:15pm', '18.15'], ['6:30pm', '18.30'], ['6:45pm', '18.45'],
          ['7pm', '19.00'], ['7:15pm', '19.15'], ['7:30pm', '19.30'], ['7:45pm', '19.45'],
          ['8pm', '20.00'], ['8:15pm', '20.15'], ['8:30pm', '20.30'], ['8:45pm', '20.45'],
          ['9pm', '21.00'], ['9:15pm', '21.15'], ['9:30pm', '21.30'], ['9:45pm', '21.45'],
          ['10pm', '22.00'], ['10:15pm', '22.15'], ['10:30pm', '22.30'], ['10:45pm', '22.45'],
          ['11pm', '23.00'], ['11:15pm', '23.15'], ['11:30pm', '23.30'], ['11:45pm', '23.45'],
          ['12am', '12.00'], ['12:15am', '12.15'], ['12:30am', '12.30'], ['12:45am', '12.45'],
          ['12pm', '12.00'], ['12:15pm', '12.15'], ['12:30pm', '12.30'], ['12:45pm', '12.45'],];
j.diasSemana = [['(?<=(\\s|~=|-|\\())Mon', 'lu'],
                ['(?<=(\\s|~=|-|\\())Tue', 'ma'],
                ['(?<=(\\s|~=|-|\\())Wed', 'mié'],
                ['(?<=(\\s|~=|-|\\())Thu', 'ju'],
                ['(?<=(\\s|~=|-|\\())Fri', 'vi'],
                ['(?<=(\\s|~=|-|\\())Sat', 'sá'],
                ['(?<=(\\s|~=|-|\\())Sun', 'do']];
j.meses = [['(?<=(\\s|~=|-|\\())Jan', 'ene'],
           ['(?<=(\\s|~=|-|\\())Feb', 'feb'],
           ['(?<=(\\s|~=|-|\\())Mar', 'mar'],
           ['(?<=(\\s|~=|-|\\())Apr', 'abr'],
           ['(?<=(\\s|~=|-|\\())May', 'may'],
           ['(?<=(\\s|~=|-|\\())Jun', 'jun'],
           ['(?<=(\\s|~=|-|\\())Jul', 'jul'],
           ['(?<=(\\s|~=|-|\\())Aug', 'ago'],
           ['(?<=(\\s|~=|-|\\())Sep', 'sep'],
           ['(?<=(\\s|~=|-|\\())Oct', 'oct'],
           ['(?<=(\\s|~=|-|\\())Nov', 'nov'],
           ['(?<=(\\s|~=|-|\\())Dec', 'dic']];

j.otrosCambios = [['daily', 'todos los días'], ['noon', 'mediodía'], ['midnight', 'medianoche'], ['then taxi', 'luego taxi'], ['last adm', 'última admisión'], ['public hols', 'festivos']];

//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;

cambiarTodosItem(j.horas);
cambiarTodosItem(j.diasSemana);
cambiarTodosItem(j.meses);
cambiarTodosItem(j.otrosCambios);

function cambiarTodosItem(myArray_param) {
  for (i = 0; i < myArray_param.length; i++) {
    cambiarItem(myArray_param, j.estiloAplicado); //******CAMBIAR
    //Clear the find/change preferences after the search.
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
  }
}

function cambiarItem(item_arr, estiloAplicado_param) {
  app.findGrepPreferences.appliedParagraphStyle = estiloAplicado_param;
  app.findGrepPreferences.appliedFont = j.fuente;
  app.findGrepPreferences.appliedCharacterStyle = j.estilosCaracter.itemByName("[None]");
  for (i = 0; i < item_arr.length; i++) {
    app.findGrepPreferences.findWhat = item_arr[i][0];
    app.changeGrepPreferences.changeTo = item_arr[i][1];
    g.doc.changeGrep(); 
  }
}
nicoshAuthor
Inspiring
July 21, 2022

hi @Kasyan Servetsky

Doesn't work for me -> Finished. 0 documents were processed

 

I've replaced Minion with Soho and Character Style None to Ninguno.

I'm not testing on a book.

I've duplicated some files moved to another folder and test your solution on it (Folder and its subfolders option).

My code it's encapsulated in ¿anonymous Function?, the @Laubender solution.

 

One question: ¿why in your code there is "g."? Lines 4-6 and g.doc.changeGrep()

I've changed the remaining "g." with "j." and doesn't work.

 

 

 

Kasyan Servetsky
Legend
July 22, 2022

Try it with my version of the script posted above: just replace Minion with Soho and Character Style None to Ninguno.
I haven't tested BP with scripts using anonymous functions so have no idea if it would work or not.

g — is the global variable containing so to say sub-variables and functions to interact with 'secondary' scripts.

g.doc — as I mentioned in my previous post – is the reference to the currently opened doc in BP. Don't replace it with j. Alternatively, you can use app.activeDocument or app.documents[0].

BP uses its own 'namespace' to avoid clashes with vars and functions in the scripts triggered from it.

 

Peter Spier
Community Expert
Community Expert
July 18, 2022

Files must be open for a GREP search, but that can be handled in a script. I'm not a scripter, myself, but I suspect someone who is will want to know if the the Book file itself will be open already, or if that will also need to be done in the script, and if you have a specific search you want to do or if you want to be able to enter the search terms, and what to do if the pattern is matched.

nicoshAuthor
Inspiring
July 19, 2022

Ok, let's answer

- The book file it can be opened. The thing is not to open 230 files.

- I have a specific search that it can be included in the script. I don't need an interface to include the grep expresion to search.

- It has to be a find and change text content.

 

Thank you for your response

 

have a great day

 

nicoshAuthor
Inspiring
July 19, 2022

I'm sorry, i didn't understand the @Peter Kahrel answer. I realized that there is a check mark to run a script. I'll give a try.


Hi @Peter Kahrel 

Here we go again.

I run your batch processor with my script and it appears a warning -> "Undefined: undefined is not an object (108)". If I run my script without batch processing it works fine ¿any idea?

This is my code

var g = {};
g.doc = app.activeDocument;
g.cajasTexto = g.doc.stories;
g.estilosParrafo = g.doc.paragraphStyles;
g.estilosCaracter = g.doc.characterStyles;
g.fuente = "Soho Pro";
g.estiloAplicado = g.estilosParrafo.itemByName("EW_Cat_03_Info_block");
g.estiloCaracterAplicado = g.estilosCaracter.itemByName("[Ninguno]");
g.diasSemana = [['(?<=(\\s|~=|-|\\())Mon', 'lu'],
                ['(?<=(\\s|~=|-|\\())Tue', 'ma'],
                ['(?<=(\\s|~=|-|\\())Wed', 'mié'],
                ['(?<=(\\s|~=|-|\\())Thu', 'ju'],
                ['(?<=(\\s|~=|-|\\())Fri', 'vi'],
                ['(?<=(\\s|~=|-|\\())Sat', 'sá'],
                ['(?<=(\\s|~=|-|\\())Sun', 'do']];
g.meses = [['(?<=(\\s|~=|-|\\())Jan', 'ene'],
           ['(?<=(\\s|~=|-|\\())Feb', 'feb'],
           ['(?<=(\\s|~=|-|\\())Mar', 'mar'],
           ['(?<=(\\s|~=|-|\\())Apr', 'abr'],
           ['(?<=(\\s|~=|-|\\())May', 'may'],
           ['(?<=(\\s|~=|-|\\())Jun', 'jun'],
           ['(?<=(\\s|~=|-|\\())Jul', 'jul'],
           ['(?<=(\\s|~=|-|\\())Aug', 'ago'],
           ['(?<=(\\s|~=|-|\\())Sep', 'sep'],
           ['(?<=(\\s|~=|-|\\())Oct', 'oct'],
           ['(?<=(\\s|~=|-|\\())Nov', 'nov'],
           ['(?<=(\\s|~=|-|\\())Dec', 'dic']];

//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;


cambiarTodosItem(g.diasSemana);
cambiarTodosItem(g.meses);


function cambiarTodosItem(myArray_param) {
  for (i = 0; i < myArray_param.length; i++) {
    cambiarItem(myArray_param, g.estiloAplicado);
    //Clear the find/change preferences after the search.
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
  }
}

function cambiarItem(item_arr, estiloAplicado_param) {
  app.findGrepPreferences.appliedParagraphStyle = estiloAplicado_param;
  app.findGrepPreferences.appliedFont = g.fuente;
  app.findGrepPreferences.appliedCharacterStyle = g.estilosCaracter.itemByName("[Ninguno]");
  for (i = 0; i < item_arr.length; i++) {
    app.findGrepPreferences.findWhat = item_arr[i][0];
    app.changeGrepPreferences.changeTo = item_arr[i][1];
    g.doc.changeGrep();    
  }
}