Skip to main content
Inspiring
February 26, 2022
해결됨

Writing to JSON file

  • February 26, 2022
  • 2 답변들
  • 782 조회

The following code cretes the json file on the desktop but fails to wrtie the obj variable values.

Any ideas why the code wrtitting functionality is not working?

 

#target photoshop

function createJSONFile(object){

  //defie file 
  var jsonFile = new File("~/Desktop/Test.json")
  //jsonFile.close();

  if(!jsonFile.exists) {
    //open file
    jsonFile.open("w")
    
    //write to file convert to string
    jsonFile.write(JSON.stringify(object));

    //close file
    jsonFile.close()
  }

}

//object variable
var obj={
  compName: "My comp",
  width: 1920,
  height: 1080,
  numLayer: 3
};

createJSONFile(obj);

 

이 주제는 답변이 닫혔습니다.
최고의 답변: Kukurykus

Change:

JSON.stringify(object)

to:

object.toSource()

2 답변

Polycontrast작성자
Inspiring
February 26, 2022

The following code cretes the json file on the desktop but fails to wrtie the obj variable values.

Any ideas why the code writing functionality is not working?

#target photoshop

function createJSONFile(object){

  //define file 
  var jsonFile = new File("~/Desktop/Test.json")
  //jsonFile.close();

  if(!jsonFile.exists) {
    //open file
    jsonFile.open("w")
    
    //write to file convert to string
    jsonFile.write(JSON.stringify(object));

    //close file
    jsonFile.close()
  }

}

//object variable
var obj={
  compName: "My comp",
  width: 1920,
  height: 1080,
  numLayer: 3
};

createJSONFile(obj);

 

 

 

 

 

 

 

 

 

 

 

Kukurykus
Brainiac
February 26, 2022

I gave you solution, which means when you use non ES command it is not going to work.

 

By the way, this is the same code you once posted. Is there some reason you post it again?

Polycontrast작성자
Inspiring
February 27, 2022

Sorry, the script poste posted accidently froa second time.

Thansk for sahring your solution.

Kukurykus
Kukurykus답변
Brainiac
February 26, 2022

Change:

JSON.stringify(object)

to:

object.toSource()