How to read JSON and save JSON to ~/AppData/Roaming?

I’m finishing migrating my plugin to UXP and I’m wondering how to read and write files with UXP?

With the old CEP version I used to read with JSX the file and parse the string to get the data for the UserPresets, and when the user saved a preset I just read the JSON, parsed it and push a new preset to the array inside the JSON and saved it.

How can it be done in UXP? I don’t understand very well the storage.localFileSystem and tokens workflow, it was really easy to do it in CEP though.

There’s a GitHub example here: uxp-photoshop-plugin-samples/index.js at main · AdobeDocs/uxp-photoshop-plugin-samples · GitHub

JSON.stringify() and JSON.parse() work in UXP for creating and parsing strings to put in storage.

Thanks Anthony, I really don’t have problems with JSON itself but how to create the JSON file and save it in for example the user Documents/ folder. I used to make a folder for my plugin inside the users Documents/ folder and I’m trying to figure out how to do it, how to get the user’s Documents/ path to write a file inside.

Hi,
perhaps it is easier to use localStorage to store user settings. You can save json there.

But if you want to store settings in a file, then look at the documentation here:
It will be easier to create file in the plugin data folder (getDataFolder). This option does not require user interaction.

To store in an arbitrary location, the user must select a file through a dialog.
Then you need to create a persistent file token for selected file and save it in local storage or data folder.
Using the token, you can perform read/write operations on a file without having to show a dialog box.

Here’s one approach for utilising the plugin data folder to store a JSON file.

3 Likes

This is great man I modified a little your code it works perfect, thank you very much this works great for what I am looking for!

1 Like

Does this getDataFolder is for the plugin itself ? Or this DataFolder is the same for any other plugin?

Each plugin has its own data folder.

This are the paths that getDataFolder() is using:

Mac:
/Users/<username>/Library/Application Support/Adobe/UXP/PluginsStorage/PHSP/<PSmajorVersionNumber>/External/<PluginId>/PluginData

Windows:
\Users\<username>\AppData\Roaming\Adobe\UXP\PluginsStorage\PHSP\<PSmajorVersionNumber>\External\<PluginId>\PluginData

If you are developing and loading the plugin with the UXP Developer Tool it’s the folder Developer instead of External.

2 Likes

That is great to know Tom thank you very much, this is very helpful!

Do you know where the localStorage path as well ?

It’s the same path but instead of the PluginData folder it is LocalStorage.

1 Like