Skip to main content
Inspiring
January 23, 2023
Question

Is there a way to convert actions into a single script?

  • January 23, 2023
  • 1 reply
  • 282 views

Is there a way to convert these 3 actions into a Script?  I would choose which of the 3 to run based on a Radio Button Selection. (NA, 4 Corners, 2 Vertical, 2 Horizontal)

 

 



NA button would do nothing

4 Corners Button 

    - Select Direction Handles of top most path

    - Clear

    - Apply Graphic Style by Name (this would be part of a Dropdown menu selection)

2 Vertical Button

    - Select Direction Handles of top most path

    - Clear

    - Alignment - Horizontal Align Center
    - Apply Graphic Style by Name (this would be part of a Dropdown menu selection)

    - Expand Appearance

    - Pathfinder - Add

2 Horizontal Button

    - Select Direction Handles of top most path

    - Clear

    - Alignment - Vertical Align Center
    - Apply Graphic Style by Name (this would be part of a Dropdown menu selection)

    - Expand Appearance

    - Pathfinder - Add

 

 

 

Here is the code as it exists today...   Ugly but Functioning

var doc = app.activeDocument;
var myLayer = doc.activeLayer;

// Create the window
var win = new Window("dialog", "Path Dimensions");

// Create a group to hold the width and height fields
var dimGroup = win.add("group");

// Add the width and height input fields
dimGroup.add("statictext", undefined, "Width (inches):");
var widthField = dimGroup.add("edittext", undefined, 8);
//widthField.text = 5;
widthField.preferredSize.width = 50;
dimGroup.add("statictext", undefined, "Height (inches):");
var heightField = dimGroup.add("edittext", undefined, 10);
//heightField.text = 5;
heightField.preferredSize.width = 50;

win.add("statictext", undefined, "Offset (inches):");
var offsetField = win.add("edittext", undefined, -0.125);
//offsetField.text = -0.125;
offsetField.preferredSize.width = 50;

win.add("statictext", undefined, "Offset (inches):");
var offsetField2 = win.add("edittext", undefined, 0);
//offsetField2.text = "";
offsetField2.preferredSize.width = 50;

win.add("statictext", undefined, "Offset (inches):");
var offsetField3 = win.add("edittext", undefined, 0);
//offsetField3.text = "";
offsetField3.preferredSize.width = 50;

win.add("statictext", undefined, "Offset (inches):");
var offsetField4 = win.add("edittext", undefined, 0);
//offsetField4.text = "";
offsetField4.preferredSize.width = 50;

win.add("statictext", undefined, "Offset For Stud Placement (inches):");
var offsetField5 = win.add("edittext", undefined, 0);
//offsetField5.text = "";
offsetField5.preferredSize.width = 50;

// Add the radio buttons
var radioGroup = win.add("group");
var rectButton = radioGroup.add("radiobutton", undefined, "Rectangle");
rectButton.value = true;
var ellipseButton = radioGroup.add("radiobutton", undefined, "Elliptical");

// Add the Stud radio buttons
var studGroup = win.add("group");
win.add("statictext", undefined, "Stud Placement");
var naButton = studGroup.add("radiobutton", undefined, "N/A");
naButton.value = true;
var fourstudButton = studGroup.add("radiobutton", undefined, "4 Studs");
var twohstudButton = studGroup.add("radiobutton", undefined, "2 Horizontal");
var twovstudButton = studGroup.add("radiobutton", undefined, "2 Vertical");


// Add the OK and cancel buttons
var btnGroup = win.add("group");
btnGroup.add ("statictext", undefined, "Select Stud Size");
var lines = btnGroup.add("dropdownlist",undefined,["#4", "#6", "#8", "#10", "#12", "#14", "1/4-20"]);

btnGroup.add("button", undefined, "OK", { name: "ok" });
btnGroup.add("button", undefined, "Cancel", { name: "cancel" });

