Skip to main content
Inspiring
January 6, 2022
Answered

Check for a JSON file with Javascript

  • January 6, 2022
  • 2 replies
  • 3455 views

Is there a way / how would one have a JavaScript that checked for a JSON file?

- If the JSON file did not exist, it would write it before continuing

- If the JSON file did exist, it would continue without overwriting it.

 

Thank you so much for any insights you can share!

 

This topic has been closed for replies.
Correct answer Charu Rajput

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.

2 replies

CarlosCanto
Community Expert
Community Expert
January 6, 2022

does this help?

 

var jsonfile = File("c:/full path/file.json");

if (jsonfile.exists) 
    alert ("File exists");
else
    alert ("File does not exist");
Inspiring
January 6, 2022

@CarlosCanto I'll give it a try!  

Inspiring
January 6, 2022

so I can get the alerts to work but I cannot get it to write a JSON file 
Here is the part of my script that I am trying to work on, 

if (jsonFile.exists)

       do nothing;

else 

      write json  ({a:true, b:false, c:false, d:false, e:false, choice:"Tooled"})

 

var docRef=app.activeDocument;

var jsonFile = File('C:/Users/Public/ptype.json')

if (jsonFile.exists) 
    ;//alert('File Exists');
else
    alert('First time use. Please click selection');

    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";

 

 

Charu Rajput
Community Expert
Community Expert
January 6, 2022

Hi,

You can try the following code

var jsonFile = File(Folder.desktop + "/test.json");
if(jsonFile.exists){
    alert("File exists");
}else{
    alert("Not exists");
    var data = {
        name: 'test',
        value : 'value'
    }
    file = File(Folder.desktop + "/test.json");
    file.open("w");
    file.write(JSON.stringify(data));
    file.close();   
}

 

NOTE: Make sure to include json.jsx file.

Best regards