As you can see from the sketch below, I insert the original photos in a folder YEAR/DATE/ORIGINALS/, edit the file in Photoshop and export 4 copies in their respective folder, creating the structure seen in the sketch (code created folders structure). The code I wrote works well, but I can only create the folder structure within the YEAR folder (2022), which I saved a permanent token that I call in the code. I would instead like to create the structure into the parent path of the active document I am editing, so that it is then saved in the respective DATE folder. is it possible? i dont want to use getfolder(), because i already have the token for the YEAR folder. I attach the code I have written so far, thank you very much.
const main = async () => {
//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);
//let docDir = path.parse(app.activeDocument.path).dir;
//acquisisce tutte le cartelle e le crea solo se non sono già state create per il profilo AdobeRGB
const AdobeRGB = await findOrCreateFolder(folder, "AdobeRGB");
const sRGB = await findOrCreateFolder(folder, "sRGB");
const AdobeTIF = await findOrCreateFolder(AdobeRGB, "AdobeTIF");
const AdobeJPEG = await findOrCreateFolder(AdobeRGB, "AdobeJPEG");
const sRGBFramed = await findOrCreateFolder(sRGB, "sRGB_Framed");
const sRGBUnFramed = await findOrCreateFolder(sRGB, "sRGB_Unframed");
//acquisisce il nome del file e rimuove l'estensione
const fileName = app.activeDocument.title;
const fileNameDef = await dothename(fileName);
//crea il file in modalità modale
require('photoshop').core.executeAsModal(async () => {
const newFile = await AdobeRGB.createFile(fileNameDef + ".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"
});
});
}
//END_MAIN
main();
////////////////////////////////////
//Funzione per scegliere la cartella e salvare il permanent token
//parte di codice che chiede all'utente la cartella fissa e crea il token persistente
const pickfolder = async () => {
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
}
// funzione che elimina l'estensione dal nome del documento attivo
const dothename = async (fileName) => {
// Get the position of the last '.' in the file name
let extensionStart = fileName.lastIndexOf(".");
// If there is an extension, remove it
if (extensionStart > -1) {
fileName = fileName.substring(0, extensionStart);
}
// Save the name without extension in a variable
const nameWithoutExtension = fileName;
console.log(`File name without extension: ${nameWithoutExtension}`);
return await nameWithoutExtension;
}
const findOrCreateFolder = async (parentFolder, folderName) => {
const entries = await parentFolder.getEntries();
const foundFolder = entries.filter((entry) => entry.isFolder).find((subFolder) => subFolder.name === folderName);
if (foundFolder) {
return foundFolder
}
return await parentFolder.createFolder(folderName)
}
document.getElementById("button").addEventListener("click", main);
document.getElementById("pickfolderbutton").addEventListener("click", pickfolder);