Skip to main content
MacPhobic
Inspiring
November 7, 2023
Answered

Adobe InDesign - Export to RTF Script

  • November 7, 2023
  • 1 reply
  • 2022 views

Hello InDesign-ers!

Its a long shot, but checking to see if someone can maybe help. We have somewhat of an old jsx script for InDesign someone found awhile back (2016) called ExportStoriesToRTF.jsx. Works on the Macs, but in Windows its been hit or miss. We can't for the life of us figure out why it works on some workstations and not others. And all versions of ID are the same. Settings too. Not sure if we actually need Java installed, but version of Java is the same as well. We get a crazy pop when we execute the script from the Scripts panel which I've attached. Anyone ever use this script? Any experience you can share?

 

Correct answer m1b

Thankyou, that's what we need.

 

The problem is probably that the script has been wrecked at some point or someone copy/pasted from another script. Line 122 where the error occurs uses the (undeclared variable "i") and compares it to the number of stories in the document. This serves no purpose (even it it worked—which it doesn't). Try removing it (and the balancing brace on line 126, so you have this:

//If the imported text did not end with a return, enter a return
if (myNewStory.characters.item(-1).contents != "\r") {
    myNewStory.insertionPoints.item(-1).contents = "\r";
}

Also the declaration line 38 should be:

var PBYN = false;

 See how that goes.

- Mark

1 reply

m1b
Community Expert
Community Expert
November 7, 2023

Hi @MacPhobic, we would have to see the rest of the script to understand why i is undefined. Are you able to share? Or at least the flow up to that point?

- Mark

MacPhobic
MacPhobicAuthor
Inspiring
November 7, 2023

@m1b You bet! Sorry, i wasn't sure if i could post it or not. Here it is:

 

main();
function main(){
	//Make certain that user interaction (display of dialogs, etc.) is turned on.
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
	if(app.documents.length != 0){
		if (app.activeDocument.stories.length != 0){
			myGetFileName(app.documents.item(0).name); 
		}
		else{
			alert("The document does not contain any text. Please open a document containing text and try again.");
		}
	}
	else{
		alert("No documents are open. Please open a document and try again.");
	}
}

function myGetFileName(myDocumentName){ 
     var myFilePath = File.saveDialog("Save Exported File As:"); 
     if(myFilePath != null){ 
          myExportAllText(myDocumentName, myFilePath); 
     } 
}

function myExportAllText(myDocumentName, myFilePath){ 
     app.scriptPreferences.userInteractionLevel=UserInteractionLevels.NEVER_INTERACT; // this line will detect all the interaction like missing fonts missing links.  

     var myPage, myStory;
     var myExportedStories = [];
     var myTempFolder = Folder.temp; 
     var myTempFile = File(myTempFolder + "/tempTextFile.rtf"); 
     var myNewDocument = app.documents.add(); 
     var myDocument = app.documents.item(myDocumentName); 
     var myTextFrame = myNewDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myNewDocument, myNewDocument.pages.item(0))}); 
     var myNewStory = myTextFrame.parentStory; 
     var minLength = 0;
     var fullLength;
     var PBNY = false;
     
     var iDial = new Window('dialog', 'Options');  
      
      var panel1 = iDial.add('panel', undefined, 'Ignore stories with fewer than this number of characters:');  
      var IgnoreChars= panel1.add('edittext', undefined, '30');
      IgnoreChars.minimumSize.height = 25;  
      IgnoreChars.minimumSize.width = 200;     
      var IncludePasteboard = panel1.add('checkbox', undefined, "Include text boxes from paste board.", {name:'paste'});
      var group = iDial.add('group', undefined, 'Group title');  
        var OKBtn = panel1.add('button', undefined, 'OK', {name:'close'});  
        OKBtn.onClick = function(){  
            minLength = IgnoreChars.text;
            PBYN = IncludePasteboard.value;
            iDial.hide();  
          } 
      
      iDial.show();    
    
      if (PBYN) { 
            var myTextFrames =  myDocument.pageItems.everyItem().getElements();
            for (var i = 0; i < myTextFrames.length; i++ ) {
                if ( myTextFrames[i] == "[object TextFrame]" ) { 
                    myStory = myTextFrames[i].parentStory;
                    exportStory(myStory, myExportedStories, minLength, myTempFile, myNewStory, myDocument);
                }
           } 
      }
      else { 
           for (var i = 0; i < myDocument.pages.length; i++) {
                myPage = myDocument.pages.item(i);
                for (var t = 0; t < myPage.textFrames.length; t++){
                    myStory = myPage.textFrames[t].parentStory;
                    exportStory(myStory, myExportedStories, minLength, myTempFile, myNewStory, myDocument);
                }
            }
      }    

     myExtension = ".rtf" 
     myNewStory.exportFile(ExportFormat.RTF, File(myFilePath + myExtension)); 
     myNewDocument.close(SaveOptions.no); 
     myTempFile.remove(); 
     app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;
     
    alert("RTF file has been created.");
    
}

function myGetBounds(myDocument, myPage){ 
     var myPageWidth = myDocument.documentPreferences.pageWidth; 
     var myPageHeight = myDocument.documentPreferences.pageHeight 
     if(myPage.side == PageSideOptions.leftHand){ 
          var myX2 = myPage.marginPreferences.left; 
          var myX1 = myPage.marginPreferences.right; 
     } 
     else{ 
          var myX1 = myPage.marginPreferences.left; 
          var myX2 = myPage.marginPreferences.right; 
     } 
     var myY1 = myPage.marginPreferences.top; 
     var myX2 = myPageWidth - myX2; 
     var myY2 = myPageHeight - myPage.marginPreferences.bottom; 
     return [myY1, myX1, myY2, myX2]; 
}

function IsInArray(myString, myArray) {
     for (x in myArray) {
          if (myString == myArray[x]) {
               return true;
          }
     }
     return false;
}


function exportStory(myStory, myExportedStories, minLength, myTempFile, myNewStory, myDocument){
    fullLength = myStory.length;
    if (!IsInArray(myStory.id, myExportedStories) && fullLength > minLength ) {
        //Export the story as rff. 
        myStory.exportFile(ExportFormat.RTF, myTempFile);
        myExportedStories.push(myStory.id);
        //Import (place) the file at the end of the temporary story.                     
        myNewStory.insertionPoints.item(-1).place(myTempFile); 
        //If the imported text did not end with a return, enter a return 
        if(i != myDocument.stories.length -1){ 
            if(myNewStory.characters.item(-1).contents != "\r"){ 
                myNewStory.insertionPoints.item(-1).contents = "\r"; 
            } 
        }
    }
}
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 7, 2023

Thankyou, that's what we need.

 

The problem is probably that the script has been wrecked at some point or someone copy/pasted from another script. Line 122 where the error occurs uses the (undeclared variable "i") and compares it to the number of stories in the document. This serves no purpose (even it it worked—which it doesn't). Try removing it (and the balancing brace on line 126, so you have this:

//If the imported text did not end with a return, enter a return
if (myNewStory.characters.item(-1).contents != "\r") {
    myNewStory.insertionPoints.item(-1).contents = "\r";
}

Also the declaration line 38 should be:

var PBYN = false;

 See how that goes.

- Mark