Copy link to clipboard
Copied
Good Morning All,
I have another javascript question I am hoping for assistance on.
I have a script that records a Radio Button Selection by creating a JSON file, then in future uses of the script, it loads that JSON file. I'm trying to recreate the script for other uses, however, it's not recording the JSON file data.
It records this:
(new Boolean(false))
({a2:true, b2:false, c2:false, d2:false, choice:"SM"})
var doc = app.activeDocument;
var size = load_size()
var choice2;
////////////////////////////////////////////////////////////////////////
var w = new Window ("dialog", "Set Default Size");
var g2 = w.add("group");
var a2 = g2.add("radiobutton", undefined, "SM");
var b2 = g2.add("radiobutton", undefined, "MD");
var c2 = g2.add("radiobutton", undefined, "LG");
var d2 = g2.add("radiobutton", undefined, "XL");
////////////////////////////////////////////////////////////////////////
var radiobuttons2 = [a2, b2, c2, d2];
for (var x = 0; x < radiobuttons2.length; x++) {
(function (x) {
radiobuttons2[x].onClick = function () {
choice2 = radiobuttons2[x].text;
};
})(x);
}
// set the radiobutton from the size file
a2.value = size.a2;
b2.value = size.b2;
c2.value = size.c2;
d2.value = size.d2;
choice2 = size.choice; // <-- updated
//////////////////////////////////////////////////////////////////////////
var g3 = w.add("group");
var okay = g3.add ("button", undefined, "OK"); //Type of button
okay.onClick = function (){
if (choice2 == undefined){
w.close;
} else if (choice2 !== undefined){
alert(choice2);
w.close();}
}
var cancel = g3.add ("button", undefined, "Cancel"); //Type of button
cancel.onClick = function (){
alert("You've Cancelled This Operation")
w.close();
}
/////////////////////////////////////////////////////////////////////////
// set the size from the radiobuttons
size.a2 = a2.value;
size.b2 = b2.value;
size.c2 = c2.value;
size.d2 = d2.value;
size.choice = choice2; // <-- updated
//////////////////////////////////////////////////////////////////////////
var defBtn = g3.add ("button", undefined, "Set Default"); //Type of button
defBtn.onClick = function () {
save_size(size);
alert(choice2);
}
w.show ();
/////////////////////////////////////////////////////////////////////////
// functions to load default PLQ Product Type
function load_size() {
var file = File('C:/Users/Public/size.json')
return (file.exists) ? $.evalFile(file) : false;
}
function save_size(size) {
var file = File('C:/Users/Public/size.json')
file.open('w');
file.encoding = 'UTF-8';
file.write(size.toSource());
file.close();
}
1 Correct answer
Here's a modified version of the final script @BryanPagenkopf and I worked on. His script went in a slightly different direction from what is below (a did some extra stuff), but this shows the answer to his original question of how to record a radio button selection to JSON.
The script checks for a specific "prefs.json" file on the system, if it finds it, the radio button selections are loaded in the dialog. If the JSON file isn't found, preset defaults are loaded in the dialog. Then, after clic
...Explore related tutorials & articles
Copy link to clipboard
Copied
I replaced your `load_size()` function with mine below and it worked fine.
function loadJSONData(file) {
if (file.exists) {
try {
file.encoding = "UTF-8";
file.open("r");
var json = file.read();
file.close();
arr = eval(json);
return arr;
} catch (e) {
("Sorry, error loading settings file!");
}
}
}
I also like to explicitly set up `new Folder` and `new File` var to make sure I'm pointing at the right thing like below.
function setupSettingsFile(folderName, fileName) {
// setup save setting folder and file
var settingsFolderPath = Folder.myDocuments + "/" + folderName;
var settingsFolder = new Folder(settingsFolderPath);
if (!settingsFolder.exists) settingsFolder.create();
var settingsFilePath = settingsFolder + "/" + fileName;
return new File(settingsFilePath);
}
Copy link to clipboard
Copied
I'm not sure if it's my Save Function or how I have my data capturing
but when I manually create the JSON file,
({a2:true, b2:false, c2:false, d2:false, choice:"SM"})
it loads into the Script as expected.
Copy link to clipboard
Copied
Thanks again for any insights!
Copy link to clipboard
Copied
Here's what I used to save my JSON files. Just pass your JSON data as an array for the `arr` variable and pass an Illustrator File Object for `file`.
function writeSettings(arr, file) {
try {
file.encoding = "UTF-8";
file.open("w");
var data = arr.toSource();
file.write(data);
file.close();
} catch (e) {
("Sorry, error saving settings!");
}
}
Copy link to clipboard
Copied
@jduncan 😢 so here is what I put in -
function save_plqtype(arr, file) {
try {
var arr = [a2.value, b2.value, c2.value, d2.value, choice2];
var file = File('C:/Users/Public/plqtype.json');
file.encoding = "UTF-8";
file.open("w");
var data = arr.toSource();
file.write(data);
file.close();
} catch (e) {
("Sorry, error saving settings!");
}
}
and this is what I get in my JSON file ( when I select "MD")-
So the array is only grabbing some of the needed data for the JSON file. 🤔
Copy link to clipboard
Copied
I'm working on the actual script you emailed me. Hang on tight and I'll email it back to you and then we can work out any questions you may have.
Copy link to clipboard
Copied
Just emailed you a private gist on Github. It's mostly a complete rewrite but I think it should accomplish what you are trying to do. Look it over and let me know if you have any questions?
Copy link to clipboard
Copied
Here's a modified version of the final script @BryanPagenkopf and I worked on. His script went in a slightly different direction from what is below (a did some extra stuff), but this shows the answer to his original question of how to record a radio button selection to JSON.
The script checks for a specific "prefs.json" file on the system, if it finds it, the radio button selections are loaded in the dialog. If the JSON file isn't found, preset defaults are loaded in the dialog. Then, after clicking OK, the current settings are written to the same JSON file for use next time the script is run.
//////////////////////////////////////////////////////////////////////////
// SCRIPT SETUP
//////////////////////////////////////////////////////////////////////////
// setup each location and it's path
var locations = {
"Loc 1": Folder.myDocuments + "/subfolder1/subfolder2/loc1/",
"Loc 2": Folder.myDocuments + "/subfolder1/subfolder2/loc2/",
"Loc 3": Folder.myDocuments + "/subfolder1/subfolder2/loc3/",
"Loc 4": Folder.myDocuments + "/subfolder1/subfolder2/loc4/",
"Loc 5": Folder.myDocuments + "/subfolder1/subfolder2/loc5/",
};
// setup each product type and it's subfolder
var products = {
"Prod 1": "prod1/",
"Prod 2": "prod2/",
"Prod 3": "prod3/",
"Prod 4": "prod4/",
};
// define the defaults in case a pref file isn't found
// must be one of the options in the two objects above
var defaults = {
location: "Loc 2",
product: "Prod 4",
};
// setup your prefs folder as an Illustrator Folder object
//https://extendscript.docsforadobe.dev/file-system-access/folder-object.html?highlight=folder
var prefFolder = setupFolderObject(Folder.myDocuments + "/" + "Prefs");
// setup your prefs file as an Illustrator File object and load
// the prefs if the file can be found else load defaults from above
// https://extendscript.docsforadobe.dev/file-system-access/file-object.html
var prefsFile = setupFileObject(prefFolder, "prefs.json");
var prefs = prefsFile.exists ? loadJSONData(prefsFile) : defaults;
//////////////////////////////////////////////////////////////////////////
// MAIN SCRIPT
//////////////////////////////////////////////////////////////////////////
var doc = app.activeDocument;
// present the dialog and return the user selected settings in an object
var settings = settingsWin(prefs);
if (settings) {
var folderLocation = locations[settings.location] + products[settings.product];
var saveFolder = setupFolderObject(folderLocation);
alert("Folder " + saveFolder + " created!");
// do do more stuff here
}
//////////////////////////////////////////////////////////////////////////
// SUPPLEMENTAL FUNCTIONS
//////////////////////////////////////////////////////////////////////////
function setupFolderObject(folderPath) {
var settingsFolder = new Folder(folderPath);
if (!settingsFolder.exists) settingsFolder.create();
return settingsFolder;
}
function setupFileObject(folder, fileName) {
var settingsFilePath = folder + "/" + fileName;
return new File(settingsFilePath);
}
function loadJSONData(file) {
try {
file.encoding = "UTF-8";
file.open("r");
var json = file.read();
file.close();
obj = eval(json);
// alert("Preferences " + file + " loaded successfully!");
return obj;
} catch (e) {
alert("Error loading " + file + " file! Loading defaults instead.");
return defaults;
}
}
function writeJSONData(obj, file) {
try {
file.encoding = "UTF-8";
file.open("w");
var data = obj.toSource();
file.write(data);
file.close();
// alert("Preferences written to file " + file + "!");
} catch (e) {
alert("Error saving setting file " + file + "!");
}
}
function getKeys(obj) {
keys = [];
for (var k in obj) {
keys.push(k);
}
return keys;
}
function captureRBSelection(rbs) {
var selection = null;
for (var i = 0; i < rbs.length; i++) {
if (rbs[i].value) selection = rbs[i].text;
}
return selection;
}
//////////////////////////////////////////////////////////////////////////
// USER DIALOG
//////////////////////////////////////////////////////////////////////////
function settingsWin(loadedPrefs) {
// create a new dialog
var win = new Window("dialog");
win.text = "Production Art Save Options";
// create a radio button for each location
win.add("statictext", undefined, "Please Verify Your Location");
var locationsKeys = getKeys(locations);
var gLocation = win.add("group");
var rb;
var locationRBs = [];
for (var i = 0; i < locationsKeys.length; i++) {
var rb = gLocation.add("radiobutton", undefined, locationsKeys[i]);
if (loadedPrefs["location"] == locationsKeys[i]) rb.value = true;
locationRBs.push(rb);
}
// create a radio button for each product
win.add("statictext", undefined, "Select Your Product Type");
var productsKeys = getKeys(products);
var gProducts = win.add("group");
var rb;
var productRBs = [];
for (var i = 0; i < productsKeys.length; i++) {
var rb = gProducts.add("radiobutton", undefined, productsKeys[i]);
if (loadedPrefs["product"] == productsKeys[i]) rb.value = true;
productRBs.push(rb);
}
// setup window buttons
var gWindowButtons = win.add("group", undefined);
var btOK = gWindowButtons.add("button", undefined, "OK");
var btCancel = gWindowButtons.add("button", undefined, "Cancel");
// if "ok" button clicked then return inputs
if (win.show() == 1) {
// check to see which location and product was selected
var selectedLocation = captureRBSelection(locationRBs);
var selectedProduct = captureRBSelection(productRBs);
var selectedSettings = { location: selectedLocation, product: selectedProduct };
// write the user selected setting to JSON
writeJSONData(selectedSettings, prefsFile);
return selectedSettings;
} else {
return;
}
}
Copy link to clipboard
Copied
Hi @BryanPagenkopf, just for your learning: Your script had this line:
var arr = [a2.value, b2.value, c2.value, d2.value, choice2];
but will give ar array with just the values: a2.value, then b2.value, etc. You want
var obj = {a2:a2.value, b2:b2.value, c2:c2.value, d2:d2.value, choice2:choice2};
to give you what you expect. It's an object with keys named for the values.
- Mark

