• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Why Only (One Result) Exported! to RTF

Enthusiast ,
Nov 05, 2022 Nov 05, 2022

Copy link to clipboard

Copied

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!");

 

Best
M.Hasanain
TOPICS
Scripting

Views

73

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Nov 05, 2022 Nov 05, 2022

Hi @M.Hasanain,

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

...

Likes

Translate

Translate
Community Expert , Nov 05, 2022 Nov 05, 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

Likes

Translate

Translate
Community Expert ,
Nov 05, 2022 Nov 05, 2022

Copy link to clipboard

Copied

Hi @M.Hasanain,

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 05, 2022 Nov 05, 2022

Copy link to clipboard

Copied

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!");
Best
M.Hasanain

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2022 Nov 05, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 05, 2022 Nov 05, 2022

Copy link to clipboard

Copied

LATEST

Thanks a lot @Manan Joshi , I got it

Best
M.Hasanain

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines