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.
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();