Skip to main content
New Participant
July 16, 2022
Answered

Reorder paragraph(s) in story by paragraph style -script?

  • July 16, 2022
  • 5 replies
  • 785 views

We have a 3rd party system that imports data into InDesign story boxes to create product catalogs using XML. The process is XML, but the final text in InDesign is just text with paragraph and character styles. There are no XML tags or structure.

 

I'm coming across an issue where I need to reorder the data, but any changes to the way the 3rd party system imports data requires lots of money for dev work and of course the time to add to a sprint, test, and deploy.

 

Has anyone come across a similar issue or have any ideas where to look for a solution or start a script? My first thought was to apply the XML tags, export the XML from InDesign and then reimport using an XSLT, but that could be clunky. Ideally I'd like to place the cursor in a story and run a script that looks for the blocks of data by paragraph and reorders it. 

 

Ex data named by paragraph style

Original order:

 

Name

Item Number

Details

Size

Price

Name Chinese

Details Chinese

Size Chinese

 

 

New order

Item Number

Name

Name Chinese

Details

Details Chinese

Size

Size Chinese

Price

 

This topic has been closed for replies.
Correct answer Robert Kyle

We use third-party software to export text from a database to our print publication system. A basic story will include a Summary, a Byline and numerous paragraphs of Body. The database stores those parts in separate records but combines them in a single text file with each paragraph tagged for its paragraph style. The catch is that the programmers decreed that the Byline always comes first, followed by the Summary. We want it the other way around. So here's a snippet of the script we use:

 

 

var myDoc = app.activeDocument;
var myStory = app.selection[0].parentStory;var sumStyle = myDoc.paragraphStyles.item("Summary_Small");
app.findGrepPreferences.appliedParagraphStyle = sumStyle;
app.findGrepPreferences.findWhat = NothingEnum.NOTHING;
var mySummary = myStory.findGrep();
if (mySummary.length > 0) {
	if (mySummary.length === 1) {
		if (mySummary[0].index != 0) { 
			mySummary[0].move(LocationOptions.AT_BEGINNING);
		}
	} else { // more than one summary found
		for (var s = mySummary.length-1; s > -1; s--) {
			if (mySummary[s].index > 0) { 
				mySummary[s].move(LocationOptions.AT_BEGINNING); 
			}

 

 

The only trick to is that if there is more than one paragraph using the Summary, the loop starts at the end of the array so that the proper order is maintained. That only happens when an Adobe user tries to "fix" the article manually before running the script.

 

You have a more complicated task, but I think you could use the same logic to restack things in the proper order.

 

Hope this helps, 

 

Robert

5 replies

Marc Autret
Legend
July 26, 2022

Hi @therealrustopher 

 

A funny way to reach your goal is to 'hack' the TextSortParagraphs snippet:

https://github.com/indiscripts/IdGoodies/blob/master/snip/TextSortParagraphs.jsx

which is now easily do-able using a custom extraOrderFunc parameter.

 

I've implemented your example (that is, your custom paragraph style ordering) in the tests directory:

https://github.com/indiscripts/IdGoodies/blob/master/tests/CustomTextSortParagraphs.jsx

 

Observe how out-of-scope paragraphs keep in place around the blocks.

 

The paragraph style names and ordering are hard-coded in the myCustomOrder function, as follows,

 

so it's pretty simple to adjust this code to other projects/users.

 

This routine should be much faster than GREP-based implementations. The script is undo-able.

 

Best,

Marc

Robert KyleCorrect answer
Inspiring
July 20, 2022

We use third-party software to export text from a database to our print publication system. A basic story will include a Summary, a Byline and numerous paragraphs of Body. The database stores those parts in separate records but combines them in a single text file with each paragraph tagged for its paragraph style. The catch is that the programmers decreed that the Byline always comes first, followed by the Summary. We want it the other way around. So here's a snippet of the script we use:

 

 

var myDoc = app.activeDocument;
var myStory = app.selection[0].parentStory;var sumStyle = myDoc.paragraphStyles.item("Summary_Small");
app.findGrepPreferences.appliedParagraphStyle = sumStyle;
app.findGrepPreferences.findWhat = NothingEnum.NOTHING;
var mySummary = myStory.findGrep();
if (mySummary.length > 0) {
	if (mySummary.length === 1) {
		if (mySummary[0].index != 0) { 
			mySummary[0].move(LocationOptions.AT_BEGINNING);
		}
	} else { // more than one summary found
		for (var s = mySummary.length-1; s > -1; s--) {
			if (mySummary[s].index > 0) { 
				mySummary[s].move(LocationOptions.AT_BEGINNING); 
			}

 

 

The only trick to is that if there is more than one paragraph using the Summary, the loop starts at the end of the array so that the proper order is maintained. That only happens when an Adobe user tries to "fix" the article manually before running the script.

 

You have a more complicated task, but I think you could use the same logic to restack things in the proper order.

 

Hope this helps, 

 

Robert

New Participant
July 25, 2022

This is a perfect example and I think I can use it as a starting point. Thanks for your help!

Community Expert
July 18, 2022

Assuming that the items you listed are names of paragraph styles, prefix the style names with numbers,

 

1 Item Number

2 Name

etc.

 

Then you can do a script that sorts these blocks by their applied paragraph style. I'm not aware that such a script exists. Maybe the solution that Brian mentioned works, (I doubt it) but you could commission someone to do it for you.

 

P.

 

brian_p_dts
Community Expert
July 17, 2022

In the Sample Script folder, there is a script called SortParagraphs.jsx. It currently alpha sorts the grafs; it could be modified, theoretically, to sort by Paragraph Style type, but it is not a trivial matter, especially if you need to resort chunks of directory listings. You could also look at solutions like EasyCatalog, which may have that feature, but there is cost involved. 

m1b
Community Expert
July 16, 2022

Hi @therealrustopher, to help us understand, can you show us a before and after? Is this what you want: that each paragraph in a text frame be sorted according to a preset list of paragraph style names?