Skip to main content
A2D2
Inspiring
April 5, 2019
Answered

Automatic pasting of text (sequential strings) from data file

  • April 5, 2019
  • 1 reply
  • 3293 views

Hello,

I am working on a book length InDesign CS6 document, the text of which looks something like this:

Lorem ipsum dolor sit amet [$£ 2%"£33000%$^6$% $£], consectetur adipiscing elit. Suspendisse pulvinar id ante at laoreet. Mauris ultrices, odio vel suscipit pulvinar, tellus turpis faucibus sem, eget venenatis nisi nisl quis ligula. Integer volutpat eget tortor id pulvinar. Duis nec lacus eget quam aliquam pharetra at non lorem. Pellentesque lobortis, quam finibus tempus pulvinar [^& %4^^ $%5£$"££], ipsum orci luctus lectus, in sollicitudin lectus tortor eget mauris. Suspendisse porttitor, nisi eu venenatis ullamcorper, eros tellus sollicitudin leo, non porttitor odio mauris eu lorem. Phasellus maximus vitae tellus vel ullamcorper [£8$£% £$%£$ %£$]. Phasellus congue porttitor elit, nec tincidunt orci eleifend a. Quisque auctor sodales turpis non ultricies. Integer sed arcu dolor. Nulla et euismod erat, vitae viverra nisl. Fusce interdum, urna id convallis imperdiet, libero urna vestibulum sem, eget consequat lorem leo id nulla. Cras euismod euismod nisl ut interdum [£$%8$^9 &*(*"$ $£%].

.

.

.

I have a plain text data file which looks something like this:

first phrase on line/para 1

second phrase on line/para 2

third phrase on line/para 3

fourth phrase on line/para 4

.

.

.

I want each text string to be sequentially pasted into each square bracketed string in the InDesign document so it looks something like this:

Lorem ipsum dolor sit amet [first phrase on line/para 1], consectetur adipiscing elit. Suspendisse pulvinar id ante at laoreet. Mauris ultrices, odio vel suscipit pulvinar, tellus turpis faucibus sem, eget venenatis nisi nisl quis ligula. Integer volutpat eget tortor id pulvinar. Duis nec lacus eget quam aliquam pharetra at non lorem. Pellentesque lobortis, quam finibus tempus pulvinar [second phrase on line/para 2], ipsum orci luctus lectus, in sollicitudin lectus tortor eget mauris. Suspendisse porttitor, nisi eu venenatis ullamcorper, eros tellus sollicitudin leo, non porttitor odio mauris eu lorem. Phasellus maximus vitae tellus vel ullamcorper [third phrase on line/para 3]. Phasellus congue porttitor elit, nec tincidunt orci eleifend a. Quisque auctor sodales turpis non ultricies. Integer sed arcu dolor. Nulla et euismod erat, vitae viverra nisl. Fusce interdum, urna id convallis imperdiet, libero urna vestibulum sem, eget consequat lorem leo id nulla. Cras euismod euismod nisl ut interdum [fourth phrase on line/para 4].

.

.

.

Any ideas how I could do this?

The bold is just for the purposes of demonstration, what is distinguishing each phrase in the document is that it is found between square brackets. The square brackets do not need replacing, just the gibberish text with the text strings from the data file.

Thank you!

This topic has been closed for replies.
Correct answer Manan Joshi

In that case try the following script

var fi = new File("/Users/manan/Desktop/abc.txt")     //Change to the path of your input file

if(!fi.exists)

{

     alert("Input file does not exist")

     exit()

}

fi.open("r");

var data = fi.read() .split('\n')

app.findGrepPreferences.findWhat = "\\[.*?\\]"

var res = app.documents[0].findGrep(true)

data = data.slice(0, res.length)

while(a = res.shift())

{

     a.contents = "[" + data.pop() + "]"

}

app.findGrepPreferences = NothingEnum.nothing;

-Manan

1 reply

Community Expert
April 5, 2019

You could investigate using DataMerge for your purpose. If you could alter your input file to have multiple columns and a single row instead of a single column you could use Data Merge. Have a look at the following link for more details on how Data Merge works

Merge data to create form letters, envelopes, or mailing labels in Adobe InDesign

Let us know if this will work for your case.

-Manan

-Manan
A2D2
A2D2Author
Inspiring
April 5, 2019

OK, but is there a way to enter the fields automatically?

E.g. find first "\[.\]" and insert 1st field

find second "\[.\]" and insert 2nd field

etc.

I have hundreds of these square bracketed strings to find.

I get the idea that if each square bracketed string could be mapped to one field then Data Merge could be used, but if this is to be done manually then I might as well copy and paste each line from the data file.

Manan JoshiCommunity ExpertCorrect answer
Community Expert
April 5, 2019

In that case try the following script

var fi = new File("/Users/manan/Desktop/abc.txt")     //Change to the path of your input file

if(!fi.exists)

{

     alert("Input file does not exist")

     exit()

}

fi.open("r");

var data = fi.read() .split('\n')

app.findGrepPreferences.findWhat = "\\[.*?\\]"

var res = app.documents[0].findGrep(true)

data = data.slice(0, res.length)

while(a = res.shift())

{

     a.contents = "[" + data.pop() + "]"

}

app.findGrepPreferences = NothingEnum.nothing;

-Manan

-Manan