Slice exporting/Save For Web - cant hardcode the settings in "Save optimized as" window

Hi everyone, Im trying to automate my slice exporting process and I almost got my plugin working, but Its just that one thing in my way. When you open Save for Web(legacy) and you hit “save” there is this second window that appears (called “Save optimized as”) and there is the “Slice:” dropdown where you have - “All slices”, “All user Slices” and the one that im after “Selected Slices”.

It seems that this options depends on the last export, and no matter what I do, if the last export was say with “All slices” selected, photoshop disregards my code (which says “Selected slices”) and falls back to “All slices”. Now the cavemen workaround would be to export once manually(with the desired option) and then never touch this option ever again but thats kind of dumb and very user unfriendly.
It also seems that Photoshop sometimes “forgets” this setting and fall back to default.

The logic behind my script is to export slices one by one and im using this the following batch action.
It works fine till the moment someone change the options in “Save optimized as”.
The descriptor for my option has to be this thing - $SWsl: { _enum: “$STsl”,_value: “$SLSs”},
where $SLSs is “Selected slices” and the “All slices” has to be $SLAL.
Any help would be greatly appreciated as my workflow heavily relies on slices and there is no way around it.

const result = await batchPlay(

  \[

     {

        \_obj: "export",

        using: {

           \_obj: "SaveForWeb",

           $Op: {

              \_enum: "$SWOp",

              \_value: "$OpSa"

           },

           $DIDr: true,

           in: {

              \_path: await tokenifyTEST(exportPath),

              // \_path: await tokenify(exportPath),

              \_kind: "local"

           },

           pathName: uxpPath,

           format: {

              \_enum: "$IRFm",

              \_value: "$PN24"

           },

           interfaceIconFrameDimmed: false,

           transparency: true,

           $Mtt: true,

           $EICC: false,

           $MttR: 255,

           $MttG: 255,

           $MttB: 255,

            $HScl: {

                \_unit: "percentUnit",

                \_value: 100

            },

            $VScl: {

                \_unit: "percentUnit",

                \_value: 100

            },

           $SHTM: false,

           $SImg: true,

           $SWsl: {

              \_enum: "$STsl",

              \_value: "$SLSs"

           },

           $SWch: {

              \_enum: "$STch",

              \_value: "$CHsR"

           },

           $SWmd: {

              \_enum: "$STmd",

              \_value: "$MDCC"

           },

           $ohXH: false,

           $ohIC: true,

           $ohAA: true,

           $ohQA: true,

           $ohCA: false,

           $ohIZ: true,

           $ohTC: {

              \_enum: "$SToc",

              \_value: "$OC03"

           },

           $ohAC: {

              \_enum: "$SToc",

              \_value: "$OC03"

           },

           $ohIn: -1,

           $ohLE: {

              \_enum: "$STle",

              \_value: "$LE03"

           },

           $ohEn: {

              \_enum: "$STen",

              \_value: "$EN00"

           },

           $olCS: false,

           $olEC: {

              \_enum: "$STst",

              \_value: "$ST00"

           },

           $olWH: {

              \_enum: "$STwh",

              \_value: "$WH01"

           },

           $olSV: {

              \_enum: "$STsp",

              \_value: "$SP04"

           },

           $olSH: {

              \_enum: "$STsp",

              \_value: "$SP04"

           },

           $olNC: \[

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC00"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC19"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC28"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC24"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC24"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC24"

                 }

              }

           \],

           $obIA: false,

           $obIP: "",

           $obCS: {

              \_enum: "$STcs",

              \_value: "$CS01"

           },

           $ovNC: \[

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC01"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC20"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC02"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC19"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC06"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC24"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC24"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC24"

                 }

              },

              {

                 \_obj: "$SCnc",

                 $ncTp: {

                    \_enum: "$STnc",

                    \_value: "$NC22"

                 }

              }

           \],

           $ovCM: false,

           $ovCW: true,

           $ovCU: true,

           $ovSF: false,

           $ovCB: true

        },

        \_options: {

           dialogOptions: "dontDisplay"

        }

     }

  \],

  {}

);

Apparently the last used Save for Web settings are stored in this .psp file:

A temporary workaround I found is to make an export with the right settings(Selected slices), close Photoshop and make the .psp “Read only”. This way it falls back to “Selected slices” even if I make an export with something different. The tradeoff is I lose some other functionalities like storing the last export path which is not a big deal really but still…There has to be some sane way of doing that.

Summoning the elders of UXP. @Jarda can you help me wise master?

Im sharing the code to:
get the list of all user slices
export only the first slice in the list (exports to a hard coded path - change it to something relevant)

Way to reproduce:

Make a dummy export through File > Export > Save for Web (Legacy) with the “Selected slices” option enabled. Run the plugin, and it will export only one slice as expected. Next, make another dummy export using the “All slices” option instead. Run the plugin again, and this time it exports all slices instead of just one.

ExportSingleSlice.js (13.1 KB)

Hi! I am not old enough to master “save for web” in such details. But maybe Imaging API would be easier to handle? You can set source bounds meaning those could match your slice: https://developer.adobe.com/photoshop/uxp/2022/ps_reference/media/imaging/#getpixels

1 Like

Hi Jarda, thanks for the link. I’ve been looking into this, but I still don’t fully grasp it. What I’m trying to do is batch export slices through code without all the screens flashing (creating temporary documents, exporting, and then jumping back to the original document etc). The Save for Web workflow already does exactly what I need, the only problem is that I can’t seem to control the export options with code.

I think imaging API can do exactly what you ask for. You can still define slices, you can read all of its details including coordinates etc. But use imaging API to export image files.

1 Like

I really hope it does man. I`ll dig deeper. Slices are such a convenient way to export images, but they’re such a pain to work with. What would be the theoretical workflow with imaging? Get an object and pass it to some export function?