getFileForSaving in a sub folder

Hello everyone, I am trying to export a list of reports to a specific directory on the desktop in a specific folder “assets”, I am using the storage.domains.userDesktop Symbol but I want to save the JSON files in the “assets” folder, is there a way to make this programatically without the user picking the folder each time for every report he/she needs to export, and can it be done without the prompt?

import { storage } from "uxp";
const fs = storage.localFileSystem;

export async function saveJSON(dataJSON, fileName = "default") {
  try {
    const json_file = await fs.getFileForSaving(fileName + ".json", {
      initialDomain: storage.domains.userDesktop,
    });
    await json_file.write(JSON.stringify(dataJSON));
  } catch (err) {
    console.error(err);
  }
}

you have multiple options:

OPTION 1:
let user pick the root folder once using getFolder(options) then you can:


OPTION 2:
use getEntryWithUrl(url) that gives you access to an entry without user interactions [*]


OPTION 3:
use require(‘fs’) instead of localFileSystem which is perfect for your use case. [*]
writeFile(path, data, options, callback)


** Options 2 and 3 require Manifest v5 and setting requiredPermissions.localFileSystem to “fullAccess” in the manifest

1 Like

hi @Maher thank you for the quick reply, I will check it out.

hey I fix it

export async function saveJSON(dataJSON, fileName = "default") {
  try {
    const docFolder = await fs.getEntryWithUrl("file:Users/user/Desktop/assets");
    const createJSONFile = await docFolder.createFile(`${fileName}.json`, {
      overwrite: true,
    });
    createJSONFile.write(JSON.stringify(dataJSON));
  } catch (err) {
    console.error(err);
  }
}

I had a mistake in the manifest v5

// I should do this
"requiredPermissions": {
    "localFileSystem": "fullAccess"
},

// I was doing tis instead
"localFileSystem": "fullAccess",