Does this command not work anymore?

Specifically “exportSelectionAsFileTypePressed”

I have tried the official sample and just about every variation I can find in the forums and I cannot seem to save a file to any directory including the plugin temp or the plug in data.

const temp = await fs.getTemporaryFolder();
await core.executeAsModal(async () => {
const export_batchplay_results = await action.batchPlay([
{
                _obj: "exportSelectionAsFileTypePressed",
                _target:{
                    _ref: "layer",
                    _id: myLayerID
                 },
                fileType: "png",
                quality: 32,
                metadata: 0,
                destFolder: temp.nativePath,
                sRGB: true,
                openWindow: false,
                _options: { dialogOptions: "dontDisplay" }
            }
], {});
},{ commandName: "exporting"})

I’ve tried adding synchronousExecution: true to the batchPlay just to see what happens but makes no difference. I dont get any error returned in the response from batchPlay either. Maybe its busted?

I think you’re supposed to pass a file token, not a path in batchPlay commands. It’s a bit misleading since the parameters are still called ‘folder’ or ‘path’, but in my experience it requires a file token. I wrote about this in a recent post.

In a nutshell you can obtain a token by calling createSessionToken and providing your folder entry, as described here.

2 Likes

Hmm ok I have tried a few different things. This is what I have at the moment:

const {storage} = require('uxp');
const {app,core} = require('photoshop');

const fs = storage.localFileSystem;
const temp = await fs.getTemporaryFolder();
const token = fs.createSessionToken(temp);
const export_img = {
    _obj: "exportSelectionAsFileTypePressed",
    _target:{
        _ref: "layer",
        _id: 4
    },
    fileType: "png",
    quality: 32,
    metadata: 0,
    destFolder: token,
    sRGB: true,
    openWindow: false,
    _options: { dialogOptions: "dontDisplay" }
};
await core.executeAsModal(async () => {
    const export_batchplay_results = await app.batchPlay([export_img], {});
    console.log(export_batchplay_results);
},{commandName: "Export"})

It still doesn’t export an image to the temp directory. I’ve tried passing different things to the destFolder like “temp.url” and “temp.nativePath”. I never get any error thrown or any error returned from the batch play results. Im running on Windows 10 and PS 25.4. Im not sure what else to try. Btw, are there any official docs on each of the batchplay commands? Thanks!

And just to double check, you do have a layer with id=4 (which is your current target in the descriptor)? I’m not familiar with this particular batchPlay command. I typically use Alchemist to get a descriptor and then modify the target to make sense.

Yes, I have also tried selecting things different ways like:

 "_target": 
           {
              "_ref": "layer",
              "_enum": "ordinal",
              "_value": "targetEnum"
           }
        

Btw how can I see this command in Alchemist? File > Export > Quick Export as PNG?

Ok, but those are different things. The thing that you just posted grabs the active layer (do you have one selected?). The original code grabs one particular layer (assuming one exists with id=4, which isn’t an index btw.).

@Jarda is your man for Alchemist :slight_smile: He would probably know. The way I typically use it is by recording what I do in Photoshop and then looking at the generated descriptor code and then wrap it in a function adjusting targets to make sense.

Btw. are you trying to export the whole document or a particular layer? If you’re trying the former then I would definitely recommend using the API (check out these docs). It would literally be one line of code. (For the latter you could probably also use the API, with a few more lines of code).

Ok, but those are different things. The thing that you just posted grabs the active layer (do you have one selected?). The original code grabs one particular layer (assuming one exists with id=4, which isn’t an index btw.).

Oh yes I know, I do have a layerID that is 4 (I verified that by running a select command in Alchemist with layerID 4)

I have tried the imaging API but it seems like it outputs a jpg? Is there a way to get png from it?

The document.saveAs method is interesting. Does that open a user dialog by default?

I’m not talking about the imaging API. I’m talking about the UXP Photoshop API. Here’s the link to the docs again: https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/document/#saveas

So it would basically be something like this (assuming you have a reference to your document):

let entry = await require('uxp').storage.localFileSystem.getFileForSaving("target.png");
document.saveAs.png(entry);

You can pass options (you can look at the docs for details). This won’t open a user dialog. Only getting the file for saving will bring up a dialog to provide the output location.

Is there a way to suppress the dialog for getFileForSaving? I don’t see any option in the docs for that. I appreciate your help! thanks!

That particular function will always bring up a dialog. However, there are locations (e.g. temp folders or the plugin folder, for which you don’t need access permissions and you can create a temp file in there). You can also access a random location on disk by providing a file path. If you search for any of those key words in the forum you will find a plethora of threads (there’s practically at least one person per week asking this :grin:). E.g check out this post, from myself a while ago.

I would also recommend looking through the docs for file access and read it in full to understand what you can do with and without permissions from the user.

So I tried this:

const {app} = require('photoshop');
const {storage} = require('uxp')
const document = app.activeDocument
const fs = storage.localFileSystem;
const temp = await fs.getTemporaryFolder();
let entry = await fs.getFileForSaving("target.png",{initialLocation: temp.nativePath});
document.saveAs.png(entry);

This still will spawn a dialog even with the domains.appLocalTemporary and I suppose that is expected.

Looking at the file access for manifest it looks like all I need is "localFileSystem": "plugin" in the permissions section but that is the default value anyways.

As far as the original question of does the command “exportSelectionAsFileTypePressed” work anymore, it seems to me like that is no (I would love to be wrong about that). So now I’m looking into some other options to save a png to the temp plugin directory. Thanks for your help!

I don’t quite understand why you still use getFileForSaving. Again, that will bring up a dialog always. That is the way to ask a user to specify a file (and give permission to access it).

In your case you’re already getting a temp folder, via getTemporaryFolder. You have trivial access to that folder (so no pop-up will occur). Now it’s just a case of creating a file in that folder via createFile (as described here) and pass that to saveAs.

P.S. There’s an almost identical thread in this forum from just a week ago. The person there wanted to save a .jpg. It sounds like you want to save a .png. Check it out.

This should do what you ask,
if you have problems fix the manifest file.

document.getElementById("btnPopulate").addEventListener("click", test1);

async function test1() {

const app = require("photoshop").app;
  const fs = await require("uxp").storage.localFileSystem;
  const formats = require("uxp").storage.formats;
  const currentDocument = app.activeDocument;
  const tempFolder = await fs.getTemporaryFolder();
  const file = await tempFolder.createFile("pollo2.png", { overwrite: true });
  console.log(file, "fileToken");

  currentDocument.saveAs.png(
        file,
        {
          quality: 32,
        },
        true
      );

   const arrayBuffer = await file.read({ format: formats.binary });
   console.log(arrayBuffer, "arrayBuffer");

}
2 Likes

Thanks! I think I’ve come full circle now lol. This is what I started with before I made this post. For some reason I thought it would only output a jpg.

2 Likes