Or if you perfer here is my original script:
var docRef=app.activeDocument;
var ptype = load_ptype() // try to loads saved ptype
|| {a: true, b:false, c:false, d:false, e:false} // or set default ptype
var choice;
var w = new Window("dialog");
w.text = "Please Select The Product Type";
var g = w.add("group");
var a = g.add("radiobutton", undefined, "Tooled");
var b = g.add("radiobutton", undefined, "Cast");
var c = g.add("radiobutton", undefined, "Etched");
var d = g.add("radiobutton", undefined, "UV Panel");
var button = w.add("button", undefined, "OK");
var myMessage = w.add("statictext");
myMessage.text = "“We don’t make mistakes, just happy little accidents.” – Bob Ross";
// set the radiobutton from the ptype
a.value = ptype.a;
b.value = ptype.b;
c.value = ptype.c;
d.value = ptype.d;
choice = ptype.choice; // <-- updated
var radiobuttons = [a, b, c, d];
for (var i = 0; i < radiobuttons.length; i++) {
(function (i) {
radiobuttons[i].onClick = function () {
choice = radiobuttons[i].text;
};
})(i);
}
w.show();
//alert(choice);
// set the ptype from radiobuttons
ptype.a = a.value;
ptype.b = b.value;
ptype.c = c.value;
ptype.d = d.value;
ptype.choice = choice; // <-- updated
save_ptype(ptype); // save the ptype
// functions to load and save ptype
function load_ptype() {
// var file = File(Folder.temp + '/ptype.json')
var file = File('C:/Users/Public/ptype.json')
return (file.exists) ? $.evalFile(file) : false;
}
function save_ptype(ptype) {
// var file = File(Folder.temp + '/ptype.json')
var file = File('C:/Users/Public/ptype.json')
file.open('w');
file.encoding = 'UTF-8';
file.write(ptype.toSource());
file.close();
}
var symbolNum;
if (choice == "Tooled") {
// Precision Tooled
symbolNum = 2;
} else if (choice == "Cast") {
// Cast
symbolNum = 3;
} else if (choice == "Etched") {
// Etched
symbolNum = 4;
} else if (choice == "UV Panel") {
// Texas
symbolNum = 5;
}
//Uncomment next line and remove var symbolNum=6 to add prompt back in. - BSP
//var symbolNum=prompt("Enter the number of the Symbol you want to replace each selected object",6);
//Updated for proof placement placement on 11/23/21 at 12:53 p.m. - Aaron Thesing
for(i=0;i<docRef.selection.length;i++){
var currObj=docRef.selection[i];
var currLeft=currObj.left;
var currTop=currObj.top;
var currWidth=currObj.width;
var currHeight=currObj.height;
var currCenterX = currLeft + (currWidth / 1.85); //defines X center
var currCenterY = currTop - (currHeight / 1.75); //defines Y center
var currInstance=docRef.symbolItems.add(docRef.symbols[symbolNum-1]);
currInstance.width=currWidth
currInstance.height=currHeight;
currInstance.left=currCenterX - (currInstance.width / 2); //makes it centered on X
currInstance.top=currCenterY + (currInstance.height / 2); //makes it centered on Y
currInstance.selected=true;
currObj.remove();
redraw();
}
Hi,
I tested your script it is working fine. I just run by changing the path of the json file to desktop as I am on mac. Do you receive any error? It may possible where you are trying to write does not have write permissions.
Here is the version that I run, it should work for you. Try to run the exact below version.
var docRef = app.activeDocument;
var ptype = load_ptype() || { a: true, b: false, c: false, d: false, e: false } // or set default ptype
var choice;
var w = new Window("dialog");
w.text = "Please Select The Product Type";
var g = w.add("group");
var a = g.add("radiobutton", undefined, "Tooled");
var b = g.add("radiobutton", undefined, "Cast");
var c = g.add("radiobutton", undefined, "Etched");
var d = g.add("radiobutton", undefined, "UV Panel");
var button = w.add("button", undefined, "OK");
var myMessage = w.add("statictext");
myMessage.text = "“We don’t make mistakes, just happy little accidents.” – Bob Ross";
// set the radiobutton from the ptype
a.value = ptype.a;
b.value = ptype.b;
c.value = ptype.c;
d.value = ptype.d;
choice = ptype.choice; // <-- updated
var radiobuttons = [a, b, c, d];
for (var i = 0; i < radiobuttons.length; i++) {
(function (i) {
radiobuttons[i].onClick = function () {
choice = radiobuttons[i].text;
};
})(i);
}
w.show();
//alert(choice);
// set the ptype from radiobuttons
ptype.a = a.value;
ptype.b = b.value;
ptype.c = c.value;
ptype.d = d.value;
ptype.choice = choice; // <-- updated
save_ptype(ptype); // save the ptype
// functions to load and save ptype
function load_ptype() {
var file = File(Folder.desktop + "/ptype.json");
return (file.exists) ? $.evalFile(file) : false;
}
function save_ptype(ptype) {
var file = File(Folder.desktop + "/ptype.json")
file.open('w');
file.encoding = 'UTF-8';
file.write(ptype.toSource());
file.close();
}
var symbolNum;
if (choice == "Tooled") {
// Precision Tooled
symbolNum = 2;
} else if (choice == "Cast") {
// Cast
symbolNum = 3;
} else if (choice == "Etched") {
// Etched
symbolNum = 4;
} else if (choice == "UV Panel") {
// Texas
symbolNum = 5;
}
//Uncomment next line and remove var symbolNum=6 to add prompt back in. - BSP
//var symbolNum=prompt("Enter the number of the Symbol you want to replace each selected object",6);
//Updated for proof placement placement on 11/23/21 at 12:53 p.m. - Aaron Thesing
for (i = 0; i < docRef.selection.length; i++) {
var currObj = docRef.selection[i];
var currLeft = currObj.left;
var currTop = currObj.top;
var currWidth = currObj.width;
var currHeight = currObj.height;
var currCenterX = currLeft + (currWidth / 1.85); //defines X center
var currCenterY = currTop - (currHeight / 1.75); //defines Y center
var currInstance = docRef.symbolItems.add(docRef.symbols[symbolNum - 1]);
currInstance.width = currWidth
currInstance.height = currHeight;
currInstance.left = currCenterX - (currInstance.width / 2); //makes it centered on X
currInstance.top = currCenterY + (currInstance.height / 2); //makes it centered on Y
currInstance.selected = true;
currObj.remove();
redraw();
}
if you are getting aany error please post the error message.