Copy link to clipboard
Copied
I'm doing a data merge for a large catalog, and I'm placing images with the "special" image column (i.e., @image1, image@inbox.ru, ...) - all works well.
However the text for each page is actually in a text file, and contains line breaks, tabs, etc. Is there an equivalent to the special image tag - or a scripted solution - to automatically place text from a text file as specified in each row of the data file?
My data file is something like this:
productName | @productImage | description |
---|---|---|
Apples | /path/to/image/apples.tif | /path/to/textfile/description/apples.txt |
Oranges | /path/to/image/orange.tif | /path/to/textfile/description/orange.txt |
1 Correct answer
Here's a quick-and-dirty script using various bits I found on these forums plus some of my own work. The script takes advantage of Script Labels to flag all text frames in a document where text import from file is desired. It also applies a style of your choosing to all paragraphs in the text frame after the file text is imported.
Use at your own peril, of course - no warranty implied:
- Add a Script Label to the targeted text frame on your page master.
- Do your data merge as normal, including the ful
Copy link to clipboard
Copied
Hi,
I could not think of a way to do this.
I suggest you use Data merge with this file normally. This will add description field as normal text into the text frames. You can then use InDesign script to scan through all frames and then place the contents of filename.txt in the frame that has the text filename.txt in it.
I hope that helps.
-Aman
Copy link to clipboard
Copied
Here's a quick-and-dirty script using various bits I found on these forums plus some of my own work. The script takes advantage of Script Labels to flag all text frames in a document where text import from file is desired. It also applies a style of your choosing to all paragraphs in the text frame after the file text is imported.
Use at your own peril, of course - no warranty implied:
- Add a Script Label to the targeted text frame on your page master.
- Do your data merge as normal, including the full path to your text file merged into that labelled text frame. IMPORTANT: put no other content in that text frame!!!
- Run this script:
var myLabel = "SomeScriptLabel"; // Script label of frames you want to process (see Step 1)
var myStyle = "SomeParagraphStyle"; // Name of style you want applied
var document = app.documents.item(0);
var allTextFrames = toArray(document.textFrames);
var textFrames = selectWhere(myLabel, "label", allTextFrames);
var i = textFrames.length;
var attemptCounter = 0;
var successCounter = 0;
while (i--) {
if (textFrames.parentPage.appliedMaster) { // ignores master pages, as "appliedMaster" property will be null
attemptCounter++;
var myStory = textFrames.parentStory;
var newText = getFileContents(myStory.contents);
if (newText !== false) {
newText = newText.replace(/\n/g,"\r"); // OPTIONAL: replaces soft line breaks with "real" line breaks
myStory.contents = newText;
updateStyles(myStory);
successCounter++;
}
}
}
alert(successCounter + " of " + attemptCounter + " text frames updated");
function selectWhere(value, key, array){
var i = array.length; var t; var filtered = [];
while(i--){
t = array;
if(t && t[key] == value){
filtered.push(t);
}
}
return filtered;
}
function toArray(objects){
var i = objects.length; var array = [];
while(i--){
array.push(objects);
}
return array;
}
function getFileContents(filePath) {
filePath = filePath.split("\r");
var myTextFile = File(filePath[0]); // use only first line of frame contents as path
if (myTextFile.exists) {
myTextFile.open('r', undefined, undefined);
if (myTextFile !== '' && myTextFile !== null) {
var thisFileText = myTextFile.read();
myTextFile.close();
return thisFileText;
}
}
return false;
}
function updateStyles(myStory) {
for (var i=0; i < myStory.paragraphs.length; i++) {
var myParagraph = myStory.paragraphs;
myParagraph.appliedParagraphStyle = myStyle;
}
}
Copy link to clipboard
Copied
This appears to me to be the only way to go about it - thanks for the comment. If I'm successful, I'll post the script when I'm done.
I'm thinking I'll go about it this way:
1. Merge and place the path to the file in each text frame with a "placeholder" style applied (e.g., a style called "Replace Me").
2. Post-merge with JSX, find all text frames in the document with the "Replace Me" style on the first paragraph, and use the contents of the text frame (which would be the full path to the text file) to open the file and replace all text in the frame.
3. Finally, re-style the text as needed using JSX.
Sure seems like an oversight to me that you can't merge text from external files (you can only place images) but I guess if many people needed to do things this way the capability would already exist, right?
Copy link to clipboard
Copied
I know it's been years, but I'm using InDesign C6 and am wondering if you were successful, and if so could you post the the script you used?

