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

Intercalate group of paragraphs from separated lists (text frames)

New Here ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

Both lists are separate stories.

Every line of text in its own paragraph.
Each block is already numbered.

It is possible to obtain this result?

Screenshot 2024-12-17 at 12.55.04 PM.png

 

TOPICS
Experiment , How to , Scripting , UXP Scripting

Views

160

Translate

Translate

Report

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 ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

Are the 1A,2A,1B,2B, etc. in the files or are they just for illustrative purposes?

With a script you should be able to take the 2 stories and split them by the paragraph symbol into 2 arrays. You then just need to loop through the arrays and merging the stories in order.

Votes

Translate

Translate

Report

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 ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

John,
Yes, you are correct. The numbers heading the paragraphs are guidelines so that the paragraphs of both texts have an assembly pattern.
We have this script but have not been able to perfect it:


#targetengine
"InDesign"

var myDocument = app.activeDocument;

// Replace with the actual paths to your files
var file1Path = "~/desktop/file1.txt";
var file2Path = "~/desktop/file2.txt";

// Function to read a file and return its content as an array of lines
function readFile(filePath) {
var file = new File(filePath);
file.open("r");
var lines = file.read().split("\n");
file.close();
return lines;
}

// Read the content of both files
var file1Lines = readFile(file1Path);
var file2Lines = readFile(file2Path); // Use the path here

// Function to group the lines based on their section
function groupLines(lines) {
var groupedLines = {};
var currentSection = "";
lines.forEach(line => {
if (line.match(/^\d+[AB]$/)) {
currentSection = line;
groupedLines[currentSection] = [];
} else {
groupedLines[currentSection].push(line);
}
});
return groupedLines;
}

// Group the lines from both files
var groupedFile1 = groupLines(file1Lines);
var groupedFile2 = groupLines(file2Lines);

// Create a new text frame
var myPage = myDocument.pages.item(0);
var myTextFrame = myPage.textFrames.add();

// Combine the lines from both groups, interleaving them
var combinedLines = [];
for (var section in groupedFile1) {
combinedLines.push(section);
combinedLines.push(...groupedFile1[section]);
combinedLines.push(...groupedFile2[section]);
}

// Write the combined lines to the new text frame
myTextFrame.contents = combinedLines.join("\r");

Votes

Translate

Translate

Report

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 ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

I feel like you will be missing the second file in the combining. I would think that you would need to push to the combined file: file 1 section, file 1 section body, file 2 section, file 2 section body. The section in that for loop would be just file 1's section and should not be a key for file 2. Right now your groupedFiles are objects of arrays. Personally I would have made them an array of objects. For example you have basically

groupedFile1 = {section1:["line1","line2","line3"],section2:["line1","line2","line3"]}

I think you would have an easier time indexing while combining with

groupedFile1 =[{section:"section 1",text:["line1","line2","line3"]}{section:"section 2",text:["line1","line2","line3"]}]

Techincally you could have already combined the paragraph text in this section to one string since you are already sorting it.

Votes

Translate

Translate

Report

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 ,
Dec 19, 2024 Dec 19, 2024

Copy link to clipboard

Copied

John, thanks. Still I have an error (mine).
Hope to work it out.
Let me consult you again.
best regards.

Votes

Translate

Translate

Report

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 ,
Dec 20, 2024 Dec 20, 2024

Copy link to clipboard

Copied

LATEST

Not a problem.

Good luck.

Anytime.

Votes

Translate

Translate

Report

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
Community Expert ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

@ana p379793058vpa

 

No need for a script. 

 

The tricky part would be to convert each part into a single block - cell.

 

GREP "^^d^u$" -> "#$0" should work. 

 

Then convert to a single column table with "#" as a separator.

 

Then put both columns together - convert to text with "paragraph" as row & column separator.

 

Sorry, can't check right now but it should work. 

 

Votes

Translate

Translate

Report

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
Community Expert ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

@ana p379793058vpa

 

Can you share an example file - just in case?

 

Votes

Translate

Translate

Report

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 ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

It seems to work but here requires too many cosmetic steps.
The grep mentioned ("^^d^u$") doesn't seem correct.
Perhaps ("^\d+\u$"). 

Thanks.


Screenshot 2024-12-17 at 5.47.39 PM.png

 

Votes

Translate

Translate

Report

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
Community Expert ,
Dec 17, 2024 Dec 17, 2024

Copy link to clipboard

Copied

Right, "\", "^" is for text F&C.

 

What do you mean by "cosmetic steps"? 

 

Votes

Translate

Translate

Report

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