Copy link to clipboard
Copied
Dear Experts,
Im trying to export text titles based on specific paragraph style to text File, but the script only export One title and continue Looping! (endless!) until manual break! , please help me to figure the problem! :
//Export Text Based on Paragraph Style
var myDoc = app.activeDocument;
var fileName = app.activeDocument.name.replace (/\.indd$/, '');
myExtension = ".txt"
fileName = fileName + myExtension;
folderSelect = Folder.selectDialog("Please select a Folder to Save Text File");
f = File(folderSelect+"/"+fileName);
f.encoding = "UTF-8";
f.open("w");
var PsData = "";
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
found = app.activeDocument.findGrep(true);
for (i = 0; i < found.length; i++) {
if (found[i].texts[0].appliedParagraphStyle.name == "EN Bold Title_1") {
found[i].showText();
PsData = found[i].contents;
f.writeln(PsData);
f.close();
}
}
Thanks in Advance
Try the following
//Export Text Based on Paragraph Style
var myDoc = app.activeDocument;
var fileName = app.activeDocument.name.replace (/\.indd$/, '.txt');
var folderSelect = Folder.selectDialog("Please select a Folder to Save Text File");
var f = File(folderSelect+"/"+fileName);
f.encoding = "UTF-8";
f.open("w");
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
app.findGrepPreferences.appliedParagraphStyle = "EN Bold Title_1"
found = app.activeDocument.findGrep(true);
a
...
Make the following amends to your code
for (i = 0; i < found.length; i++){
myfoundTextSorted.push (found[i].contents + "\t" + found[i].parentTextFrames[0].parentPage.name);
}
myfoundTextSorted.sort(function(a,b){
return a.match(/\t(\d+)$/)[1] - b.match(/\t(\d+)$/)[1]
})
myStrings = myfoundTextSorted.join("\r")
-Manan
Ok, let me try adding some description to the code I shared.
The changes in the for loop are to make the entries in a defined format which would help us design our sorting approach in the later stage. Each entry in the array is created in the following format
<Content of the found element><tab character><the page number where the content is found>
Now we get to sort the array with each element in the format defined above. In the sort method we can pass as an argument a function that determines t
...Copy link to clipboard
Copied
Try the following
//Export Text Based on Paragraph Style
var myDoc = app.activeDocument;
var fileName = app.activeDocument.name.replace (/\.indd$/, '.txt');
var folderSelect = Folder.selectDialog("Please select a Folder to Save Text File");
var f = File(folderSelect+"/"+fileName);
f.encoding = "UTF-8";
f.open("w");
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
app.findGrepPreferences.appliedParagraphStyle = "EN Bold Title_1"
found = app.activeDocument.findGrep(true);
app.findGrepPreferences = null;
for (i = 0; i < found.length; i++)
f.writeln(found[i].contents);
f.close();
-Manan
Copy link to clipboard
Copied
Thanks a lot @Manan Joshi
Copy link to clipboard
Copied
Hi Manan, Please i developed the old script to export character style text and sort the array, i succeed in that but when i try to show the page number in front of each text line i failed, here is my try, do you think this can be done?, please help to fix it and thanks in advance :
//Export Text Based on Character Style Sorted with Page Number
var myDoc = app.activeDocument;
var fileName = app.activeDocument.name.replace (/\.indd$/, '.txt');
var folderSelect = Folder.selectDialog("Please select a Folder to Save Text File");
var f = File(folderSelect+"/"+fileName);
f.encoding = "UTF-8";
f.open("w");
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
app.findGrepPreferences.appliedCharacterStyle = "En-Index-Title"
found = app.activeDocument.findGrep(true);
var myfoundTextSorted = new Array;
app.findGrepPreferences = null;
for (i = 0; i < found.length; i++)
var PageNumber = found[i].characters[0];
myfoundTextSorted.push (found[i].contents, PageNumber.parentTextFrames[0].parentPage.name);
myfoundTextSorted.sort();
myString = myfoundTextSorted.join ('\r')
//$.writeln(myString); //write in Console
f.writeln(myString);
f.close();
alert("Done!, Click OK to Open Folder!", "Finish");
if (f.exists)
Folder(folderSelect).execute(); //Open the Desktop Folder
Copy link to clipboard
Copied
Hi @M.Hasanin,
I see multiple issues in your code, which i list below
Try to work on these pointers and let us know if you are still stuck.
-Manan
Copy link to clipboard
Copied
Hi @Manan Joshi
Thanks for your fixing recommendations , i will do my best to fix and let you know if i stuck in some levels!.
Copy link to clipboard
Copied
Hi @Manan Joshi I Fixed the Code as the Following, but i dont know how to sort it! (i stuck in this point) it Show Text with Page Number, here is the fixed code without sorting :
//Export Text Based on Character Style with Page Number
var myDoc = app.activeDocument;
var fileName = app.activeDocument.name.replace (/\.indd$/, '.txt');
var folderSelect = Folder.selectDialog("Please select a Folder to Save Text File");
if(folderSelect == null){ //User Hit Cancel!
exit();
}else{
var f = File(folderSelect+"/"+fileName);
f.encoding = "UTF-8";
f.open("w");
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
app.findGrepPreferences.appliedCharacterStyle = "En-Index-Title"
found = app.activeDocument.findGrep(true);
var myfoundTextSorted = new Array;
app.findGrepPreferences = null;
for (i = 0; i < found.length; i++){
myfoundTextSorted.push (found[i].contents, found[i].parentTextFrames[0].parentPage.name+"\r");
}
myStrings = myfoundTextSorted.join ('\t')
//Write File and Close
f.writeln(myStrings);
f.close();
alert("Done!, Click OK to Open Folder!", "Finish");
if (f.exists)
Folder(folderSelect).execute(); //Open the Desktop Folder
}
and here is what i get after execution (title and tab and page number) :
Copy link to clipboard
Copied
Make the following amends to your code
for (i = 0; i < found.length; i++){
myfoundTextSorted.push (found[i].contents + "\t" + found[i].parentTextFrames[0].parentPage.name);
}
myfoundTextSorted.sort(function(a,b){
return a.match(/\t(\d+)$/)[1] - b.match(/\t(\d+)$/)[1]
})
myStrings = myfoundTextSorted.join("\r")
-Manan
Copy link to clipboard
Copied
Thanks alot @Manan Joshi but i need to understand what is happening! , the javascript learning is long journey! :), so please let me tell you what i basically understand from the updated code, in the first four lines i understand this method is organized and easy to update also the last line,but what i really need to understand is the sorting itself, its something looks like GREP, maybe it mean to sort by page number only but really i cant get exactly the full meaning, can you please explain more about this method? and thanks again.
Copy link to clipboard
Copied
Ok, let me try adding some description to the code I shared.
The changes in the for loop are to make the entries in a defined format which would help us design our sorting approach in the later stage. Each entry in the array is created in the following format
<Content of the found element><tab character><the page number where the content is found>
Now we get to sort the array with each element in the format defined above. In the sort method we can pass as an argument a function that determines the comparison(equality, greater than, less than) of two values of the array.
For details on the sort method, I list the function description below
Array.sort (userFunction: Function )
Core JavaScript Classes
Sorts the elements of the array in place, using the given function to compare to elements.
If no function is provided, the elements are sorted alphabetically. Returns no return value.
userFunction: Data Type: Function
A user-supplied function of the form userFunction(a, b) which returns less than 0 if a is greater than b, 0 if a and b are equal, and greater than 0 if b is greater than a.
Example:
array.sort(userFunction)
Now all we are left with is to define this function. In this function I use grep to extract the page numbers from each array entry and then use that to determine the order between two array values. Here I did not provision for the cases where the page numbers are same for both array values, in such a case my code will consider both the values as equal. However you can augment the sort function to consider text sorting in cases where the page numbers are equal.
Finally after sorting we join the array elements with a new line between each entry.
I hope you get the drift of what I did and would now be able to use it or change the sort function as per your needs.
-Manan
Copy link to clipboard
Copied
Wow!, Wonderful Explanation, Thanks a lot Eng. @Manan Joshi , Thanks for Helping me and the Community, you are a Great Teacher, and execuse my many many questions because im graphic designer who try to learn indesign scripting, Thanks again.
Copy link to clipboard
Copied
Hi,
Would it be possible to edit this script for exporting a paragraph based on a paragraph style AND the following paragraph?
I'm trying to extract key terms (which use a specific paragraph style) and it's definition (which is in the paragraph following the key term). I did a few try but can't get it to work.
Any help would be greatly appreciated.
Copy link to clipboard
Copied
Something like the following should work. Change the name of the paragraphstyle you want to search by changing the value of variable pstyle
var pstyle = "PS1"
var myDoc = app.activeDocument;
var fileName = app.activeDocument.name.replace (/\.indd$/, '.txt');
var folderSelect = Folder.selectDialog("Please select a Folder to Save Text File");
var f = File(folderSelect+"/"+fileName);
f.encoding = "UTF-8";
f.open("w");
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
app.findGrepPreferences.appliedParagraphStyle = pstyle
found = app.activeDocument.findGrep();
app.findGrepPreferences = null;
for (i = 0; i < found.length; i++){
f.writeln(found[i].contents);
var idx = found[i].characters[-1].index + 2
var psc = found[i].parentStory.characters
if(psc[idx].isValid && psc[idx].appliedParagraphStyle.name != pstyle)
f.writeln(psc[idx].paragraphs[0].contents);
}
f.close();
-Manan
Copy link to clipboard
Copied
It works fine and will save me so much time.
Thank you so much Manan!