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

Tagging an array of characters with an xml tag using Javascript.

Explorer ,
Jun 27, 2019 Jun 27, 2019

I would like to create a script which loops over all paragraphs and tags the content of the paragraph between the last tab and the first return using an xml tag.

This doesn't work:

var doc = app.activeDocument;

allDocParagraphs = doc.stories.everyItem().paragraphs.everyItem().getElements();

var docXmlRootElem = doc.xmlElements[0];

myXmlTag = doc.xmlTags.add("XML_tag_A");

for (var i = 0; i < allDocParagraphs.length; i++) {

    var curParagraph = allDocParagraphs;

    var curParChars = curParagraph.characters;

    var charsToSelectArray = new Array();

    for (var j = 0; j < curParChars.length; j++) {

        var curChar = curParChars;

        var curCharContents = curChar.contents;

        if (curCharContents == "\t") {

            charsToSelectArray = new Array();

        }

        else if (curCharContents != "\r" && curCharContents != "\n") {

            charsToSelectArray.push(curChar);

        }

    }

}

docXmlRootElem.xmlElements.add(myXmlTag, charsToSelectArray);

The last line gives this error:

Runtime Error: Error Code# 30477: Invalid value for parameter 'xmlContent' of method 'add'. Expected Text, Story, PageItem, Movie, Sound, Graphic, Table or Cell, but received (Character, Character, Character, Character, Character, Character, Character, Character, Character, Character, Character, Character, Character). @ file '~/.../bug.jsx' [line:23, col:NaN]

Which gives some sense, since I am providing an Array of Character.

So I try to first make a selection of the characters and then tag them (just like you would do in the UI) using this code:

var doc = app.activeDocument;

allDocParagraphs = doc.stories.everyItem().paragraphs.everyItem().getElements();

var docXmlRootElem = doc.xmlElements[0];

myXmlTag = doc.xmlTags.add("XML_tag_A");

for (var i = 0; i < allDocParagraphs.length; i++) {

    var curParagraph = allDocParagraphs;

    var curParChars = curParagraph.characters;

    app.select(NothingEnum.NOTHING);

    for (var j = 0; j < curParChars.length; j++) {

        var curChar = curParChars;

        var curCharContents = curChar.contents;

        if (curCharContents == "\t") {

            app.select(NothingEnum.NOTHING);

        }

        else if (curCharContents != "\r" && curCharContents != "\n") {

            doc.select(curChar, SelectionOptions.ADD_TO);

        }

    }

}

But then I get this error on the doc.select line:

Runtime Error: Error Code# 30477: Invalid value for parameter 'selectableItems' of method 'select'. Expected Object, Array of Objects, NothingEnum enumerator or SelectAll enumerator, but received Character. @ file '~/.../bug2.jsx' [line:19, col:NaN]

This doesn't make sense to me since a Character is an object. Why can't I select it?

An array of Characters also doens't work.

Selecting using app.select() or app.selection also doesn't seem to work:

var doc = app.activeDocument;

allDocParagraphs = doc.stories.everyItem().paragraphs.everyItem().getElements();

var docXmlRootElem = doc.xmlElements[0];

myXmlTag = doc.xmlTags.add("XML_tag_A");

for (var i = 0; i < allDocParagraphs.length; i++) {

    var curParagraph = allDocParagraphs;

    var curParChars = curParagraph.characters;

    var charsToSelectArray = new Array();

    for (var j = 0; j < curParChars.length; j++) {

        var curChar = curParChars;

        var curCharContents = curChar.contents;

        if (curCharContents == "\t") {

            charsToSelectArray = new Array();

        }

        else if (curCharContents != "\r" && curCharContents != "\n") {

            charsToSelectArray.push(curChar);

        }

    }

    app.select(NothingEnum.NOTHING);

    //app.select(charsToSelectArray); // Fails with  Invalid value for parameter 'selectableItems' of method 'select'

    app.selection = charsToSelectArray; // Fails with Invalid value for set property 'selection'. Expected Array of Objects, Object or NothingEnum enumerator

}

You can download a testfile here: Dropbox - tag problem.indd - Simplify your life

Edit: Added new failed test

TOPICS
Scripting
676
Translate
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

Community Expert , Jun 27, 2019 Jun 27, 2019

Hi Joansy,

Use the following code in place of your code in line 19

if(doc.selection.length)

    doc.select(curChar, SelectionOptions.ADD_TO);

else

    doc.select(curChar)

The issue explanation that i have is if the selection does not have anything, InDesign throws an error when using the selectionoptions.add_to argument. So we need to make adjustments in our code for the same.

Also i see your code is a work in progress, so i would not barge in with any more information that disturbs you from your thoug

...
Translate
Community Expert ,
Jun 27, 2019 Jun 27, 2019

Hi Joansy,

Use the following code in place of your code in line 19

if(doc.selection.length)

    doc.select(curChar, SelectionOptions.ADD_TO);

else

    doc.select(curChar)

The issue explanation that i have is if the selection does not have anything, InDesign throws an error when using the selectionoptions.add_to argument. So we need to make adjustments in our code for the same.

Also i see your code is a work in progress, so i would not barge in with any more information that disturbs you from your thought process. One pointer though, once you are done correcting this piece of code, you could investigate the use of find grep to find these characters instead of looping over each character of every paragraph. That would be much optimised and faster.

-Manan

Translate
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
Explorer ,
Jun 27, 2019 Jun 27, 2019

That works! Thanks a lot!

I'll also check grep

Translate
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
Explorer ,
Jun 27, 2019 Jun 27, 2019
LATEST

I first wanted to  finish using my approach by looping over the characters, but it gave additional issues with return characters. No such issues using the grep method. In case anybody would need it:

var doc = app.activeDocument;

allDocParagraphs = doc.stories.everyItem().paragraphs.everyItem().getElements();

var docXmlRootElem = doc.xmlElements[0];

app.findGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.findWhat = "[^(\t|\r|\n)]+$"

myXmlTag = doc.xmlTags.add("XML_tag_A");

for (var i = 0; i < allDocParagraphs.length; i++) {

    var curParagraph = allDocParagraphs;

    var curParChars = curParagraph.characters;

    app.select(NothingEnum.NOTHING);

    var textToTag = curParagraph.findGrep();

    app.select(textToTag)

    var selection = app.selection[0];

    docXmlRootElem.xmlElements.add(myXmlTag, selection);

}

Translate
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