Skip to main content
Inspiring
January 10, 2019
Answered

Find items via script label

  • January 10, 2019
  • 3 replies
  • 3695 views

I am a catalog production artist. I have been working with a JSX script which fetches and places INDD snippets from a folder based the file name. I would like to streamline my process by forgoing the Snippets, and simply copy the required text frames from one INDD document to another, identifying the required text frames by searching on the Script Label. I have this bit of code that works to select a specific text frame. However, once it finds the target, it doesn't do anything with it;

do

{

  if (app.documents.length == 0)

  {

    break;

  }

  var document = app.activeDocument;

  if (! (document instanceof Document))

  {

    break;

  }

  var currentItem = null;

  if (app.selection.length > 0)

  {

    currentItem = app.selection[0];

  }

  var undoneItems = [];

  for (var idx = 0; idx < document.allPageItems.length; idx++)

  {

    var pageItem = document.allPageItems[idx];

    if (pageItem.label == "11010001")

    {

      undoneItems.push(pageItem);

    }

  }

  if (undoneItems.length <= 0)

  {

    break;

  }

  var nextItemIdx = 0;

  if (currentItem != null)

  {

    var currentItemIdx = -1;

    var searchItemIdx = 0;

    while (currentItemIdx == -1 && searchItemIdx < undoneItems.length)

    {

       if (undoneItems[searchItemIdx] == currentItem)

       {

         currentItemIdx = searchItemIdx;

       }

       searchItemIdx++;

    }

    if (currentItemIdx >= 0)

    {

      nextItemIdx = currentItemIdx + 1;

      if (nextItemIdx >= undoneItems.length)

      {

        nextItemIdx = 0;

      }

    }

  }

  app.select(undoneItems[nextItemIdx]);

}

while (false);

However, the Script Label is hard coded in the script which you can see in the highlighted line above. What I want is to be able to do is feed a text document of 8 digit numbers, have the script walk though the entire INDD source file, and copy the found items from the text file into an empty INDD target file. An extra bonus would be if the script could return a report of items it did not locate when completed.

This topic has been closed for replies.
Correct answer Sunil Yadav

Try this code, this might help you...

var myFile = File(".../Desktop/Test/TestFile.txt");

myFile.open("r");

myLine = myFile.read();

myFile.close();

var lines = myLine.split(/\r?\n/);

var allLabels = [];

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

    allLabels.push(lines.toString());

    }

var myDoc = app.documents[0];

var pages = myDoc.pages;

for(var p = 0; p < pages.length; p++){

    var pageItems = pages.allPageItems;

    for(var pt = 0; pt < pageItems.length; pt++){

        if(pageItems[pt].label != ""){

            var found = match(pageItems[pt], allLabels);

            if(found){

                /// Do your stuff here whether you want to copy or whatever you want to do-----

                }

            }

        }

    }

////////////////////////////////////////////////

function match(myItem, myArray){

    for(var q = 0; q < myArray.length; q++){

        if(myArray.toString() == myItem.label.toString()){

            return true;

            }

        }

    return false;

    }

3 replies

Stefan Rakete
Inspiring
January 11, 2019

Have a look at this snippet-placer tool

http://www.indesignscript.de/skript-detail/detail/News/snippetplacer/

Thx Stefan

KramerAuthor
Inspiring
January 14, 2019

Sorry Stephan, that tool is in German and Ich spreche kein deutsch. Thanks though.

Sunil Yadav
Sunil YadavCorrect answer
Legend
January 11, 2019

Try this code, this might help you...

var myFile = File(".../Desktop/Test/TestFile.txt");

myFile.open("r");

myLine = myFile.read();

myFile.close();

var lines = myLine.split(/\r?\n/);

var allLabels = [];

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

    allLabels.push(lines.toString());

    }

var myDoc = app.documents[0];

var pages = myDoc.pages;

for(var p = 0; p < pages.length; p++){

    var pageItems = pages.allPageItems;

    for(var pt = 0; pt < pageItems.length; pt++){

        if(pageItems[pt].label != ""){

            var found = match(pageItems[pt], allLabels);

            if(found){

                /// Do your stuff here whether you want to copy or whatever you want to do-----

                }

            }

        }

    }

////////////////////////////////////////////////

function match(myItem, myArray){

    for(var q = 0; q < myArray.length; q++){

        if(myArray.toString() == myItem.label.toString()){

            return true;

            }

        }

    return false;

    }

KramerAuthor
Inspiring
January 14, 2019

@yDvs92328: I cannot get past line 13. I get an error that says, "Object does not support the property or method 'allPageItems." BTW: I am working INDD CC 2019 for Mac if it makes a difference.

Community Expert
January 14, 2019

It should be:

var pageItems = pages

.allPageItems;

Variable pages holds the pages collection of the document.

Regards,
Uwe

Community Expert
January 11, 2019

The requirement is not clear. Answer the following questions

  • The input would be a text file with each line of eight digit numbers? Like
    • 12345678
    • 23456789
  • The script will parse this text file, pick up each number and find all the frames in the activedocument in InDesign with the label equal to this number
  • Copy these frames over to a new document? Will this document be open or do you want this document to be opened, frames copied over and then the document saved and closed.

And the above steps are repeated till all the numbers in text file are processed.

Correct if something is wrong or something is missing.

-Manan

-Manan
KramerAuthor
Inspiring
January 14, 2019

Yes, you have the process correct. Ideally, these found items copied into a document which is opened by the user.