While working on a book in InDesign recently, I needed to switch the position of two paragraphs throughout the 400-page book.
There were 2 paragraph styles involved: A “Quote” style, and a “Source” style. In the book as I had received it, the source was always listed before the quote. But a decision was made that the quote should come first.
InDesign offers no convenient way of performing this operation. I stress “convenient” here, because perhaps it could be done with a GREP search (but even if it could be, I suspect that any footnotes would be messed up in the process).
So I wrote the following script. Here are the instructions:
Place your text cursor in the first paragraph and run the script. In my case, I place the cursor in a paragraph to which the “Source” paragraph style has been applied. Following the “Source” style comes text styled with the “Quote” paragraph style.
The script alerts the user as to what is about to happen. In my case, this means that anywhere in the document where “Source” is followed by “Quote”, the “Quote” paragraph will be moved before the “Source” paragraph.
Click OK, and it is done!
As usual for these quick scripts that I write for my own convenience and share, there is no error checking. There is no 1-step undo (though that could be easily added – check out some other scripts on the blog for the way to do this). And there are no scope options, meaning that the script will always work on the entire document.
Here’s the script. To use, copy to a text editor, save with a .jsx extension, and place in your InDesign Scripts Panel folder.
// BS"D // Transpose Paragraphs // An InDesign script Copyright (c) Bookraft LLC, 2017 // Another freebie script from www.Id-Extras.com // This code is released under the GNU Public License (GPL) // This script has not been thoroughly tested. Use at your own risk only! d = app.activeDocument; style1 = app.selection[0].paragraphs[-1].appliedParagraphStyle; style2 = app.selection[0].paragraphs[-1].insertionPoints[-1].appliedParagraphStyle; confirmed = confirm("If a paragraph with style '" + style2.name + "' comes after a paragraph with style '" + style1.name + "' they will be transposed. Click YES to continue, NO to exit."); if (confirmed === false) exit(); app.findTextPreferences = null; app.findTextPreferences.appliedParagraphStyle = style1; myFinds = d.findText(true); for (i = 0; i < myFinds.length; i++){ f = myFinds[i]; p1 = f.paragraphs[-1]; p2 = f.insertionPoints[-1].paragraphs[0]; if (p1.appliedParagraphStyle === style1 && p2.appliedParagraphStyle === style2){ p2.move(LocationOptions.BEFORE, p1.insertionPoints[0]); } }
Igor Freiberger
December 27, 2016 11:12 pmAnother amazing script. Thank you very much!