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

Using JSX to add lines between objects

Participant ,
Mar 11, 2023 Mar 11, 2023

I am trying to createa ascript that places a horizontal line between selected objets. Could someone eyball this and tell me where I'm going wrong?

 

Thanks

 

// Get the active document
var myDoc = app.activeDocument;
// Get the selection
var mySelection = myDoc.selection;
// Check if at least two objects are selected
if (mySelection.length < 2) {
alert("Please select at least two objects.");
} else {
// Get the page of the first selected object
var myPage = mySelection[0].parentPage;
// Get the geometric bounds of the selection
var myBounds = mySelection[0].geometricBounds;
for (var i = 1; i < mySelection.length; i++) {
var objBounds = mySelection[i].geometricBounds;
if (objBounds[0] < myBounds[0]) {
myBounds[0] = objBounds[0];
}
if (objBounds[1] < myBounds[1]) {
myBounds[1] = objBounds[1];
}
if (objBounds[2] > myBounds[2]) {
myBounds[2] = objBounds[2];
}
if (objBounds[3] > myBounds[3]) {
myBounds[3] = objBounds[3];
}
}
// Calculate the center of the selection
var myX1 = myBounds[0];
var myY1 = myBounds[1];
var myX2 = myBounds[2];
var myY2 = myBounds[3];
var myCenter = (myY1 + myY2) / 2;
// Add a horizontal line to the page
var myLine = myPage.lines.add({
geometricBounds: [myCenter + "p", "36p", (myCenter + 1) + "p", (myPage.bounds[3] - 36) + "p"],
strokeColor: myDoc.swatches.itemByName("Black"),
strokeWeight: 0.5,
strokeTint: 100,
strokeAlignment: StrokeAlignment.centerAlignment
});
}
TOPICS
How to , Import and export , Publish online , Scripting , SDK , UXP Scripting
814
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

Advisor , Mar 11, 2023 Mar 11, 2023

Hello @ht354,

You'll need to use...

 

var myLine = myPage.graphicLines.add

 

 to add a graphic line.

 

Regards,

Mike

Translate
People's Champ ,
Mar 11, 2023 Mar 11, 2023

Well, I just copied/pasted your script and ran it. Then I got the error which is pretty explicit. In your case, script fails here:

myPage.lines.add…

Which means the Page object doesn't understand what lines is as a property. So next question is: is Line a regular property of Page object and answer is here:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Page.html

I think I now understand why some folks warned me about people asking for help when they asked ChatGPT to create a script that doesn't work but don't understand why.

You will find more interest in the long-term understanding of the deep concepts of InDesign Scripting.

Loic

 

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 ,
Mar 11, 2023 Mar 11, 2023

Hello @ht354,

You'll need to use...

 

var myLine = myPage.graphicLines.add

 

 to add a graphic line.

 

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
Participant ,
Mar 12, 2023 Mar 12, 2023

That worked! Thank you. The whole line versus graphic line is very confusing in the Adobe Documentation as you read through it. As a newbie dipping his toes into Extendscript programing I can see its power but I can also see why so very few people delve into it for InDesign. For example the script places a horizontal line on the page but it is at a 10° angle, enbedded into a layer without beingselected to show without having to manually go to Layers and select and enable the horizontal line.

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 ,
Mar 12, 2023 Mar 12, 2023
LATEST

Hi @ht354 , I haven’t tested your script, but the geometricBounds array is Y1,X1,Y2,X2, not X1,Y1,X2,Y2, which might explain the angle you are getting. To select a page item, you have to use the select() method—myLine.select()

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