How can I save the active document

I have worked into UXP in the past few days.
I can open a Photoshop file and save as a JPG.

const fs = require('uxp').storage.localFileSystem;
const folder = await fs.getFolder();
async function saveFileToFolder(folder, fileName) {
   const file = await folder.createFile(`${fileName}.jpg`,{overwrite: true});
   const activeDocument = require("photoshop").app.activeDocument;
   return,{quality: 10},true);
await saveFileToFolder(folder,"jpgFile");

But how can I save the active document. The user loaded it via File Dialog from Photoshop.
How do I get folder of the type folder from the activeDocument?


I dont get any further. So far I have only found examples where I first get a token via a file dialog. Then I can also save with this token.
But how do I get the token when the document is already open? A small code snippet would be very helpful for me.


The short answer is that you can’t.

The long answer:
You need a token to save a file and you can only get one from a file picker invoked via getFolder().
A token provides access to all descendant folders though, so one approach is to get the user to define a root working folder once on plugin installation, generate a Persistent Token for it, and then store that Persistent Token somewhere for later use.
Here’s a demo:

That’s great.

Thanks for this example. I had to change it for my purpose. I use other folder. But it works.

Unfortunately, it doesn’t work without a getfolder().