Skip to main content
M.Hasanin
Inspiring
November 5, 2022
Answered

Why Only (One Result) Exported! to RTF

  • November 5, 2022
  • 1 reply
  • 654 views

Hi Experts, I just wonder why only One GREP Results is Exported to RTF Text, this is working good with (*.txt) file with no problems but when exporting to rtf it only export one result! , please help and thanks in advance , here is the code :

//Collect all grep search Results
try {
    var myDoc = app.activeDocument;
    } catch(e) { exit(); }
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = "\\u(\\u|\\.)+";
    
    //File Name and Path
    var myFileName = app.activeDocument.name.replace (/\.indd$/, ''); //Document Name will taken
    myExtension = ".rtf"
    myFileName = myFileName + myExtension;
    folderPath = "~/Desktop"
    var myRTF = folderPath+"/"+myFileName;

    var myGrepFoundText = new Array;
    founds = myDoc.findGrep(true);
    for (var i = 0; i < founds.length; i++) {
        //Add and Sort
        myGrepFoundText.push(founds[i].contents + "\t" + founds[i].parentTextFrames[0].parentPage.name);
        myGrepFoundText.sort();
        //Font Specs
        myGrepFoundText.appliedFont = 'Arial';
        myGrepFoundText.fontStyle = 'Regular';
        myGrepFoundText.pointSize  = 24;
        //Join the Array!
        myStrings = myGrepFoundText.join("\r")
        i++
        //Export to File
        myStrings.exportFile(ExportFormat.RTF, File(myRTF));
    }
    //Try To Open Exported RTF in Associated App
    var cFile= new File(myRTF);
         if(cFile.exists){
           cFile.execute();
             }else{
                alert("File Not Found!", "Error!");  
    }
    //END
    alert("Done!");

 

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

myGrepResults is a string and string does not have the exportFile method and that is why you get the error. What you need to do is use File object, open a file with write permission at the required path and write the string into that file and close it.

-Manan

1 reply

Community Expert
November 5, 2022

Hi @M.Hasanin,

Only one rtf file is created because you overwrite the multiple files you create. If you see you have the filepath created in the variable myRTF then inside the for loop you keep on writing to this file for every iteration so one file is written then the other file overwrites it and so on.

On the other hand there are other issues in your loop as well, you push the result in an array and immediately sort it, this mean for a loop that runs 5 times you call sort function 5 times, a better approach could be to sort once the array is filled i.e. after the loop ends and thus called only once. Now if your goal is to export multiple files then you should just export the files(create unique name) in each iteration, no need to save it in an array or sort it.

Ideally you should create just one rtf file with all the results. To do that move the file export code as well as the array sort code outside the for loop and write the content of the array after sorting and join into the file.

I hope this gives you enough to relook at your logic and figure out the difference in your thinking and the code.

-Manan

-Manan
M.Hasanin
M.HasaninAuthor
Inspiring
November 5, 2022

Hi @Manan Joshi  and thank you, i was stupid!, not enough coffee today ! 🙂

i modified the code as you recommended but it strange through error (myGrepresult.exportFile) is not a function :

here is the modified logic :

//Collect all grep search Results
try {
    var myDoc = app.activeDocument;
    } catch(e) { exit(); }
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = "\\u(\\u|\\.)+";
    
    //File Name and Path
    var myFileName = app.activeDocument.name.replace (/\.indd$/, ''); //Document Name will taken
    var myExtension = ".rtf"
    var myNamewExt = myFileName + myExtension;
    var folderPath = "~/Desktop"
    var myRTF = folderPath+"/"+myNamewExt;
    //
    var myGrepFoundText = new Array;
    founds = myDoc.findGrep(true);
    //
    for (var i = 0; i < founds.length; i++) {   
        //Add to Array
        myGrepFoundText.push(founds[i].contents + "\t" + founds[i].parentTextFrames[0].parentPage.name);
            }
        myGrepFoundText.sort();
        var myGrepResults = myGrepFoundText.join("\r")
        myGrepResults.exportFile(ExportFormat.RTF, File(myRTF));
        
        //Try To Open Exported RTF in Associated App
        var cFile= new File(myRTF);
             if(cFile.exists){
               cFile.execute();
                 }else{
                    alert("File Not Found!", "Error!");  
        }
    //END
    alert("Done!");
Mohammad Hasanin
Manan JoshiCommunity ExpertCorrect answer
Community Expert
November 5, 2022

myGrepResults is a string and string does not have the exportFile method and that is why you get the error. What you need to do is use File object, open a file with write permission at the required path and write the string into that file and close it.

-Manan

-Manan