Skip to main content
Participant
May 4, 2016
Answered

Save .JSON file from HTML Panel Extension

  • May 4, 2016
  • 2 replies
  • 1806 views

I'm developing an extension that creates a range of data as a javascript object. I'd like to allow the user to click a "save" button, navigate to a directory (optional), and save the json file created in the HTML session. I tried a download link (similar to what I'd do on a web page), but that doesn't save out the file. Is there an efficient way for a user to save out txt,json, or xml data from an HTML Panel?

This topic has been closed for replies.
Correct answer erickk56243057

Alright, I've got a working version using jsx:

function saveJSON(txt)

{

    txt=JSON.stringify(txt)

    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

   

    var Path = app.activeDocument.path;

    var saveFile = File(Path + "/" + Name +".json");

   if(saveFile.exists)

        saveFile.remove();

    saveFile.encoding = "UTF8";

    saveFile.open("e", "TEXT", "????");

    saveFile.writeln(txt);

    saveFile.close();

}

Reference this post for getting JSON.stringify on the JSX side: HTML Panels Tips: #5 passing Objects from JSX to HTML | Photoshop, etc.

2 replies

erickk56243057AuthorCorrect answer
Participant
May 4, 2016

Alright, I've got a working version using jsx:

function saveJSON(txt)

{

    txt=JSON.stringify(txt)

    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

   

    var Path = app.activeDocument.path;

    var saveFile = File(Path + "/" + Name +".json");

   if(saveFile.exists)

        saveFile.remove();

    saveFile.encoding = "UTF8";

    saveFile.open("e", "TEXT", "????");

    saveFile.writeln(txt);

    saveFile.close();

}

Reference this post for getting JSON.stringify on the JSX side: HTML Panels Tips: #5 passing Objects from JSX to HTML | Photoshop, etc.

btempleton1982
Inspiring
May 4, 2016

Maybe you can do it with just javascript, jquery, etc.

What I would do is create a hybrid plugin and pass whatever data you want from the extension back to the C++. There you can use the API to pop a Save as dialog.

Participant
May 4, 2016

btempleton1982 Thanks for the tip! Does anyone have a simpler solution that doesn't involve hybrid plugins?