Save the active psd document in TIFF format in the parent folder of the active document

I’d like to save the active psd document in TIFF format in the parent folder of the active document with UXP plugin, can someone help me?

How far have you managed to get with it?
Have you any code that isn’t working that you can share to help debug your problem?

welcome to the forums :slight_smile:

ASFAIK there is no direct way of saving as TIFF without using batchplay.

so let’s break the issue down to 3 basic steps:

1. we parse the document path to get the wanted folder

const app = require("photoshop").app;
// the folder where the file is stored
let docDir = path.parse(app.activeDocument.path).dir
// going up one level...
let parentDir = path.parse(docDir).dir

2. we ask the user to give access (Entry) to said folder or one of its parents

3-1. we generate wanted action descriptor using alchemist plugin or by copying the JavaScript from a valid action.

3-2. we use batchPlay with “_path” set to the token (tokens can be permanent or temporary) generated from the entry generated using FileSystem’s createFile


as you can see there are multiple steps involved in the process that’s why we need to know what have you tried and where are you stuck,

2 Likes

Thank you for your answer, if i generate a permanent token for a folder, it is will be valid also for all the subfolders?

yes.

once the user gives you access to a folder using getFolder()

you can use getEntry(filePath), createFile(name, options)… or do whatever you want in that folder and its entries (sub folders / files).

to avoid asking the user in the future you can createPersistentToken(entry) and store that token however you see fit.

to restore access you can use getEntryForPersistentToken(token)


for storage I use something like:
localStorage.setItem('tokensObject', JSON.stringify(tokensObject));

and
tokensObject= await JSON.parse(localStorage.getItem("tokensObject"));

3 Likes

Thank you for our help :slight_smile:

i do not understand how i can recall the persistent token to create a folder

You need to save it somewhere in the persistent file storage so you could later access it

1 Like

i have saved it, but i dont understand how to recall it and create a folder

How do you save it and how do you try to access it? Do you use getDataFolder() I just linked to?

1 Like
const fs = require('uxp').storage.localFileSystem;

token = await JSON.parse(localStorage.getItem("fixfolder")); //seleziona il token salvato

const folder = await fs.getEntryForPersistentToken(token); //estrae il percorso della cartella persistente

const subfolder = await folder.createFolder("pippo");  //Crea le cartelle desiderate e le sottocartelle)
const subsubfolder = await subfolder.createFolder("paperino");


const newFile = await subfolder.createFile("newfile.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"
});

I write this code, it create the two folder correctly, but dont save the file

Please try to format your code properly. It’s a bit difficult to read when it’s like normal text :frowning:

1 Like

sorry, formatted correctly now

doesn’t save operations require executing in modal state?

1 Like

I’m not sure this does what you expect to do. This should not return persistent token. You also don’t save that token anywhere, so you cannot expect to get it later. See createPersistentToken()

1 Like

Saving files doesn’t actually change Ps state, so I’d expect you wouldn’t need modal execution :thinking:

1 Like

I have already save the token before, the token now its right, the proiblem is to save the file, i have try this code withourt success:

require('photoshop').core.executeAsModal(async () => {

const newFile = await subfolder.createFile("newfile.tif", { overwrite: true });
const saveFile = await fs.createSessionToken(newFile);

         });

P.S.
This is the code with which I saved the token previously:

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

I got strage results
I created a minimal isolated test with try/catch to know what’s going on:

async function testSave() {
   try {
      let myEntry = await fs.getFileForSaving("myFile", { types: ["TIFF"] });
      if (!myEntry) {
         // file picker was cancelled
         return;
      } else {
         await executeAsModal(async () => {
            let saveFile = fs.createSessionToken(myEntry)
            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"
            });
         })
      }
   } catch (error) {
      console.error(error)
   }
}

before using modal state I got the error:

Error: Event: save may modify the state of Photoshop. Such events are only allowed from inside a modal scope. See…

after modal I got

The command failed because Photoshop is in a modal state

please try your code with executeAsModal

@Pinus , do you get any error? Also, where do expect to find your new file? I see you try to save it in pippo and not in paperino

1 Like