0

I am totally new to javascript and extended script. I made a lot of tutorial to try to learn the right method and syntax but for several weeks I can't create the script I want.

The script is quite simple, it is a script which displays a GUI window (see attached image) and which asks to choose between two types of document (with or without margin) and which asks for the size of the file to the user.

I have already created this little code but, being a beginner, I don't know if I am using the right method and the right syntax.

Could a charitable soul tell me if I'm on the right track, maybe give me some advice or anything else that would be useful to me.

I did a lot of research but I can't find any example code that could help me do what I want.

GUI of my Illustrator script

My code :


function theScript() {
///////////////////////////////////////////////////
// BEGIN THE SCRIPT
//=================================================


function mainDialog() {
// DIALOG
// ======
var dialog = new Window("dialog", undefined, undefined, {closeButton: false});
    dialog.text = "Document Creator";
    dialog.orientation = "column";
    dialog.alignChildren = ["left","top"];
    dialog.spacing = 10;
    dialog.margins = 16;

// GLOBALCONTAINER
// ===============
var globalContainer = dialog.add("group", undefined, {name: "globalContainer"});
    globalContainer.orientation = "row";
    globalContainer.alignChildren = ["left","top"];
    globalContainer.spacing = 10;
    globalContainer.margins = 0;

// OPTIONPANEL
// ===========
var optionPanel = globalContainer.add("panel", undefined, undefined, {name: "optionPanel"});
    optionPanel.text = "Document Creator Options";
    optionPanel.orientation = "column";
    optionPanel.alignChildren = ["left","top"];
    optionPanel.spacing = 10;
    optionPanel.margins = 20;

// ARTWORKOPTIONGROUP
// ==================
var artworkOptionGroup = optionPanel.add("group", undefined, {name: "artworkOptionGroup"});
    artworkOptionGroup.orientation = "column";
    artworkOptionGroup.alignChildren = ["left","top"];
    artworkOptionGroup.spacing = 10;
    artworkOptionGroup.margins = 0;
    artworkOptionGroup.alignment = ["fill","top"];

// ARTWORKOPTIONPANEL
// ==================
var artworkOptionPanel = artworkOptionGroup.add("panel", undefined, undefined, {name: "artworkOptionPanel"});
    artworkOptionPanel.text = "Artwork Template";
    artworkOptionPanel.orientation = "column";
    artworkOptionPanel.alignChildren = ["left","top"];
    artworkOptionPanel.spacing = 10;
    artworkOptionPanel.margins = [10,16,10,10];
    artworkOptionPanel.alignment = ["fill","top"];

var artworkFull = artworkOptionPanel.add("radiobutton", undefined, undefined, {name: "artworkFull"});
    artworkFull.text = "Artwork Full";
    artworkFull.value = true;
    artworkFull.alignment = ["fill","top"];

var artworkSimple = artworkOptionPanel.add("radiobutton", undefined, undefined, {name: "artworkSimple"});
    artworkSimple.text = "Artwork Simple";
    artworkSimple.alignment = ["fill","top"];

// ARTWORKSIZEPANEL
// ================
var artworkSizePanel = optionPanel.add("panel", undefined, undefined, {name: "artworkSizePanel"});
    artworkSizePanel.text = "Artwork Size";
    artworkSizePanel.orientation = "column";
    artworkSizePanel.alignChildren = ["left","top"];
    artworkSizePanel.spacing = 10;
    artworkSizePanel.margins = [10,16,10,10];
    artworkSizePanel.alignment = ["fill","top"];

// ARTWORKSIZEGROUP
// ================
var artworkSizeGroup = artworkSizePanel.add("group", undefined, {name: "artworkSizeGroup"});
    artworkSizeGroup.orientation = "row";
    artworkSizeGroup.alignChildren = ["left","center"];
    artworkSizeGroup.spacing = 10;
    artworkSizeGroup.margins = 0;

var widthLabel = artworkSizeGroup.add("statictext", undefined, undefined, {name: "widthLabel"});
    widthLabel.text = "Width";
    widthLabel.alignment = ["left","fill"];

var width_artwork = artworkSizeGroup.add('edittext {properties: {name: "width_artwork"}}');
    width_artwork.text = "1000";
    width_artwork.preferredSize.width = 50;

var xLabel = artworkSizeGroup.add("statictext", undefined, undefined, {name: "xLabel"});
    xLabel.text = "x";

var heightLabel = artworkSizeGroup.add("statictext", undefined, undefined, {name: "heightLabel"});
    heightLabel.text = "Height";
    heightLabel.preferredSize.width = 50;

var height_artwork = artworkSizeGroup.add('edittext {properties: {name: "height_artwork"}}');
    height_artwork.text = "1000";

// BUTTONGROUP
// ===========
var buttonGroup = dialog.add("group", undefined, {name: "buttonGroup"});
    buttonGroup.orientation = "row";
    buttonGroup.alignChildren = ["left","top"];
    buttonGroup.spacing = 10;
    buttonGroup.margins = [0,10,0,0];
    buttonGroup.alignment = ["right","top"];

var ok_button = buttonGroup.add("button", undefined, undefined, {name: "ok_button"});
    ok_button.text = "Create";

var cancel_button = buttonGroup.add("button", undefined, undefined, {name: "cancel_button"});
    cancel_button.text = "Cancel";

// ITEM REFERENCE LIST ( Info: http://jongware.mit.edu/Sui/index_1.html )
dialog.items = {
  globalContainer: globalContainer, // group
  optionPanel: optionPanel, // panel
  artworkOptionGroup: artworkOptionGroup, // group
  artworkOptionPanel: artworkOptionPanel, // panel
  artworkFull: artworkFull, // radiobutton
  artworkSimple: artworkSimple, // radiobutton
  artworkSizePanel: artworkSizePanel, // panel
  artworkSizeGroup: artworkSizeGroup, // group
  widthLabel: widthLabel, // statictext
  width_artwork: width_artwork, // edittext
  xLabel: xLabel, // statictext
  heightLabel: heightLabel, // statictext
  height_artwork: height_artwork, // edittext
  buttonGroup: buttonGroup, // group
  ok_button: ok_button, // button
  cancel_button: cancel_button // button
};
dialog.itemsArray = [globalContainer, optionPanel, artworkOptionGroup, artworkOptionPanel, artworkFull, artworkSimple, artworkSizePanel, artworkSizeGroup, widthLabel, width_artwork, xLabel, heightLabel, height_artwork, buttonGroup, ok_button, cancel_button];

  dialog.callback = function(type, formData) {
    if (type === "continue"){
        var artworkFull;
        var artworkSimple;
        var width_artwork;
        var height_artwork;

      // Do something with formData values here
        var cLen = formData.controls.length;
        var formValues = '';
        for (var c = 0; c < cLen; c++ )
        {
            if (formData.controls[c].name === "artworkFull") {
                full = formData.controls[c].value;
            }
            if (formData.controls[c].name === "artworkSimple") {
                simple = formData.controls[c].value;
            }
            if (formData.controls[c].name === "width_artwork") {
                width = formData.controls[c].value;
            }
            if (formData.controls[c].name === "height_artwork") {
                height = formData.controls[c].value;
            }
        }
      takeMeasurement(args.DOC_REF, full, simple, width, height);
    }
    else {
      // Cancel
      return false;
    }
  }

  dialog.show();

  return dialog;

}());

