Please try to format your code properly. It’s a bit difficult to read when it’s like normal text
sorry, formatted correctly now
doesn’t save operations require executing in modal state?
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()
Saving files doesn’t actually change Ps state, so I’d expect you wouldn’t need modal execution
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
Yes, I want to save it in “pippo”, I don’t receive errors, but the promise state is “fulfilled” and the promise result is “undefined”
ok got it!
- use
executeAsModal
- remove
"modalBehavior": "fail"
it’s a good practice to wrap your code in a try/catch to see errors.
I have received the error: “ncaught ReferenceError: batchPlay is not defined
at :17:16”
const batchPlay = require("photoshop").action.batchPlay;
also see this
I use execute as modal in my example above.
const core = require('photoshop').core;
const executeAsModal = core.executeAsModal;
O, for sure
Ok, now it run correctly, thank you vry much
i dont know why, but now stopped working and generate the error:
saveFile is not defined.
this is the code:
const app = window.require('photoshop').app
const batchPlay = require('photoshop').action.batchPlay;
const fs = require('uxp').storage.localFileSystem;
//const core = require('photoshop').core;
//const executeAsModal = core.executeAsModal;
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");
//crea il file in modalità modale
require('photoshop').core.executeAsModal(async () => {
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 think that i changed nothing, but noiw say me that error
You moved batchPlay outside of exeModal. It should be inside
It works now, but it was also out before!
Thank you
Re your point that save operations require executeAsModal: I’m able to save the current document as JPEG without using executeAsModal. This works fine for me:
const save_image_as_JPEG = {
_obj: "save",
as: {
_obj: "JPEG",
extendedQuality: image_save_quality,
matteColor: {
_enum: "matteColor",
_value: "none",
},
},
copy: true,
in: {
_path: outputFileToken,
_kind: "local"
}
};
try {
await app.batchPlay([save_image_as_JPEG]);
} catch (error) {
console.error(`Error during image save`, error);
}