// Check if the OK button was clicked
if (win.show() == 1) {
  // Get the width, height, and offset values
  var wdth = widthField.text * 72;
  var hdth = heightField.text * 72;
  var offset = offsetField.text * 72;
  var offset2 = (parseFloat(offsetField2.text) + parseFloat(offsetField.text)) * 72;
  var offset3 = (parseFloat(offsetField3.text) + parseFloat(offsetField2.text) + parseFloat(offsetField.text)) * 72;
  var offset4 = (parseFloat(offsetField4.text) + parseFloat(offsetField3.text) + parseFloat(offsetField2.text) + parseFloat(offsetField.text)) * 72;
  var offset5 = (parseFloat(offsetField5.text) + parseFloat(offsetField4.text) + parseFloat(offsetField3.text) + parseFloat(offsetField2.text) + parseFloat(offsetField.text)) * 72;
  /*
  alert(offsetField.text);
  alert(offset);
  alert(offsetField2.text);
  alert(offset2);
  alert(offsetField3.text);
  alert(offset3);
  alert(offsetField4.text);
  alert(offset4);
  alert(offsetField5.text);
  alert(offset5);
*/
 
  // Create the path
  var pathTop = -1000;
  var pathLeft = 1000;
  var path = rectButton.value
    ? myLayer.pathItems.rectangle(pathTop, pathLeft, wdth, hdth)
    : myLayer.pathItems.ellipse(pathTop, pathLeft, wdth, hdth);

  // Set path colors
  var pathColor = new CMYKColor();
  pathColor.cyan = 100;
  pathColor.magenta = 100;
  pathColor.yellow = 100;
  pathColor.black = 100;
  path.stroked = false;
  path.fillColor = pathColor;

  // Create Offset Path
  if (offset != 0) {
    var offsetPath = rectButton.value
      ? myLayer.pathItems.rectangle(
          pathTop + offset,
          pathLeft - offset,
          wdth + offset * 2,
          hdth + offset * 2
        )
      : myLayer.pathItems.ellipse(
          pathTop + offset,
          pathLeft - offset,
          wdth + offset * 2,
          hdth + offset * 2
        );
  // Create Offset2 Path
  if (offset2 < offset) {
    var offsetPath2 = rectButton.value
    ? myLayer.pathItems.rectangle(
        pathTop + offset2,
        pathLeft - offset2,
        wdth + offset2 * 2,
        hdth + offset2 * 2
      )
    : myLayer.pathItems.ellipse(
        pathTop + offset2,
        pathLeft - offset2,
        wdth + offset2 * 2,
        hdth + offset2 * 2
      );
  // Create offset3 Path
  if (offset3 < offset2) {
    var offsetPath3 = rectButton.value
    ? myLayer.pathItems.rectangle(
        pathTop + offset3,
        pathLeft - offset3,
        wdth + offset3 * 2,
        hdth + offset3 * 2
      )
    : myLayer.pathItems.ellipse(
        pathTop + offset3,
        pathLeft - offset3,
        wdth + offset3 * 2,
        hdth + offset3 * 2
      );

  // Create offset4 Path
  if (offset4 < offset3) {
        var offsetPath4 = rectButton.value
        ? myLayer.pathItems.rectangle(
            pathTop + offset4,
            pathLeft - offset4,
            wdth + offset4 * 2,
            hdth + offset4 * 2
          )
        : myLayer.pathItems.ellipse(
            pathTop + offset4,
            pathLeft - offset4,
            wdth + offset4 * 2,
            hdth + offset4 * 2
          );

  // Create offset5 Path
  if (offset5 != 0) {
      var offsetPath5 = rectButton.value
      ? myLayer.pathItems.rectangle(
              pathTop + offset5,
              pathLeft - offset5,
              wdth + offset5 * 2,
              hdth + offset5 * 2
            )
          : myLayer.pathItems.ellipse(
              pathTop + offset5,
              pathLeft - offset5,
              wdth + offset5 * 2,
              hdth + offset5 * 2
            );

    // Enable the following line if you want the offset path below the path
    // offsetPath.move(path, ElementPlacement.PLACEAFTER);

    // Set offset path color
    var offsetColor = new CMYKColor();
    offsetColor.cyan = 0;
    offsetColor.magenta = 0;
    offsetColor.yellow = 0;
    offsetColor.black = 100;
    offsetPath.stroked = false;
    offsetPath.fillcolor = offsetColor;
      }
} else {
  alert("No path created");
}
}
}
  }
}

 

 

This topic has been closed for replies.

1 reply

Community Manager
January 24, 2023

Hello @BryanPagenkopf,

 

Thanks for reaching out. Would you mind trying the suggestions shared in this community post (https://community.adobe.com/t5/illustrator-discussions/can-i-make-a-button-to-run-a-script/td-p/10043240) and checking if it helps?

 

Looking forward to hearing from you.

 

Thanks,

Anubhav