Skip to main content
Known Participant
July 18, 2017
Answered

Extract words with specific style applied

  • July 18, 2017
  • 1 reply
  • 873 views

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!

This topic has been closed for replies.
Correct answer lfcorullon13651490

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

}

}

1 reply

lfcorullon13651490
Legend
July 18, 2017

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.

RibnogAuthor
Known Participant
July 18, 2017

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.

lfcorullon13651490
lfcorullon13651490Correct answer
Legend
July 18, 2017

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

}

}