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

Tagging in InDesign CS5, js

Participant ,
Apr 25, 2012 Apr 25, 2012

Hi,

I need to assign tags to objects in InDesign through script. I looked in OMV, but I couldn't find tags. I would assume it would be something like this:

myFrame.tag = "picture1";

I found autoTag, but it does not let me to assign my tag.

Your help is highly appreciated.

Yulia

TOPICS
Scripting
2.2K
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

Participant , Apr 27, 2012 Apr 27, 2012

Thank you, Kasyan, it worked perfectly.

Translate
Valorous Hero ,
Apr 25, 2012 Apr 25, 2012

Main();

function Main() {

    var doc = app.activeDocument;

    var frame = app.selection[0];

    var xmlElement = doc.xmlElements[0].xmlElements.add("picture1");

    frame.markup(xmlElement);

}

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
Participant ,
Apr 27, 2012 Apr 27, 2012

Thank you, Kasyan, it worked perfectly.

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
Community Expert ,
Dec 23, 2016 Dec 23, 2016

You should not have clicked "correct answer" to your own reply!

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
Engaged ,
Dec 23, 2016 Dec 23, 2016

Dear Kasyan,

Is it possible to complete document tagged once page wise?

If my object text frame then use para tag and if my object images then use img tag.

Suppose in my document 2 pages then structure look like attachment.

Regards,

Sumit

Screen Shot 2016-12-23 at 5.32.05 PM.png

-Sumit
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
Valorous Hero ,
Dec 25, 2016 Dec 25, 2016

From what I see on the screenshot, for each page you want to add an "Article" element and add "para" and "img" elements (for each text/image frame on the page) to it, skipping frames which are not on pages (e.g. on the pasteboard). Though I'm not quite sure that this is exactly what you want: can't make out the details on the screenshot.

Anyway here's the code illustrating how you can approach the task:

var scriptName = "Apply tags page-by-page",

doc;

PreCheck();

//===================================== FUNCTIONS ======================================

function Main() {

    var page, xmlElementArticle, xmlElementPara, xmlElementImg, textFrame, rectangle,

    pages = doc.pages;

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

        page = pages;

        xmlElementArticle = doc.xmlElements[0].xmlElements.add("Article");

       

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

            textFrame = page.textFrames;

            xmlElementPara = xmlElementArticle.xmlElements.add("para");

            textFrame.markup(xmlElementPara);

        }

   

        for (var k = 0; k < page.rectangles.length; k++) {

            rectangle = page.rectangles;

            xmlElementImg = xmlElementArticle.xmlElements.add("img");

            rectangle.markup(xmlElementImg);

        }

    }

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function PreCheck() {

    if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);

    doc = app.activeDocument;

    if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);

    if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);

    Main();

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function ErrorExit(error, icon) {

    alert(error, scriptName, icon);

    exit();

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

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
Engaged ,
Dec 25, 2016 Dec 25, 2016

Dear Kasyan,

You are great. Your script nicely working and you understood my problem. My English is poor.

Everything is okay except one thing. I need object in sequence like below:

ksyan.PNG

Regards,

Sumit

-Sumit
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
Valorous Hero ,
Dec 26, 2016 Dec 26, 2016

Hi Sumit,

My English is far from perfect too.

What are you trying to achieve?

  1. You want to write the script on your own but don't know how, or encountered a problem?
  2. You're going to post a request-after-request until you have the script written for you by someone else?

In case 1

Take a look at the geometricBounds property. (If some objects have strokes, use visibleBounds instead). This property is an array of 4 numbers which give you coordinates of the top, left, bottom and right edges of the frame. You can use them to sort frames on the page. How exactly to do this I have no idea because I don't know your situation: how to sort -- first horizontally or vertically, take into account the width and height or not, etc.

Search the forum; probably someone has  already done this before.

In case 2

This forum is mostly for solving scripting problems. Also, people look for ready-to-use scripts here. If you want a script written for you, please describe ALL you need in the original post and maybe someone will honor your request.

Regards,
Kasyan

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
Engaged ,
Dec 26, 2016 Dec 26, 2016

Dear Kasyan,

Thank you so much for your reply.

I will use your last script that is working good and I will move element manually as I needed.

Thanks and Regards,

Sumit

-Sumit
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
Engaged ,
Dec 30, 2016 Dec 30, 2016

Dear Kasyan,

When, I edit second loop for P element, then text frames comes in sequence like below.

        for (var j = page.textFrames.length-1; j > 0; j--) { 

            textFrame = page.textFrames

            xmlElementPara = xmlElementArticle.xmlElements.add("para");  

            textFrame.markup(xmlElementPara);  

        }

 

Screen Shot 2016-12-30 at 5.57.34 PM.png

But problem is escaping last text frame of every pages.

If I use below code:

        for (var j = page.textFrames.length; j > 0; j--) { 

            textFrame = page.textFrames

            xmlElementPara = xmlElementArticle.xmlElements.add("para");  

            textFrame.markup(xmlElementPara);  

        }

Then get error massage.

Screen Shot 2016-12-30 at 6.14.36 PM.png

Can you guide me once, please?

Regards,

Sumit

-Sumit
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
Valorous Hero ,
Dec 30, 2016 Dec 30, 2016

You should loop through ALL the objects either forwards

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

or backwards

for (var i = myCollection.length - 1; i >= 0; i--) {

As far as I understand, you're trying to skip the last element in collection. But there's no dependence between the object's index

myCollection

and it's position on the page. I guess you want to skip the bottom most frame on the page. In this case (the easiest way) you can check if the object is below a certain position from the top of the page (for simplicity, let's assume that zero point is in the default location: the top left corner of the page) using geometricBounds[0] (top of the frame). Something like this:

if (myCollection.geometricBounds[0] < 1000) {

    // add xml element

}

else {

    // skip it

}

Another -- a better and a little more complex way -- is to write a function which would get all frames and compare their top edges returning the bottom most one.

— Kas

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
Engaged ,
Jan 13, 2017 Jan 13, 2017
LATEST

Thank you Kasyan!

-Sumit
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