Skip to main content
Known Participant
November 1, 2008
Question

looking for a script to import/export all footnotes on a MAC/CS3

  • November 1, 2008
  • 29 replies
  • 3177 views
To all you amazing scripting gurus,

CS3 on a MAC. I am faced with an awkward situation. I am given a 300+ scientific document with 400+ footnotes that they keep on changing ALL THE TIME.

i Is there a script that would import into inDesign the footnotes from another doc/txt/csv file and replace the ones to their already existing reference avoiding overflows and at the same time applying a defined paragraph style? As an extra bonus it would be even greater if one could have an option do the reverse. That is to select all the existing footnotes in an indesign document and export them to a doc/txt/csv file. This way editors can play to their heart's desire and it would be a real treat for designers.

I know it is quite a challenge since inDesign does not even have an option to select all footnotes. But I've seen what you experts can do with scripts.

Keeping my hopes high, thank you all in advance
Michael
This topic has been closed for replies.

29 replies

Known Participant
November 21, 2008
Hi Kasyan,

The latest code NO more rewrites the numbers, Excellent work.
It does however, still mess up the formatting (italics, bolds, etc) if there is any. I think it moves them according to the number of characters preceding the first formatting.
And does not like paragraphs (even if they preexist in the footnote. That is one must have one paragraph footnotes). It also meddles with kern (changes it from optical that I now use to Metrics) and adds ligatures.
Nevertheless, it fulfills my immediate needs since all of the footnotes I have are single paragraphs and there is hardly any formatting in them. The ones that have I will handle manually.
As for the N-space question. This is what I use 99% of the times (I find the space it gives quite pleasing).

I will keep on checking this post in case you want and still feel up to perfecting the script.

Thank you so much
Michael
Kasyan Servetsky
Legend
November 20, 2008


// footnotes2text script for InDesign CS3
footnotes2text();

function footnotes2text() {
if (app.documents.length == 0) {
alert("Please open a document and try again.");
exit();
}
var myDoc = app.activeDocument;

if (myDoc.viewPreferences.horizontalMeasurementUnits != MeasurementUnits.MILLIMETERS || myDoc.viewPreferences.verticalMeasurementUnits != MeasurementUnits.MILLIMETERS) {
alert("Measurement units should be set to millimiters.");
exit();
}

var myFile = new File("~/Desktop/" + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + ".rtf");
var mySel = myDoc.selection[0];
if (myDoc.selection.length == 1 && mySel.constructor.name == "TextFrame") {
var myStory = mySel.parentStory;
}
else {
alert("Select a single text frame and try again.");
exit();
}
var myFnotes = myStory.footnotes;
var myTextFrame = myDoc.textFrames.add({geometricBounds:[8, -400, 241, -5]});

for ( var j = 0; j < myFnotes.length; j++ ) {
var myFnText = myFnotes.texts[0];
myFnText.insertionPoints[0].contents = "<fnote" + myFnotes.id + ">";
myFnText.insertionPoints[-1].contents = "</fnote>\r";
myFnText.move(LocationOptions.AT_END, myTextFrame.texts[0]);
}
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(<fnote)(\\d+)(>)(\\d+)(~>)( *.*w*d*)(</fnote>)";
app.changeGrepPreferences.changeTo = "$1$2$3$5$6$7";
myTextFrame.texts[0].changeGrep();
myTextFrame.texts[0].exportFile(ExportFormat.RTF, myFile, false);
myTextFrame.remove();
alert("All done");
}
Kasyan Servetsky
Legend
November 20, 2008
Now I see the problem.
One more question:
Do you always separate footnote numbers by N-space?
Known Participant
November 20, 2008
Kasyan,

I tested the scripts and they work. They take everything out and bring everything back in to the right place.

With one glitch.

They leave some kind of a residue behind that marks the file permanently.
I don't know if it happens on "the way out" or "the way in".
The result is that if and every time you run the script on the same file, the reference number on the footnote is rewritten. So if you run the script twice, the footnote #12 becomes 1212, 4 times, 12121212.
I think it must be on the way out when you mark the area of the footnote. After the script text2footnotes runs, it should clean the marks left by footnotes2text. Otherwise it works in an additive way. Hence the result.
So I guess figuring this out would be the last obstacle.

Could you please give it another try?

Thanx again
Michael
Kasyan Servetsky
Legend
November 19, 2008
Michael,

Here is new versions of the scripts that I tested,though in a perfunctory manner, with the file you sent me today.

Kasyan


// footnotes2text script for InDesign CS3
footnotes2text();

