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

Extract words with specific style applied

Participant ,
Jul 18, 2017 Jul 18, 2017

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!

TOPICS
Scripting

Views

604

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 1 Correct answer

Enthusiast , Jul 18, 2017 Jul 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

...

Votes

Translate

Translate
Enthusiast ,
Jul 18, 2017 Jul 18, 2017

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.

Votes

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
Participant ,
Jul 18, 2017 Jul 18, 2017

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.

Votes

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 ,
Jul 18, 2017 Jul 18, 2017

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

}

}

Votes

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
Participant ,
Jul 19, 2017 Jul 19, 2017

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​

Votes

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 ,
Jul 19, 2017 Jul 19, 2017

Copy link to clipboard

Copied

LATEST

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

}

}

Votes

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