Copy link to clipboard
Copied
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
Thank you, Kasyan, it worked perfectly.
Copy link to clipboard
Copied
Main();
function Main() {
var doc = app.activeDocument;
var frame = app.selection[0];
var xmlElement = doc.xmlElements[0].xmlElements.add("picture1");
frame.markup(xmlElement);
}
Copy link to clipboard
Copied
Thank you, Kasyan, it worked perfectly.
Copy link to clipboard
Copied
You should not have clicked "correct answer" to your own reply!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
Copy link to clipboard
Copied
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:
Regards,
Sumit
Copy link to clipboard
Copied
Hi Sumit,
My English is far from perfect too.
What are you trying to achieve?
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
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.
Can you guide me once, please?
Regards,
Sumit
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thank you Kasyan!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now