function footnotes2text() {
if (app.documents.length == 0) {
alert("Please open a document and try again.");
exit();
}
var myDoc = app.activeDocument;

if (myDoc.viewPreferences.horizontalMeasurementUnits != MeasurementUnits.MILLIMETERS || myDoc.viewPreferences.verticalMeasurementUnits != MeasurementUnits.MILLIMETERS) {
alert("Measurement units should be set to millimiters.");
exit();
}

var myFile = new File("~/Desktop/" + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + ".rtf");
var mySel = myDoc.selection[0];
if (myDoc.selection.length == 1 && mySel.constructor.name == "TextFrame") {
var myStory = mySel.parentStory;
}
else {
alert("Select a single text frame and try again.");
exit();
}
var myFnotes = myStory.footnotes;
var myTextFrame = myDoc.textFrames.add({geometricBounds:[8, -400, 241, -5]});

for ( var j = 0; j < myFnotes.length; j++ ) {
var myFnText = myFnotes.texts[0];
myFnText.insertionPoints[0].contents = "<fnote" + myFnotes.id + ">";
myFnText.insertionPoints[-1].contents = "</fnote>\r";
myFnText.move(LocationOptions.AT_END, myTextFrame.texts[0]);
}
myTextFrame.texts[0].exportFile(ExportFormat.RTF, myFile, false);
myTextFrame.remove();
alert("All done");
}



// text2footnotes script for InDesign CS3
footnotes2text();

function footnotes2text() {
if (app.documents.length == 0) {
alert("Please open a document and try again.");
exit();
}
var myDoc = app.activeDocument;

if (myDoc.viewPreferences.horizontalMeasurementUnits != MeasurementUnits.MILLIMETERS || myDoc.viewPreferences.verticalMeasurementUnits != MeasurementUnits.MILLIMETERS) {
alert("Measurement units should be set to millimiters.");
exit();
}

var mySel = myDoc.selection[0];
if (myDoc.selection.length == 1 && mySel.constructor.name == "TextFrame") {
var myStory = mySel.parentStory;
var myFnotes = myStory.footnotes;
}
else {
alert("Select a single text frame and try again.");
exit();
}
var myFile = new File("~/Desktop/" + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + ".rtf");
if (myFile.exists) {
var myTextFrame = myDoc.textFrames.add({geometricBounds:[8, -400, 241, -5]});
myTextFrame.place(myFile, false);
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(<fnote)(\\d+)(>)( *.*w*d*)(</fnote>)";
app.changeGrepPreferences.changeTo = "$4";

var myFinds = myTextFrame.findGrep();
for ( var j = myFinds.length-1; j >= 0; j-- ) {
var myFnId = parseInt(myFinds.contents.substr(6, 4));
var myFootNote = myFnotes.itemByID(myFnId);
if (myFootNote != null) {
var myText = myFinds.changeGrep();
myFnotes.texts[0].insertionPoints[0].contents = "" + (j + 1);
myText[0].move(LocationOptions.AT_END, myFnotes.texts[0]);
}
}
}
else {
alert("Sourse file does not exist.");
exit();
}
myTextFrame.remove();
alert("All done");
}
Known Participant
November 18, 2008
Hi Kasyan,

I ran the script but still get errors.

Running the footnotes2text stops half the way (on different files in different footnotes.)

Running the text2footnotes produces an error.

Michael
Kasyan Servetsky
Legend
November 18, 2008
Hi all,

I made another version of the footnote script that doesn’t scramble formatting (at least, I hope that it doesn’t). Here I applied a different approach:
First select the text frame, you want to export footnotes from, with black arrow.
Run the footnotes2text script.
Text of each footnote is moved into a temporary frame created on the pasteboard. Then the text of this frame is exported in RTF format to the desktop. The file has the same name as InDesign document. Open the file in Word and edit.
Don’t remove tabs at start of the paragraphs.
Don’t add/remove paragraphs — the number of paragraphs in a footnote before and after editing should be the same.

When you finished editing, save and close the file.

In InDesign, select the frame where the footnotes used to be, and run text2footnotes script.

Run these scripts on a copy of your document or save it before running the scripts, so that you could revert the document to its original state.

Kasyan


// footnotes2text script for InDesign CS3
footnotes2text();

