adobe-indesignextendscript

newbie moving from applescript to extend script trying to round corners


I am pretty comfortable with scripting Indesign using Applescript, now I want to learn extendscript, & idjs sripting, I am an absolute beginer with both. Below I have managed to cobble together a basic setup, but I think iv'e mixed the above styles into one script, as it feels and looks very awkward. can someone please show me how to tidy this mess up so I can see how it should be, and learn from the correct way.

The script runs how i'd like it to, but fails at rounding the rectangles corners, in applescipt I would use "convert shape given convert to rounded rectangle", in jsx I managaed to have my code round a rectangle as per below.

var myDoc = app.documents.add();
var rect =myDoc.rectangles.add({geometricBounds : [0,0,45,90]}) ;
rect.convertShape(ConvertShapeOptions.CONVERT_TO_ROUNDED_RECTANGLE, 8);

but I cant figure out how to incorporate it intomy other code, which is this

let myInDesign = require("indesign");
let app = myInDesign.app;
//let myLayer = myDoc.layers.item("Layer 1");

//************* Create Page Size *************
var myDoc = app.documents.add();
  with (myDoc.documentPreferences) {
      
pageHeight = "120 mm";
 pageWidth = "235 mm";
  pagesPerDocument = 1;

winHeight = "45 mm";
 winWidth = "90 mm";
  winInLeft = "20 mm";
   winUpBottom = "55 mm";  // up from bottom 20mm //

var marginPreferences = app.marginPreferences
 marginPreferences.bottom = "17mm";
  marginPreferences.left = "10mm";
   marginPreferences.right = "10mm";
    marginPreferences.top = "10mm";

app.activeDocument.zeroPoint = [0, 0];


//************* WINDOW SIZE & POSITION ************* LAYER INDEX 1
windowLayer = "Window";
 targetLayer=myDoc.layers.itemByName(windowLayer);
  myDoc.layers.add ({name:windowLayer});
   targetLayer=myDoc.layers.itemByName(windowLayer);

var rect =myDoc.rectangles.add({geometricBounds : [0,0,winHeight,winWidth] , strokeColor : myDoc.colors.itemByName("Keyline") , strokeWeight : "0.25" , overprintStroke : true ,});
rect.move([winInLeft, winUpBottom]);


rect.convertShape(ConvertShapeOptions.CONVERT_TO_ROUNDED_RECTANGLE, "8");    //************* script stops here.  

;

//************* ENV SIZE ************* LAYER INDEX 2
keylineLayer = "Keyline";
 targetLayer=myDoc.layers.itemByName(keylineLayer);
  myDoc.layers.add ({name:keylineLayer});
   targetLayer=myDoc.layers.itemByName(keylineLayer);
    var rect =myDoc.rectangles.add({geometricBounds : [0,0,pageHeight,pageWidth] , strokeColor : myDoc.colors.itemByName("Keyline") , strokeWeight : "0.25" , overprintStroke : true ,});
;

//});
    
}


Solution

  • Just in case, here is the fixed Kurt's JSX code:

    // Function to create a new document with specified preferences
    function createDocument(w, h) {
        var myDoc = app.documents.add();
        myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
        myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
        myDoc.documentPreferences.pageWidth = w;
        myDoc.documentPreferences.pageHeight = h;
        myDoc.documentPreferences.pagesPerDocument = 1; // ?
        return myDoc;
    }
    
    // Function to add a layer if it doesn't already exist
    function ensureLayer(doc, layerName) {
        var layer = doc.layers.itemByName(layerName);
        if (layer.isValid == false) layer = doc.layers.add({name: layerName});
        return layer;
    }
    
    // Function to create a rectangle with rounded corners
    function createRoundedRectangle(doc, layer, bounds, cornerRadius) {
        var rect = doc.rectangles.add({
            geometricBounds: bounds,
            // strokeColor: doc.colors.itemByName("Keyline"),
            strokeWeight: 0.25,
            overprintStroke: true,
            itemLayer: layer
        });
        rect.convertShape(ConvertShapeOptions.CONVERT_TO_ROUNDED_RECTANGLE, undefined, undefined, cornerRadius);
        return rect;
    }
    
    // Main Script
    (function main() {
        var myDoc = createDocument(235, 120);
        var windowLayer = ensureLayer(myDoc, "Window");
        var keylineLayer = ensureLayer(myDoc, "Keyline");
    
        // Adding window rectangle
        createRoundedRectangle(
            myDoc,
            windowLayer,
            [0, 0, 45, 90],
            8
        ).move([20, 55]);
    
        // Adding keyline rectangle
        createRoundedRectangle(
            myDoc,
            keylineLayer,
            [0, 0, myDoc.documentPreferences.pageHeight, myDoc.documentPreferences.pageWidth],
            0 // No rounding for keyline
        );
    })();
    

    There were the several problems.

    1. Skipped the two arguments undefined, undefined, here:
    rect.convertShape(ConvertShapeOptions.CONVERT_TO_ROUNDED_RECTANGLE, undefined, undefined, cornerRadius);
    

    See the documentation: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Rectangle.html

    enter image description here

    1. If you want to use the color 'Keyline' you have to create the color first, since it's a new document, unlikely it has this color already. Otherwise you will get an error at this line:
    strokeColor: doc.colors.itemByName("Keyline"),
    
    1. This way to create/pick a layer doesn't work:
    try {
        layer = doc.layers.itemByName(layerName);
    } catch (e) {
        layer = doc.layers.add({name: layerName});
    }
    

    The first line will always get you some 'true' value, even if the document has no such layer. And the catch line will newer be activated. You have to check if the layer is a valid layer via the layer.isValid option.

    Also:

    InDesign can use current measurement units. You don't have to calculate them every time from the points (as in Illustrator). You can set the units for the document and use them.

    myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    

    These lines are from UXP-scripts:

    const myInDesign = require("indesign");
    const app = myInDesign.app;
    

    InDesign 2014 can't run UXP-scripts (if my memory serves me right). I don't know, perhaps it gets you no error. But it's just redundant for JSX-scripts anyway.