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

Get specific rectangle

Community Beginner ,
Dec 31, 2019 Dec 31, 2019

hi all,

I need some help with javascript for indesign.

How can get a specific rectangle with image? I would to copy a specific rectangle with inside image and to paste into the other document. I thinking to use label but i don't find method to get that object via javascript.

Thank, 

Sergio

TOPICS
How to , Scripting
2.0K
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

Advocate , Jan 12, 2020 Jan 12, 2020

Try this code:

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

var currentDoc = app.documents[0];
var nextDoc = app.documents[1];
for(var l = 0; l < currentDoc.links.length; l++){
    if(currentDoc.links[l].parent.parent.constructor.name.toString() == "Rectangle"){
        if(currentDoc.links[l].parent.parent.parent.constructor.name.toString() == "Spread"){
            app.select(currentDoc.links[l].parent.parent);
            app.copy();
            app.open(nextDoc.

...
Translate
Advisor ,
Dec 31, 2019 Dec 31, 2019

Sergio,

If you are selecting and copying to the clipboard you could simply run the code snipet below in the other document.

 

doc = app.documents[0];

app.paste();

 

Regards,

Mike

 

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 31, 2019 Dec 31, 2019

You can get the containing frame if you know the image link name like so: 

 

app.activeDocument.links.itemByName("filename.png").parent.parent;
 
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 Beginner ,
Dec 31, 2019 Dec 31, 2019

Hi all, and many thanks to Brian and Mike.

I am sorry but my english is not very good.

Follow I try to explain with an example. Immagine this:

Doc_a with some rectangles that crop images. Rect_a, Rect_b and Rect_c

I would copy Rect_b and paste in a new Doc_b.

I hope i was clear.

I wish happy new year to all community members.

Sergio

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 Beginner ,
Jan 02, 2020 Jan 02, 2020

Hi all,

Finally I solved the problem myself.

I set Rectangle name throught layer palette, than I get it with follow code:

var MyRectangle = app.documents.item(0).pages.item(0).rectangles.item("Rectangle_01");

Image.jpg

 

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
Advisor ,
Jan 02, 2020 Jan 02, 2020

Hello Sergio,

 

To answer your question from this post:

"I am sorry but my english is not very good.

Follow I try to explain with an example. Immagine this:

Doc_a with some rectangles that crop images. Rect_a, Rect_b and Rect_c

I would copy Rect_b and paste in a new Doc_b.

I hope i was clear."

 

I came up with the code below, its works to your specifications with the images having applied labels. Be sure to only select one image at a time when running.........

doc = app.documents[0];
var myDocFolder = doc.filePath;

var mySelection = app.selection[0];

app.copy();

var myLabelName = mySelection.label;

app.documents.add();

app.paste();

var myNewDoc = app.documents[0];

var docID = myLabelName.replace(/Rect_/gi, "Doc_");

myNewDoc.save(File(myDocFolder.fsName + "/" + docID));

 

Regards,

Mike

 

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
Guest
Jan 02, 2020 Jan 02, 2020

hi Sergio,

kindly Find below scrtipt to get a specific rectangles set some label in window/utilities/scriptLabel

var myDoc=app.documents[0]

var myPageFrame = myDoc.pages[0].rectangles;OR myDoc.pages[0].rectangles.allPageItems
for (var tx=0; myPageFrame.length>tx; tx++){

if(myPageFrame[tx].label == "Address"){
myPageFrame[tx].textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_CENTER_POINT;
myPageFrame[tx].textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;
myPageFrame[tx].fit(FitOptions.frameToContent);
}
}
 

u can add any page like pages[0] or 1,

if u don t know then add on loop for page also  

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
Advocate ,
Jan 12, 2020 Jan 12, 2020

Try this code:

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

var currentDoc = app.documents[0];
var nextDoc = app.documents[1];
for(var l = 0; l < currentDoc.links.length; l++){
    if(currentDoc.links[l].parent.parent.constructor.name.toString() == "Rectangle"){
        if(currentDoc.links[l].parent.parent.parent.constructor.name.toString() == "Spread"){
            app.select(currentDoc.links[l].parent.parent);
            app.copy();
            app.open(nextDoc.filePath.fsName.replace(/\\/g,'/')+"/"+nextDoc.name);
            app.pasteInPlace();
            app.open(currentDoc.filePath.fsName.replace(/\\/g,'/')+"/"+currentDoc.name);
            }
        }
    }

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

Best

Sunil

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

Hi Sergio,

note the following if you call items by name with e.g.:

 

item("Name")

itemByName("Name")

 

That will always return only one single item ( InDesign CS5 and above ).

Even if there are more items with the same name.

 

Most important: Verify that your item really exists with property isValid. E.g run the two lines below. You'll see that there will be no error message. And definitely there is no page item at all in the new document:

var myDoc = app.documents.add();
var myItem = myDoc.pageItems.itemByName( "MyName" );

InDesign will not resolve the identifier of the page item until you like to do something with it.

For example you want to apply a fill color:

myItem.fillColor = myDoc.colors.itemByName("Magenta");

The line above will throw an error. Why? myItem simply does not exist.

You could test with:

myItem.isValid // Would return false in this case

 

Regards,
Uwe Laubender

( ACP )

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