function footnotes2text() {
if (app.documents.length == 0) {
alert("Please open a document and try again.");
exit();
}
var myDoc = app.activeDocument;
if (myDoc.viewPreferences.horizontalMeasurementUnits != MeasurementUnits.PICAS || myDoc.viewPreferences.verticalMeasurementUnits != MeasurementUnits.PICAS) {
alert("Measurement units should be set to picas.");
exit();
}
var myFile = new File("~/Desktop/" + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + ".rtf");
var mySel = myDoc.selection[0];
if (myDoc.selection.length == 1 && mySel.constructor.name == "TextFrame") {
var myStory = mySel.parentStory;
}
else {
alert("Select a single text frame and try again.");
exit();
}
var myFnotes = myStory.footnotes;
var myParNum = new Array(myFnotes.length);
var myTextFrame = myDoc.textFrames.add({geometricBounds:[1.9, -15.6, 57, -1.2]});

for ( var j = 0; j < myFnotes.length; j++ ) {
myParNum = myFnotes.texts[0].paragraphs.length;
var myFnText = myFnotes.texts[0].paragraphs.everyItem();
myFnText.move(LocationOptions.AT_END, myTextFrame.texts[0]);
myTextFrame.insertionPoints[-1]. contents = "\r";
}
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "^F";
app.changeTextPreferences.changeTo = "";
myTextFrame.texts[0].changeText();
myTextFrame.texts[0].exportFile(ExportFormat.RTF, myFile, false);
myTextFrame.remove();
myDoc.insertLabel("footnotes", myParNum.join());
alert("All done");
}



// text2footnotes script for InDesign CS3
text2footnotes();

function text2footnotes() {
if (app.documents.length == 0) {
alert("Please open a document and try again.");
exit();
}
var myDoc = app.activeDocument;
if (myDoc.viewPreferences.horizontalMeasurementUnits != MeasurementUnits.PICAS || myDoc.viewPreferences.verticalMeasurementUnits != MeasurementUnits.PICAS) {
alert("Measurement units should be set to picas.");
exit();
}
var mySel = myDoc.selection[0];
if (myDoc.selection.length == 1 && mySel.constructor.name == "TextFrame") {
var myStory = mySel.parentStory;
var myFnotes = myStory.footnotes;
}
else {
alert("Select a single text frame and try again.");
exit();
}
var myFile = new File("~/Desktop/" + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + ".rtf");
if (myFile.exists) {
var myTextFrame = myDoc.textFrames.add({geometricBounds:[1.9, -15.6, 57, -1.2]});
myTextFrame.place(myFile, false);
var myFnotes = myStory.footnotes;
var myParNum = myDoc.extractLabel("footnotes").split(",");
for ( var j = myFnotes.length-1; j >= 0; j-- ) {
var myText = myTextFrame.texts[0].paragraphs.itemByRange(myTextFrame.texts[0].paragraphs.length-(myParNum), myTextFrame.texts[0].paragraphs.length-1); // [j-1];
myFnotes.texts[0].insertionPoints[0].contents = "" + (j + 1);
myText.move(LocationOptions.AT_END, myFnotes.texts[0]);
try {
if (myFnotes.texts[0].paragraphs.length > 1) {
myTextFrame.texts[0].paragraphs[-1].characters[-1].remove();
}
else if (myTextFrame.texts[0].paragraphs[-1].characters[-1].contents == "\r") {
myTextFrame.texts[0].paragraphs[-1].characters[-1].remove();
}
}
catch (e) {}
}
}
else {
alert("Sourse file does not exist.");
exit();
}
myTextFrame.remove();
alert("All done");
}
Kasyan Servetsky
Legend
November 11, 2008
I’ve already thought about this approach — demonstrated by ExportHTML.jsx example script. I’ll give it a try when time permits and if I attain any success I’ll let you know about it.

>Could an xml file be an option?

I’m afraid it would be very hard for the editor to read and edit the text in an XML-editing application.
November 11, 2008
I haven't been tuned into the complete conversation and the machinations of Kasyan's script (what a nice person Kasyan is). I'm wondering if the process could be served by converting italics, bold and other formatting to simple text tags before exporting? For example,
i italic text
converted to <i>italic text<\i>. There would need to be consideration of the fact there could be superscripts, subscripts and other formatting like underline, but it is a finite list.

I guess the bigger problem is that people can sometimes complain about working with these codes in the text. Given that money is a constraint as you mentioned earlier---and InCopy is certainly the best way to deal with the issue overall---I think that you could argue they just have to live with it or cough up the money for InCopy.

The conversion of formatting to codes is relatively simple and has been discussed before (there is even an example in the scripting guides supplied by Adobe). The search would need to be constrained to the paragraph style applied to the footnotes, and this is simple (you do have a paragraph style applied to the footnotes, don't you... of course you do). The rest of Kasyan's script would work well then. Do the conversion before the script start and reverse the conversion at the end and you have your solution, including formatting.

Like I said, I haven't been fully tuned into this conversation, but Kasyan did a great job helping with this problem that I think it would be shame not to take advantage of this work. With a small amount of extra time and code it could be expanded to work with text formatting, too.
Known Participant
November 11, 2008
Kasyan,

I tried the RTF trick but for some reason when reimported it messes up the formatting of most of the text. I think this is a dead end. Could an xml file be an option?
So for the time being I stick with your script hoping that you find a fix for it.

Waiting (if you still have the time) for the "magic" fix.

Michael