Deletion of selection via BatchPlay fails to work, with no errors

I am attempting to condense the following process into a button. With the layer of interest selected:

  • Select → Subject
  • invert selection
  • delete selection
  • deselect

I recorded the respective batchplay descriptors via Alchemist, and collated them into an array, resulting in the code below:

async function selectAndDeleteWhitespace() {
	const batchCommands = [
		{
			_obj: "autoCutout",
			sampleAllLayers: false,
			_options: {
				dialogOptions: "dontDisplay"
			}
		},
		{
			"_obj": "autoCutout",
			"sampleAllLayers": false,
			"_isCommand": true
		},
		{
			"_obj": "inverse",
			"_isCommand": true
		},
		{
			"_obj": "delete",
			"_isCommand": true
		},
		{
			"_obj": "set",
			"_target": [
				{
					"_ref": "channel",
					"_property": "selection"
				}
			],
			"to": {
				"_enum": "ordinal",
				"_value": "none"
			},
			"_isCommand": true
		}];
	return await require("photoshop").core.executeAsModal(async () => {
		await require('photoshop').action.batchPlay(batchCommands, {});
	}, { commandName: "Select and Delete" });
}

Whenever the button is pressed, it merely selects the pixels (the first step). When I press the button again, I get a modal dialogue warning me about a deletion. When I confirm the modal dialogue, nothing happens. Throughout this, the console records zero errors. Not sure what’s going on.

I’m assuming that there is some missing information when retrieving the event descriptor, that Alchemist didn’t pick up, based on a similar thread where someone was having trouble deleting all of the selected pixels.

I’ve done some additional debugging. I refactored the code into something more readable. This time, I simply have an action button which ostensibly deletes the selected pixels in a layer, based on the recorded Alchemist event descriptor readout:

async function deleteSelection() {
  const batchCommands = [eventDeleteSelection];
  return await executeAsModal(batchPlay(batchCommands, {}), { commandName: "Delete Selection " });
}

where

const eventDeleteSelection = {
  "_obj": "delete",
  "_isCommand": true
};

Still, annoyingly, no errors, and I’ve extensively searched through the Photoshop API to see if it offers any clues for how to do this simple task. No luck, so far. Are there any example scripts in the uxp-photoshop-plugin-samples that might shed some light?

I managed to get this to work by deleting the line "_isCommand": true from the event descriptor:

const eventDeleteSelection = {
  "_obj": "delete",
  // "_isCommand": true (comment this one out)
};

Why does this work? I have no idea. Is it documented anywhere? Nope! :smiley: (seriously, I am desperate for documentation on this stuff, even if it’s auto-generated. I’m surprised that UXP has been out for almost 2 years now and the information surrounding batchPlay() remains so scarce.

Seriously: https://developer.adobe.com/photoshop/uxp/code_samples/batchplay_samples/
“batchPlay examples are coming soon.”
“Last updated 8/12/2021” :grimacing:
UXP has now been available to the public for 3X as long as it was when that documentation was written.

Rule of thumb - always remove _isCommand key. When I started with UXP I also had issues with it. Don’t use it anywhere and less problems

1 Like

Thanks for this suggestion! I’ll try removing it by default until I run into issues doing that.