Copy link to clipboard
Copied
Hi,
Is possible extract words with a specific character style applied to other indesign document or a txt file using a script?
Thank you in advance!
Try this:
...var doc = app.activeDocument;
var list = [];
//~ var sel = app.selection[0].parentStory;
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
//~ var found = sel.findGrep();
var found = doc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
for
Copy link to clipboard
Copied
var doc = app.activeDocument;
var list = [];
var sel = app.selection[0].parentStory;
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
var found = sel.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
for (var i=found.length-1; i>=0; i--) {
list.push(found.contents);
}
alert(list.join("\r"));
Try this.
Copy link to clipboard
Copied
Hi!
Your code works only if the text frame is selected. I need to export all words of the document.
And instead of a list I want a txt file with the words. Maybe using the "ExportFormat.textType" command.
Can you help me? Thank you for your attention.
Copy link to clipboard
Copied
Try this:
var doc = app.activeDocument;
var list = [];
//~ var sel = app.selection[0].parentStory;
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
//~ var found = sel.findGrep();
var found = doc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
for (var i=0; i<found.length; i++) {
list.push(found.contents);
}
//~ alert(list.join("\r"));
var myFile = new File(Folder.desktop + "/" + app.activeDocument.name.replace(/\.indd$/i, ".txt"));
if (File.fs == "Windows")
var listFile = myFile.saveDlg("Save list", "Plain text file: *.txt" );
else
listFile = myFile.saveDlg("Save list");
if (listFile != null)
{
if (listFile.open("w"))
{
listFile.encoding = "utf8"; //ENCONDING
listFile.write(list.join("\r") + "\r"); //WRITE THE ARRAY TO TXT FILE
listFile.close(); //CLOSE TXT FILE
listFile.execute(); //OPEN TXT FILE
}
}
Copy link to clipboard
Copied
Thank you so much! Perfect!
I have a last wish if you could help.
I have some words that are duplicated across the document and your script get all of them. There's a way to get only one instance of each word?
I promise not to bother you anymore. ​
lf.corullon​
Copy link to clipboard
Copied
var doc = app.activeDocument;
var list = [];
if (app.selection.length != 1) {
var sel = doc;
}
else {
var sel = app.selection[0].parentStory;
}
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByName("test");
var found = sel.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
for (var j=0; j<found.length; j++) {
list.push(found
.contents); }
//~ alert(list.join("\r"));
function unique(list) {
var o = {}, i, l = list.length, r = [];
for(i=0; i<l;i++) o[list] = list;
for(i in o) r.push(o);
return r;
};
var listFinal = unique(list).join("\r");
alert(listFinal);
var myFile = new File(String(app.activeDocument.fullName).replace(/\.indd$/i, ".txt"));
if (File.fs == "Windows")
var listFile = myFile/*.saveDlg("Save list", "Plain text file: *.txt" )*/;
else
listFile = myFile/*.saveDlg("Save list")*/;
if (listFile != null)
{
if (listFile.open("w"))
{
listFile.encoding = "utf8"; //ENCONDING
listFile.write(listFinal + "\r"); //WRITE THE ARRAY TO TXT FILE
listFile.close(); //CLOSE TXT FILE
//~ listFile.execute(); //OPEN TXT FILE
}
}