• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Check for a JSON file with Javascript

Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

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!

 

TOPICS
Scripting

Views

1.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 06, 2022 Jan 06, 2022

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("dia
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

does this help?

 

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

if (jsonfile.exists) 
    alert ("File exists");
else
    alert ("File does not exist");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

@CarlosCanto I'll give it a try!  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

Hi,

Did you try the above code, where we are creating test.json file if it does not exists. To run it sucessfully, please include JSON.jsx file.

 

Also,

1. please share you complete code as load_ptype method is missing.

2. If you want complete code after else only to be excuted when JSON not exists then please enclosed complete code in the curly braces of else.

 

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

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.

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

when i run it, the script creates a file but it still errors out.

 

Error 1302: No Such Element
Line: 98
->      var currInstance = docRef.symbolItems.add(docRef.symbols[symbolNum - 1]);

 

I don't receive this error if the file already exists.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

the JSON file created is not written with the default option 

 

({a:true, b:false, c:false, d:false, e:false, choice:undefined})
 
default is
({a:true, b:false, c:false, d:false, e:false, choice:"Tooled"})

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 06, 2022 Jan 06, 2022

Copy link to clipboard

Copied

LATEST

@Charu Rajput I added choice:"Tooled" to line 10 and it seems to be working now!   Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines