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

Writing to JSON file

Engaged ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

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

 

TOPICS
Actions and scripting

Views

357

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

LEGEND , Feb 26, 2022 Feb 26, 2022

Change:

JSON.stringify(object)

to:

object.toSource()

Votes

Translate

Translate
Adobe
LEGEND ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

Change:

JSON.stringify(object)

to:

object.toSource()

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 ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

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

 

 

 

 

 

 

 

 

 

 

 

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
LEGEND ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

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?

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 ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

LATEST

Sorry, the script poste posted accidently froa second time.

Thansk for sahring your solution.

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