Skip to content

Instantly share code, notes, and snippets.

@gepatto
Created February 24, 2024 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gepatto/9677e6291240b55609a52274db6b7a6f to your computer and use it in GitHub Desktop.
Save gepatto/9677e6291240b55609a52274db6b7a6f to your computer and use it in GitHub Desktop.
function cmToPoints(cm) { return cm * 28.3464567; }
function pointsToCm(pt) { return pt * 0.035277776; }
var names = ["Foo", "Bar"];
//
var doc;
var layer;
var docWidth = cmToPoints(80);
var docHeight = cmToPoints(45);
var areaWidth = 29;
var areaHeight = 40;
var rectangleWidth = 5;
var rectangleHeight = 2.5;
// deze zijn (nog) in points
var cornerRadius = 12;
var spacingHorizontal = 8;
var spacingVertical = 8;
var docMarginX = 8;
var docMarginY = 8;
var trimNames = false;
var noColor = new NoColor();
var red = new RGBColor(); red.red = 255;red.green = 0;red.blue = 0;
var fontIndex = -1;
var fontSize = 24;
var fontNames_array = [];
var fontName = "Nobel-Bold";
var hasWarning = false;
// fill fontNames Array
for(var t=0 ; t< app.textFonts.length; t++){
fontNames_array.push(app.textFonts[t].name);
if(fontNames_array[t] == fontName){
fontIndex = t;
}
}
if(fontIndex==-1){
fontName = app.textFonts[0];
fontIndex = 0;
}
function trim(s){
return String(s).replace(/^\s+|\s+$/gm,'');
}
function createBadges(){
var cols = Math.floor(areaWidth / (rectangleWidth + spacingHorizontal));
var tf;
if(fontName!="" && fontName!=undefined){
tf = app.textFonts.getByName(fontName);
}
if (tf == null) {
tf = app.textFonts[0];
}
names = txtNames.text.split(",");
for (var i = 0; i < names.length; i++) {
var badgeGroup = app.activeDocument.groupItems.add();
badgeGroup.name = trim(names[i]) + "_badge";
var xPos = docMarginX + (i%cols)*(rectangleWidth+spacingVertical);
var yPos = docHeight - docMarginY - (Math.floor(i/cols) * (rectangleHeight+spacingHorizontal)) ;
var roundRect = badgeGroup.pathItems.roundedRectangle( yPos , xPos , rectangleWidth, rectangleHeight, cornerRadius, cornerRadius, false);
roundRect.stroked = true;
roundRect.strokeWidth = 0.01;
roundRect.strokeColor = red;
roundRect.fillColor = noColor;
var myTextFrame = badgeGroup.textFrames.add();
myTextFrame.contents = chkTrimSpaces.value?trim(names[i]):names[i];
myTextFrame.textRange.characterAttributes.textFont = tf;
myTextFrame.textRange.characterAttributes.size = fontSize;
myTextFrame.position = [xPos + (rectangleWidth - myTextFrame.width) * .5, yPos - (rectangleHeight - myTextFrame.height) * .5];
if( (myTextFrame.width > rectangleWidth || myTextFrame.height > rectangleHeight )&& !hasWarning){
hasWarning = true;
var myWarning = doc.textFrames.add();
myWarning.contents = "Warning: At least one text is larger than the container"
myWarning.textRange.characterAttributes.size = 48;
myWarning.textRange.characterAttributes.fillColor = red;
myWarning.position = [16,56];
alert( 'Warning \nName: \'' + myTextFrame.contents +'\' is larger than the container');
}
}
}
// CREATE DIALOG UI
// ======
var dialog = new Window("dialog");
dialog.text = "Badge Creator";
//ialog.orientation = "column";
dialog.alignChildren = ["center","top"];
dialog.spacing = 16;
dialog.margins = 16;
// MAINGROUP
// =========
var mainGroup = dialog.add("group", undefined, {name: "mainGroup"});
mainGroup.orientation = "column";
mainGroup.alignChildren = ["left","center"];
mainGroup.spacing = 8;
mainGroup.margins = 4;
// DIMENSIONSGROUP
// ===============
var dimensionsGroup = mainGroup.add("group", undefined, {name: "dimensionsGroup"});
dimensionsGroup.orientation = "row";
dimensionsGroup.alignChildren = ["center","center"];
dimensionsGroup.spacing = 10;
dimensionsGroup.margins = 4;
dimensionsGroup.alignment = ["fill","center"];
// BADGEPANEL
// ==========
var BadgePanel = dimensionsGroup.add("panel", undefined, undefined, {name: "BadgePanel"});
BadgePanel.text = "Badge";
BadgePanel.preferredSize.width = 201;
BadgePanel.orientation = "column";
BadgePanel.alignChildren = ["right","top"];
BadgePanel.spacing = 10;
BadgePanel.margins = 16;
BadgePanel.alignment = ["center","fill"];
// BADGEWIDTHROW
// =============
var badgewidthrow = BadgePanel.add("group", undefined, {name: "badgewidthrow"});
badgewidthrow.orientation = "row";
badgewidthrow.alignChildren = ["left","center"];
badgewidthrow.spacing = 10;
badgewidthrow.margins = 0;
var lblBadgewidth = badgewidthrow.add("statictext", undefined, undefined, {name: "lblBadgewidth"});
lblBadgewidth.text = "Width (cm)";
var txtBadgeWidth = badgewidthrow.add('edittext {properties: {name: "badgeWidth"}}');
txtBadgeWidth.text = rectangleWidth;
txtBadgeWidth.preferredSize.width = 63;
// BADGEHEIGHTROW
// ==============
var badgeheightrow = BadgePanel.add("group", undefined, {name: "badgeheightrow"});
badgeheightrow.orientation = "row";
badgeheightrow.alignChildren = ["left","center"];
badgeheightrow.spacing = 10;
badgeheightrow.margins = 0;
var lblBadgeheight = badgeheightrow.add("statictext", undefined, undefined, {name: "lblBadgeheight"});
lblBadgeheight.text = "Height (cm)";
var txtBadgeHeight = badgeheightrow.add('edittext {properties: {name: "badgeHeight"}}');
txtBadgeHeight.text = rectangleHeight;
txtBadgeHeight.preferredSize.width = 62;
// BADGERADIUSROW
// ==============
var badgeradiusrow = BadgePanel.add("group", undefined, {name: "badgeradiusrow"});
badgeradiusrow.orientation = "row";
badgeradiusrow.alignChildren = ["left","center"];
badgeradiusrow.spacing = 10;
badgeradiusrow.margins = 0;
var lblCornerradius = badgeradiusrow.add("statictext", undefined, undefined, {name: "lblCornerradius"});
lblCornerradius.text = "Corner Radius (points)";
var txtCornerRadius = badgeradiusrow.add('edittext {properties: {name: "cornerRadius"}}');
txtCornerRadius.text = cornerRadius;
txtCornerRadius.preferredSize.width = 65;
// PANELAREA
// =========
var PanelArea = dimensionsGroup.add("panel", undefined, undefined, {name: "PanelArea"});
PanelArea.text = "Cut Area";
PanelArea.orientation = "column";
PanelArea.alignChildren = ["right","top"];
PanelArea.spacing = 10;
PanelArea.margins = 16;
PanelArea.alignment = ["center","fill"];
// AWIDTH
// ======
var awidth = PanelArea.add("group", undefined, {name: "awidth"});
awidth.orientation = "row";
awidth.alignChildren = ["left","center"];
awidth.spacing = 10;
awidth.margins = 0;
var lblAreawidth = awidth.add("statictext", undefined, undefined, {name: "lblAreawidth"});
lblAreawidth.text = "Width (cm)";
var txtAreaWidth = awidth.add('edittext {justify: "right", properties: {name: "areaWidth"}}');
txtAreaWidth.text = areaWidth
txtAreaWidth.preferredSize.width = 62;
// AHEIGHT
// =======
var aHeight = PanelArea.add("group", undefined, {name: "aHeight"});
aHeight.orientation = "row";
aHeight.alignChildren = ["left","center"];
aHeight.spacing = 10;
aHeight.margins = 0;
var lblAreaheight = aHeight.add("statictext", undefined, undefined, {name: "lblAreaheight"});
lblAreaheight.text = "Height (cm)";
var txtAreaHeight = aHeight.add('edittext {justify: "right", properties: {name: "areaHeight"}}');
txtAreaHeight.text = areaHeight;
txtAreaHeight.preferredSize.width = 62;
// TEXTGROUP
// =========
var textGroup = mainGroup.add("group", undefined, {name: "textGroup"});
textGroup.orientation = "column";
textGroup.alignChildren = ["center","center"];
textGroup.spacing = 12;
textGroup.margins = 0;
textGroup.alignment = ["fill","center"];
// TEXTPANEL
// =========
var textPanel = textGroup.add("panel", undefined, undefined, {name: "textPanel"});
textPanel.text = "Names";
textPanel.orientation = "column";
textPanel.alignChildren = ["fill","top"];
textPanel.spacing = 10;
textPanel.margins = 16;
textPanel.alignment = ["fill","center"];
//var lblNames = textPanel.add("statictext", undefined, undefined, {name: "lblNames"});
// lblNames.text = "Names";
var txtNames = textPanel.add('edittext {properties: {name: names , multiline: true, scrollable: true }}');
txtNames.helpTip = "comma separated names ";
txtNames.text = names.join(",")
txtNames.preferredSize.height = 64;
var chkTrimSpaces = textPanel.add("checkbox", undefined, undefined, {name: "trimSpaces"});
chkTrimSpaces.text = "Trim Spaces";
chkTrimSpaces.value = true;
// FONTPANEL
// =========
var fontPanel = textGroup.add("panel", undefined, undefined, {name: "fontPanel"});
fontPanel.text = "Font";
fontPanel.orientation = "column";
fontPanel.alignChildren = ["fill","top"];
fontPanel.spacing = 10;
fontPanel.margins = 16;
fontPanel.alignment = ["fill","center"];
var lblFontFamily = fontPanel.add("statictext", undefined, undefined, {name: "lblFontFamily"});
lblFontFamily.text = "FontFamily";
var lstFontNames = fontPanel.add("dropdownlist", undefined, undefined, {name: "fontName", items: fontNames_array});
lstFontNames.selection = fontIndex;
fontName = lstFontNames.selection;
lstFontNames.onChange = function(){
fontName = lstFontNames.selection;
}
// FONTSIZEGROUP
// =============
var fontsizeGroup = fontPanel.add("group", undefined, {name: "fontsizeGroup"});
fontsizeGroup.orientation = "row";
fontsizeGroup.alignChildren = ["right","center"];
fontsizeGroup.spacing = 10;
fontsizeGroup.alignment = ["left","top"];
var lblFontsize = fontsizeGroup.add("statictext", undefined, undefined, {name: "lblFontsize"});
lblFontsize.text = "Font Size (points)";
var txtFontSize = fontsizeGroup.add('edittext {properties: {name: "fontSize"}}');
txtFontSize.text = fontSize;
txtFontSize.preferredSize.width = 65;
txtFontSize.alignment = ["right","center"];
// DOCUMENTGROUP
// =============
var documentGroup = mainGroup.add("group", undefined, {name: "documentGroup"});
documentGroup.orientation = "column";
documentGroup.alignChildren = ["center","center"];
documentGroup.spacing = 10;
documentGroup.margins = 0;
documentGroup.alignment = ["fill","center"];
// DOCUMENT
// ========
var Document = documentGroup.add("panel", undefined, undefined, {name: "Document"});
Document.text = "Document";
Document.preferredSize.width = 400;
Document.orientation = "column";
Document.alignChildren = ["left","top"];
Document.spacing = 10;
Document.margins = 17;
Document.alignment = ["fill","center"];
var chkCreateDocument = Document.add("checkbox", undefined, undefined, {name: "createDocument"});
chkCreateDocument.text = "Create New Document";
chkCreateDocument.onClick = function(){
docsize.enabled = chkCreateDocument.value;
}
var docsize_array = ["80x45","40x30"];
var docsize = Document.add("dropdownlist", undefined, undefined, {name: "docsize", items: docsize_array});
docsize.enabled = false;
docsize.selection = 0;
docsize.onChange = function(){
var s = String(docsize.selection).split('x');
docWidth = cmToPoints(s[0]);
docHeight = cmToPoints(s[1]);
}
// BTNGROUP
// ========
var btnGroup = mainGroup.add("group", undefined, {name: "btnGroup"});
btnGroup.orientation = "column";
btnGroup.alignChildren = ["center","center"];
btnGroup.spacing = 10;
btnGroup.margins = 0;
btnGroup.alignment = ["center","center"];
var btnCreate = btnGroup.add("button", undefined, undefined, {name: "btnCreate"});
btnCreate.text = "Create";
btnCreate.onClick = function(){
areaWidth = cmToPoints(parseFloat(txtAreaWidth.text));
areaHeight = cmToPoints(parseFloat(txtAreaHeight.text));
rectangleWidth = cmToPoints(parseFloat(txtBadgeWidth.text));
rectangleHeight = cmToPoints(parseFloat(txtBadgeHeight.text));
cornerRadius = parseFloat(txtCornerRadius.text);
fontSize = parseFloat(txtFontSize.text);
names = txtNames.text.split(',');
if (app.documents.length == 0 || (chkCreateDocument.value) ) {
doc = app.documents.add(DocumentColorSpace.RGB, docWidth, docHeight);
} else {
doc = app.activeDocument;
}
layer = doc.layers[0];
createBadges();
dialog.close();
}
dialog.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment