HI Everyone
I m trying to make a UXP for photoshop in which at the beginning i am taking taking images from user. i instantly converted them to tokens and saved in an array, now at the later stages of my plugin i want to loop through those images and get the name of all images.
The problem is that now the array contain the session tokens , hwo i should get the name without opening each image.
Blockquote
const fileTypes = [“jpg”, “jpeg”, “png”];
const options = {
types: fileTypes,
canSelectMultiple: true
};
const pickedFiles = await fs.getFileForOpening(options);
if (pickedFiles) {
pickedFiles.forEach(pImg => {
srcPathTokken.push(fs.createSessionToken(pickedFiles))
});
}
Blockquote
// and in addition to that can any expert guide me how to handle the flow next time for a structure like that
Thanks
When you push to the array why not push an object that includes the filename and the token as properties?
3 Likes
Maher
April 21, 2023, 7:55am
3
a token is a string and not an object, so as it is you can’t get anything from it.
your options are:
convert it back to entry when needed and get name from entry (not recommended)
myEntry = await fs.getEntryForPersistentToken(myToken)
fileName = myEntry.name
get and store file path and/or name from entry while converting it to a token
I’d store my tokens as an array of objects
let fileTypes = ["jpg", "jpeg", "png"];
let options = {
types: fileTypes,
allowMultiple: true //not canSelectMultiple
};
let myFiles = await fs.getFileForOpening(options);
if (myFiles){
let myTokens = myFiles.map((myEntry) => {
return {
name: myEntry.name,
path: myEntry.nativePath,
token: fs.createSessionToken(myEntry)
}
})
}
now the array called myTokens
will contain a list of objects
[{
name: "file1.jpg",
path: "D:\myFolder\file1.jpg",
token: "session-token/xxxxxxxxxxx"
},
{
name: "file2.jpg",
path: "D:\myFolder\file2.jpg",
token: "session-token/xxxxxxxxxxx"
}]
to loop through it:
myTokens.forEach(myToken => {
console.log(myToken.name);
console.log(myToken.path);
console.log(myToken.token);
})
you can also store just the path of the file and get name like this:
let fileName = path.basename(myToken.path)
1 Like
because i am too weak in programming,
got the solution now. Thankyou