Copy link to clipboard
Copied
Any recommendations on how to simplify this Script?
var doc = app.activeDocument;
var choice;
var strEnter = doc.name; //alert(strEnter);
var soNum = strEnter.slice(0,7); //alert(soNum);
var sc = "/";
//Creates new Popup
var Pallette = new Window ("dialog", "Save Precision Tooled Art Files");
Pallette.add ("statictext", undefined, "Pick Production File");
Pallette.orientation = "row";
//Dropdown Menu
var myDropdown = Pallette.add ("dropdownlist", undefined, [
"Tooled",
"Cast",
"Etched",
"UV Print",
"Ceramic Insert",
"Laser Engraving"
]);
var myButtonGroup = Pallette.add ("group");
myButtonGroup.orientation = "column";
myDropdown.selection = 0;
var btnCreate = myButtonGroup.add ("button", undefined, "OK");
btnCreate.onClick = function () {
choice = myDropdown.selection.text;
w.button.onClick = function () {
w.close();}
};
Pallette.show ();
//
//
//
if (choice == "Tooled") {
var folderString = "G:" + sc + "Layout" + sc + "Plaques_Layout" + sc + "Precision Tooled" + sc;
var saveString = (folderString + soNum + ".eps"); //alert(saveString);
var NamePreset = 'Illustrator 2020';
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === fals
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?") === true) {
doc.saveAs(newFile, saveOpts);
doc.close;}
}
if (newFile.exists === false) {
var saveString = (folderString + soNum + ".eps");
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);
doc.close;}
//
//
//
} else if (choice == "Cast") {
var folderString = "M:" + sc + "440IAPLAQUES" + sc;
var saveString = (folderString + soNum + ".eps"); //alert(saveString);
var NamePreset = 'Illustrator 2020';
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?") === true) {
doc.saveAs(newFile, saveOpts);
doc.close;}
}
if (newFile.exists === false) {
var saveString = (folderString + soNum + ".eps");
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);
doc.close;
//
//
//
} else if (choice == "Etched") {
var folderString = "G:" + sc + "Layout" + sc + "Plaques_Layout" + sc + "Etched" + sc;
var saveString = (folderString + soNum + ".eps"); //alert(saveString);
var NamePreset = 'Illustrator 2020';
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?") === true) {
doc.saveAs(newFile, saveOpts);
doc.close;
}
}
if (newFile.exists === false) {
var saveString = (folderString + soNum + ".eps");
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);
doc.close;
//
//
//
} else if (choice == "UV Print") {
var folderString = "G:" + sc + "Layout" + sc + "Plaques_Layout" + sc + "Giclee-Wayfinding" + sc;
var saveString = (folderString + soNum + ".eps"); //alert(saveString);
var NamePreset = 'Illustrator 2020';
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?") === true) {
doc.saveAs(newFile, saveOpts);
doc.close;
}
}
if (newFile.exists === false) {
var saveString = (folderString + soNum + ".eps");
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);}
doc.close;}
//
//
//
} else if (choice == "Ceramic Insert") {
var folderString = "G:" + sc + "Layout" + sc + "Plaques_Layout" + sc + "Precision Tooled" + sc;
var saveString = (folderString + soNum + ".eps"); //alert(saveString);
var NamePreset = 'Illustrator 2020';
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?") === true) {
doc.saveAs(newFile, saveOpts);
doc.close;}
}
if (newFile.exists === false) {
var saveString = (folderString + soNum + ".eps");
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);}
doc.close;}
//
//
//
} else if (choice == "Laser Engraved") {
var folderString = "G:" + sc + "Layout" + sc + "Plaques_Layout" + sc + "Laser Engraved" + sc;
var saveString = (folderString + soNum + ".eps"); //alert(saveString);
var NamePreset = 'Illustrator 2020';
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?") === true) {
doc.saveAs(newFile, saveOpts);
doc.close;}
}
if (newFile.exists === false) {
var saveString = (folderString + soNum + ".eps");
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);}
doc.close;
};
Thank you!
This is my take on simplifying this script.
I am using "JSDoc" and VSCode which lets you have hints and auto-completion inside the code-editor.
I've moved various strings in the code to central object "AppStrings" and I looked at what's different in the functions to determine that actually it's just the folder string.
/**
* @typedef ProcessChoice
* {string} name
* {string} folder
*/
var doc = app.activeDocument;
var strEnter = doc.name; //alert(strEnter);
var soNum = strEnter.slice(0,7)
...
Copy link to clipboard
Copied
You have the same routine repeated for each of six destinations, why not just have one and call on it.
Copy link to clipboard
Copied
This is my take on simplifying this script.
I am using "JSDoc" and VSCode which lets you have hints and auto-completion inside the code-editor.
I've moved various strings in the code to central object "AppStrings" and I looked at what's different in the functions to determine that actually it's just the folder string.
/**
* @typedef ProcessChoice
* {string} name
* {string} folder
*/
var doc = app.activeDocument;
var strEnter = doc.name; //alert(strEnter);
var soNum = strEnter.slice(0,7); //alert(soNum);
var AppStrings = {
/** @TyPe {"Tooled"} */
TOOLED : ("Tooled"),
/** @TyPe {"Cast"} */
CAST : ("Cast"),
/** @TyPe {"Etched"} */
ETCHED : ("Etched"),
/** @TyPe {"UV Print"} */
UV_PRINT : ("UV Print"),
/** @TyPe {"Ceramic Insert"} */
CERAMIC : ("Ceramic Insert"),
/** @TyPe {"Laser Engraving"} */
LASER : ("Laser Engraving"),
/** @TyPe {"G:"} */
G_DRIVE : ("G:"),
/** @TyPe {"M:"} */
M_DRIVE : ("M:"),
/** @TyPe {"Layout"} */
LAYOUT : ("Layout"),
/** @TyPe {"Plaques_Layout"} */
PLAQUES_LAYOUT : ("Plaques_Layout"),
/** @TyPe {"Precision Tooled"} */
PRECISION_TOOLED : ("Precision Tooled"),
/** @TyPe {"Illustrator 2020"} */
AI_2020_PRESET : ("Illustrator 2020"),
/** @TyPe {"440IAPLAQUES"} */
_440IAPLAQUES : ("440IAPLAQUES"),
/** @TyPe {"Giclee-Wayfinding"} */
GICLEE : ("Giclee-Wayfinding"),
/** @TyPe {".eps"} */
EPS : (".eps"),
/** @TyPe {"/"} */
SC : ("/"),
};
var sc = AppStrings.SC;
var Choices = {};
Choices[AppStrings.TOOLED] = {
name : AppStrings.TOOLED,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.PRECISION_TOOLED + sc,
};
Choices[AppStrings.CAST] = {
name : AppStrings.CAST,
folder : AppStrings.M_DRIVE + sc + AppStrings._440IAPLAQUES + sc,
};
Choices[AppStrings.ETCHED] = {
name : AppStrings.ETCHED,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.ETCHED + sc,
};
Choices[AppStrings.UV_PRINT] = {
name : AppStrings.UV_PRINT,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.GICLEE + sc,
};
Choices[AppStrings.CERAMIC] = {
name : AppStrings.CERAMIC,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.PRECISION_TOOLED + sc,
};
Choices[AppStrings.LASER] = {
name : AppStrings.LASER,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.LASER + sc,
};
/** { AppStrings["TOOLED"] | AppStrings["CAST"] | AppStrings["ETCHED"] | AppStrings["UV_PRINT"] | AppStrings["CERAMIC"] | AppStrings["LASER"] } */
var choice;
/**
* Does the one process with various conditions to accommodate all the choices.
* {ProcessChoice} choiceObj
*/
function processChoice (choiceObj) {
var folderString = choiceObj.folder;
var saveString = (folderString + soNum + AppStrings.EPS); //alert(saveString);
var NamePreset = AppStrings.AI_2020_PRESET;
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false;
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?")) {
doc.saveAs(newFile, saveOpts);
doc.close;
} else {
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);
doc.close;
}
}
}
//Creates new Popup
var Pallette = new Window ("dialog", "Save Precision Tooled Art Files");
Pallette.add ("statictext", undefined, "Pick Production File");
Pallette.orientation = "row";
//Dropdown Menu
var choiceList = [];
for (var all in Choices) {
choiceList.push(all);
}
var myDropdown = Pallette.add ("dropdownlist", undefined, choiceList);
var myButtonGroup = Pallette.add ("group");
myButtonGroup.orientation = "column";
myDropdown.selection = 0;
var btnCreate = myButtonGroup.add ("button", undefined, "OK");
btnCreate.onClick = function () {
choice = myDropdown.selection.text;
w.button.onClick = function () {
w.close();}
};
Pallette.show ();
processChoice(Choices[choice]);
Having the JSDoc inside the AppStrings object lets you see the string when you hover over the variables anywhere, you can also get proper references to check exactly how many times there was a particular string in the code and it's not in the comments. Plus, if for any reason those strings need to change, you can either change them via hard code or add something more dynamic in and you would know where out of all the places to make the change.
The chained if-statement is replaced by the "Choices" object which is a key-value associative array kind of object where the name of the choice is the 'key' to 'unlocking' more information about the choice such as its folder string. The if-conditions are replaced by the key access operation "Choices[choice]" so it always goes to its proper spot and gets the right folder string for the choice.
Copy link to clipboard
Copied
@Silly-V Any Ideas as to where I went wrong here? I can get the Tooled and Cast Choices to save but for some reason Etched, UV_Print, Ceramic, & Laser won't actually save the file. It runs through, I have an alert(saveString) to verify the location is correct that pops up as expected but then it does not save the file.
Copy link to clipboard
Copied
Looks like the function after I edited it was only going to work for situations where the file already exists.
Here is a change to make it save all the time:
function processChoice (choiceObj) {
var folderString = choiceObj.folder;
var saveString = (folderString + soNum + AppStrings.EPS); //alert(saveString);
var NamePreset = AppStrings.AI_2020_PRESET;
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false;
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?")) {
doc.saveAs(newFile, saveOpts);
doc.close;
}
} else {
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);
doc.close;
}
}
Copy link to clipboard
Copied
I think this problem was with my original code.
Thanks again!
Copy link to clipboard
Copied
@Silly-V Am I going about this correctly? I have 5 different server locations for the 5 different production types.
I believe I have it set up to pull in Location from a JSON file but Visual Studio Code seems to believe there is an issue with how I have my IF ELSE Statement structured. What am I not taking into consideration?
/**
* @typedef ProcessChoice
* {string} name
* {string} folder
*/
var doc = app.activeDocument;
var strEnter = doc.name; //alert(strEnter);
var soNum = strEnter.slice(0,7); //alert(soNum);
var prefs = load_prefs() // try to loads saved prefs
|| {a: true, b:false, c:false, d:false, e:false, choice:"MN" } // or set default prefs
prefs.choice = choice;
var AppStrings = {
/** {"Tooled"} */
TOOLED : ("Tooled"),
/** {"ETCH"} */
ETCH : ("Etch"),
/** {"UV Print"} */
UV_PRINT : ("UV Print"),
/** {"Cast"} */
CAST : ("Cast"),
/** {"Ceramic Insert"} */
CERAMIC : ("Ceramic Insert"),
/** {"C:"} */
C_DRIVE : ("C:"),
/** {"G:"} */
G_DRIVE : ("G:"),
/** {"M:"} */
M_DRIVE : ("M:"),
/** {"Q:"} */
Q_DRIVE : ("Q:"),
/** {"T:"} */
T_DRIVE : ("T:"),
/** {"W:"} */
W_DRIVE : ("W:"),
/** {"NETWORK:"} */
NETWORK : ("Idp-fs-02"),
/** {"DATA"} */
DATA : ("DATA"),
/** {"GRAPHICS"} */
GRAPHICS : ("GRAPHICS"),
/** {"Layout"} */
LAYOUT : ("Layout"),
/** {"Plaques_Layout"} */
PLAQUES_LAYOUT : ("Plaques_Layout"),
/** {"Plaque_Layouts"} */
PLAQUE_LAYOUTS : ("Plaque_Layouts"),
/** {"WA_Plaque_LayoutS"} */
WA_PLAQUE_LAYOUTS : ("WA-Plaque Art Layouts"),
/** {"Precision Tooled"} */
PRECISION_TOOLED : ("Precision Tooled"),
/** {"Illustrator 2020"} */
AI_2020_PRESET : ("Illustrator 2020"),
/** {"440IAPLAQUES"} */
_440IAPLAQUES : ("440IAPLAQUES"),
/** {"Giclee-Wayfinding"} */
GICLEE : ("Giclee-Wayfinding"),
/** {".eps"} */
EPS : (".eps"),
/** {"/"} */
SC : ("/"),
};
var choice;
var sc = AppStrings.SC;
var Choices = {};
if (Choices[AppStrings.TOOLED] && (choice == "MN")) {
// Minnesota
name : AppStrings.TOOLED,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.PRECISION_TOOLED + sc,
// Washington
}else if (Choices[AppStrings.TOOLED] && (choice == "WA")){
name : AppStrings.TOOLED,
folder : AppStrings.W_DRIVE + sc + AppStrings.WA_PLAQUE_LAYOUTS + sc + AppStrings.LAYOUTS + sc,
// Canada
}else if (Choices[AppStrings.TOOLED] && (choice == "CA")){
name : AppStrings.TOOLED,
folder : AppStrings.Q_DRIVE + sc + AppStrings.GRAPHICS + sc + AppStrings.PLAQUE_LAYOUTS + sc + AppStrings.PRECISION_TOOLED + sc,
// Texas
}else if (Choices[AppStrings.TOOLED] && (choice == "TX")){
name : AppStrings.TOOLED,
folder : AppStrings.T_DRIVE + sc + AppStrings.GRAPHICS + sc + AppStrings.PLAQUE_LAYOUTS + sc + AppStrings.PRECISION_TOOLED + sc,
// Remote Work From Home
}else if (Choices[AppStrings.TOOLED] && (choice == "Remote")){
name : AppStrings.TOOLED,
folder : AppStrings.C_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.PRECISION_TOOLED + sc,
};
Choices[AppStrings.CAST] = {
name : AppStrings.CAST,
folder : AppStrings.M_DRIVE + sc + AppStrings._440IAPLAQUES + sc,
};
Choices[AppStrings.ETCH] = {
name : AppStrings.ETCH,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.ETCH + sc,
};
Choices[AppStrings.UV_PRINT] = {
name : AppStrings.UV_PRINT,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.GICLEE + sc,
};
Choices[AppStrings.CERAMIC] = {
name : AppStrings.CERAMIC,
folder : AppStrings.G_DRIVE + sc + AppStrings.LAYOUT + sc + AppStrings.PLAQUES_LAYOUT + sc + AppStrings.CERAMIC + sc,
};
/**
* Does the one process with various conditions to accommodate all the choices.
* {ProcessChoice} choiceObj
*/
function processChoice (choiceObj) {
var folderString = choiceObj.folder;
var saveString = (folderString + soNum + AppStrings.EPS); //alert(saveString);
var NamePreset = AppStrings.AI_2020_PRESET;
var newFile = new File (saveString);
var saveOpts = new EPSSaveOptions();
saveOpts.EPSPreset = NamePreset;
//View After Saving Option True=Yes False=No
saveOpts.viewAfterSaving === false;
//Checking for overwrite
if (newFile.exists === true) {
if (confirm("Replace existing File?")) {
doc.saveAs(newFile, saveOpts);
doc.close;
}
} else {
newFile = new File (saveString);
doc.saveAs(newFile, saveOpts);
doc.close;
}
}
// Load Perfs for Location Preferences
function load_prefs() {
// var file = File(Folder.temp + '/perfs.json')
var file = File('C:/Users/Public/perfs.json')
return (file.exists) ? $.evalFile(file) : false;
}
//Creates new Popup
var Pallette = new Window ("dialog", "Save Art File");
Pallette.add ("statictext", undefined, "Choose Production Type");
Pallette.orientation = "row";
//Dropdown Menu
var choiceList = [];
for (var all in Choices) {
choiceList.push(all);
}
var myDropdown = Pallette.add ("dropdownlist", undefined, choiceList);
var myButtonGroup = Pallette.add ("group");
myButtonGroup.orientation = "column";
myDropdown.selection = 0;
var btnCreate = myButtonGroup.add ("button", undefined, "OK");
btnCreate.onClick = function () {
choice = myDropdown.selection.text;
w.button.onClick = function () {
w.close();}
};
Pallette.show ();
processChoice(Choices[choice]);
Copy link to clipboard
Copied
Don't have any if-statements; put them in as new choices.
This can't work:
var Choices = {};
if (Choices[AppStrings.TOOLED]
Choices is {}, there is no AppStrings.TOOLED inside there, so this if-statement will be false.
Here is a simplified example of the original code:
// ====================== OLD ====================== //
var myChoice = "ID_1";
if (myChoice == "ID_1") {
alert("A");
} else if (myChoice == "ID_2") {
alert("B");
}
// ====================== NEW ====================== //
var Choices = {};
Choices["ID_1"] = { value : "A" };
Choices["ID_2"] = { value : "B" };
function processChoice (choiceData) {
alert(choiceData.value);
}
var myChoice = "ID_1";
processChoice(Choices[myChoice]);
So it looks like the bottom section has more code, and the beginning it sure does. However, as the block inside the if-statements can be large and as more choices get added one would have to re-paste the whole if-block.
The Choices object wasn't created as a literal in my example because I wanted to ensure that a variable which contains a string would be used as the key. This way I can rename the variable and change the string too, and it will change in just the places where it's being used.
In the above example I skipped the whole AppStrings string-variable-holder, but you can see the strings "ID_1" used as the key. Note that before Choices gets a key and value via Choices["ID_1"], it is empty.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now