Scripting shape creation
Copy link to clipboard
Copied
Could anyone direct me as to how to get started on and go about writing a script to have InDesign generate a file that would have one solid-line, non-filled rectangle drawn for a user-entered Finished size of a graphic (e.g., 24" x 36"), and one dashed line, non-filled rectangle drawn for a 1" Bleed border (e.g., 25" x 37")?
Thanks!!!
Copy link to clipboard
Copied
Moving to InDesign Scripting forum
Copy link to clipboard
Copied
Hello Stefan,
Try the code below for you needs.
Thanks to Shonkyin for creating the dialog on this post Create a new document from a dialog.
var UIresult = myDisplayDialog();
var myDocument = app.documents.add();
with(myDocument.documentPreferences){
pageWidth = UIresult[0] + "inches";
pageHeight = UIresult[1] + "inches";
//~ pageOrientation = PageOrientation.landscape;
pagesPerDocument = 1;
}
var doc = app.documents[0];
doc.properties = {
viewPreferences:{
horizontalMeasurementUnits: +MeasurementUnits.INCHES,
verticalMeasurementUnits: +MeasurementUnits.INCHES,
},
documentPreferences: {
documentBleedBottomOffset: ".5 in",
documentBleedTopOffset: ".5 in",
documentBleedInsideOrLeftOffset: ".5 in",
documentBleedOutsideOrRightOffset: ".5 in",
}
}
var page = doc.pages.item(0);
page.marginPreferences.properties = {
top : 0,
left: 0,
right: 0,
bottom: 0
}
frame = doc.rectangles.add ({geometricBounds:["-0.5in","-0.5in", UIresult[1] + ".5in", UIresult[0] + ".5in"], contentType:ContentType.GRAPHIC_TYPE});
frame.fillColor = "None";
frame.strokeWeight = "2.5pt";
frame.strokeType = "Dashed";
frame.strokeColor = "Black";
frame = doc.rectangles.add ({geometricBounds:[".0in",".0in", UIresult[1] + "in", UIresult[0] + "in"], contentType:ContentType.GRAPHIC_TYPE});
frame.fillColor = "None";
frame.strokeWeight = "2.5pt";
frame.strokeType = "Solid";
frame.strokeColor = "Black";
function myDisplayDialog()
{
var myDialog = new Window ('dialog', 'Create New Document');
myDialog.alignChildren = "left";
var rg0 = myDialog.add ('group');
rg0.add('statictext',[-95, 0, 45, 25], 'Enter Document Width:');
var docuWidth = rg0.add('edittext',[-10, 0, 45, 25], '');
rg0.add('statictext',undefined, 'inches');
var rg1 = myDialog.add ('group');
rg1.add('statictext',undefined, 'Enter Document Height:');
var docuHeight = rg1.add('edittext',[-10, 0, 45, 25], '');
rg1.add('statictext',undefined, 'inches');
var rg2 = myDialog.add ('group');
rg2.alignment = "right";
rg2.add('button', undefined, 'Cancel', {name: 'cancel'});
rg2.add('button', undefined, 'OK', {name: 'ok'});
var myResult = myDialog.show();
if (myResult == 1)
{
return [docuWidth.text, docuHeight.text];
}
if (myResult == 2)
{
exit();
}
}
Regards,
Mike
Copy link to clipboard
Copied
Great thanks Mike. I'll give this a try.
Copy link to clipboard
Copied
One thing you need to know is that all shapes in InDesign are based on what is known as geometric bounds. Bounds is a list that measurs from top left in the order of topY, leftX, bottomY, rightX. The following is a simple script written in AppleScript. It assumes that there is a document open with page size larger than the rectangles to be created. The geometric bounds of the rectangles are calculated from the center of the page. You will want to experiment with the weights of the strokes. A dashed stroke in InDesign can be either "Dashed", "Dashed (3 and 2)", or "Dashed (4 and 4)".
--measurements are in inches
set rectWid to 36
set rectHgt to 24
set bleedWid to 37
set bleedHgt to 25
set sWgt1 to 0.12 --stroke weight for inside rectangle
set sWgt2 to 0.06 --stroke weight for bleed
set sColor to "Black" --stroke color
set sType1 to "Solid" --stroke type for inside rectangle
set sType2 to "Dashed (4 and 4)" --stroke type for bleed
set sFill to "None"
--assumes page is larger than largest rectangle
set insideRect to rectFromCenter(rectWid, rectHgt, sWgt1, sColor, sType1, sFill)
set outsideRec to rectFromCenter(bleedWid, bleedHgt, sWgt2, sColor, sType2, sFill)
(*Creates rectangle from center given width, height, stroke and fill parameters*)
on rectFromCenter(wid, hgt, sWgt, sColor, sType, sFill)
tell application "Adobe InDesign CC 2019"
set pageRef to page 1 of document 1
set pageBounds to bounds of pageRef
copy pageBounds to {y0, x0, y1, x1}
set cx to x0 + ((x1 - x0) / 2)
set cy to y0 + ((y1 - y0) / 2)
set gBounds to {cy - (hgt / 2), cx - (wid / 2), cy + (hgt / 2), cx + (wid / 2)}
set measurement unit of script preferences to inches
tell page 1 of document 1
set myRect to make rectangle with properties {geometric bounds:gBounds, fill color:sFill, stroke color:sColor, stroke weight:sWgt, stroke type:sType}
end tell
end tell
return myRect
end rectFromCenter
Copy link to clipboard
Copied
Hi S Hopkins,
Where do I actually paste this Apple script?
Thanks!