var createSimpleDoc = function(simple) {
  try {
    // Create a new document
    var aiDocPreset = new DocumentPreset();
        aiDocPreset.units = RulerUnits.Inches;
        aiDocPreset.width = width_artwork;
        aiDocPreset.height = height_artwork;
        aiDocPreset.rasterResolution = DocumentRasterResolution.HighResolution;
        aiDocPreset.title = "Simple Document";
        aiDocPreset.colorMode = DocumentColorSpace.CMYK;

    var doc = app.documents.addDocument("Simple Document Preset", aiDocPreset);
  }

  catch(err) {

    if () {

    }
    else if () {

    }
    else {
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.message + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }
  }
}

var createFullDoc = function(full) {
  try {
    // Create a new document
    var aiDocPreset = new DocumentPreset();
        aiDocPreset.units = RulerUnits.Inches;
        aiDocPreset.width = width_artwork + 10;
        aiDocPreset.height = height_artwork+ 10;
        aiDocPreset.rasterResolution = DocumentRasterResolution.HighResolution;
        aiDocPreset.title = "Full Document";
        aiDocPreset.colorMode = DocumentColorSpace.CMYK;

    var doc = app.documents.addDocument("Full Document Preset", aiDocPreset);
  }

  catch(err) {

    if () {

    }
    else if () {

    }
    else {
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.message + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }
  }
}

//=================================================
// END THE SCRIPT
///////////////////////////////////////////////////
}

theScript();

2
  • Confusing... -- "I can't create the script I want" -- then you show a script which appears to be working based upon the question (I didn't try the script). There's no mention of what you "can't create". I'd also ask, if this the actual script? Seems this can all be handled by creating a couple presets in the New Document window and there's no real need for a custom script merely to have 2 document setups - But if you are just doing this to learn.. I understand.
    – Scott
    Commented Jun 29, 2023 at 17:22
  • Thank you @Scott for your reply, even if it help me not too much 😉 ! And, YES, it's only for fun and to learn the "how to…" ! A simple training and entertainment
    – GilbertOOl
    Commented Jun 30, 2023 at 4:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.