Hello, i have this code that work properly, but i want to add a check to find if the folder already exist bfore to create it, can someone help me? Thank you
//parte di codice che chiede all'utente la cartella fissa e crea il token persistente
const fs = require('uxp').storage.localFileSystem;
const fixfolder = await fs.getFolder(); //apre una schermata che permette di scegliere la cartella
const persistentToken = await fs.createPersistentToken(fixfolder); //crea un token persistente per la cartella
localStorage.setItem('fixfolder', JSON.stringify(persistentToken)); //associa il token creato alla entry della cartella
//parte di codice che richiama il token salvato per la cartella
const app = window.require('photoshop').app
const batchPlay = require('photoshop').action.batchPlay;
const fs = require('uxp').storage.localFileSystem;
//seleziona il token salvato
token = await JSON.parse(localStorage.getItem("fixfolder"));
//estrae il percorso della cartella persistente
const folder = await fs.getEntryForPersistentToken(token);
//Crea le cartelle desiderate e le sottocartelle)
const AdobeRGB = await folder.createFolder("AdobeRGB");
const AdobeTIF = await AdobeRGB.createFolder("AdobeTIF");
const AdobeJPEG = await AdobeRGB.createFolder("AdobeJPEG");
//crea il file in modalitĂ modale
require('photoshop').core.executeAsModal(async () => {
const newFile = await AdobeTIF.createFile("prova.tif", { overwrite: true });
const saveFile = await fs.createSessionToken(newFile);
const result = await batchPlay(
[
{
"_obj": "save",
"as":
{
"_obj": "TIFF",
"byteOrder":
{
"_enum": "platform",
"_value": "IBMPC"
},
"layerCompression":
{
"_enum": "encoding",
"_value": "RLE"
}
},
"in": {
"_path": saveFile,
"_kind": "local"
}
}
], {
"synchronousExecution": false,
//"modalBehavior": "fail"
});
});
You can use getEntries to get any files and sub-folders in your folder and then use .filter() to filter the returned array to only sub-folders (note the use of UXP’s isFolder method), and then use find() to search for the specific subfolder by name. If it exists then you now have it assigned to a variable, and if not it returns undefined
Finally, you can test the folder variable’s truthiness with an if statement.
That would be exactly the same as my example but with your AdobeRGB folder as the parent.
You’ll also need to tweak the find() for whichever folder you are trying to find.
I’m not sure you understand how to use functions?
You need to call the function elsewhere in your code and pass the real values to it.
Those arguments you have changed are proxies for those values so that the function becomes re-usable with different values.
To call the function you just use the function name and parentheses: findOrCreateFolder(folder, "folder name")
But as this particular function returns a value you want to assign it to a variable: const theFolder = findOrCreateFolder (folder, "folder name")
The final consideration is that it is an asynchronous function and so will need to be called from within another async function and pre-pended with await:
const main = async () => {
// The findOrCreateFolder function and all other code goes here - including whatever defines the folder variable that is going to be passed to findOrCreateFolder
const theFolder = await findOrCreateFolder(folder, "folder name");
}
main();