Hello! I’m needing to convert an existing Extendscript workflow into a UXP panel, but i’m a little lost with UXP and slowly understanding how to re-approach the project! So far, I have my panel up and running, and am able to do some of the basic operations. But…
How can i check to see if a folder exists at the location of the current document? Here’s what I’m trying to achieve:
Assuming my document’s name is: “superhero.psd”:
- Check to see if there is a folder called “superhero-exports” in the same directory as “superhero.psd”
- If there is, set that directory into a variable as the target directory for other functions (in my case, saving out PNGs)
- If there is not, create one, concatenating the filename into '[filename]+“-exports” ', and set that directory into a variable as the target directory for other functions
The more I can see a parallel workflow, the more I’m understanding how to slide over into UXP from Extendscript. Any help is appreciated!
OK I slogged my way through a ChatGPT implementation. Not sure if it’s the most elegant but it works and returns a target directory!
const fs = require("uxp").storage.localFileSystem;
async function ensureDocumentSaved() {
const app = require("photoshop").app;
const document = app.activeDocument;
if (!document) {
throw new Error("No active document found.");
}
if (!document.path) {
console.log("Document is not saved. Prompting user to save it.");
alert("Please save the document once first.");
return false;
}
return document;
}
async function getDocumentNameWithoutExtension() {
const document = await ensureDocumentSaved();
if(document){
const fullName = document.name; // Gets the full document name with extension
const nameWithoutExtension = fullName.replace(/\.[^/.]+$/, ""); // Removes the extension
return nameWithoutExtension;
}else{
return false;
}
}
async function setupExportFolder() {
const nameWithoutExtension = await getDocumentNameWithoutExtension();
if(nameWithoutExtension){
const folderName = `${nameWithoutExtension}-exports`;
// Get the parent directory of the document
const documentFolder = await fs.getFolder();
if (!documentFolder) {
throw new Error("Could not access the document directory.");
}
// Get all entries in the selected folder
const entries = await documentFolder.getEntries();
// Filter entries to find the export folder
let targetDirectory = entries.find(entry => entry.name === folderName);
if (!targetDirectory) {
console.log("Folder does not exist. Creating it.");
targetDirectory = await documentFolder.createFolder(folderName);
} else {
console.log("Folder already exists.");
}
return targetDirectory; // Return the target directory for further use
}else{
return false
}
}
async function initializeTargetDirectory() {
try {
const targetDirectory = await setupExportFolder();
if(targetDirectory){
console.log("Target directory initialized:", targetDirectory.nativePath);
return targetDirectory;
}else{
return
}
} catch (error) {
console.error("Error during export process initialization:", error.message);
}
}
initializeTargetDirectory();
If anyone has better ideas please let me know. Thanks!