Check if pattern exists?

Is there any way to check to see if a pattern exists by the name?

What you mean by “pattern”? Please elaborate I don’t get very well what you mean exactly.

Are you asking for layers names? pixels? Pattern of what?

I am not sure how to check if they exist, but a workaround I use. Include a psd with a pattern fill layer, or layer with a pattern layer style in your plugin files and then you can import that and pull the pattern from that file.

Also,are you going to be at SPAC next week?

No, I won’t be at SPAC. Wanted to go but just too much going on.

For the pattern fill, I can get it to create one just fine. The issue is seeing if it already exists so I’m not creating redundant patterns in batch for every image that runs through it…. as that piles up the patterns in a hurry.

One thing I can maybe do is a try/catch on this fill step it if doesn’t work when using the pattern fill assume the reason is because it doesn’t exist and then create it…. but I don’t like that because if it does exist and still throws an error, it would continue creating extra and the user ends up with a pile of them.

In the old extendscript, you could check to see if they exist, but it seems that UXP doesn’t work the same way and give a “command get not available” when trying to get the array of patterns.

maybe try/catch deleting and then creating so if it did exist you are deleting it first?

I already tried that but it won’t work because there is a pattern ID for each one. The ID is randomly generated when the pattern is made so there isn’t a way to know what the ID is unless you can get the list of patterns.

Multiple patterns can have the same name so you need the ID to delete it.

In extendscript, you could get an array of the patterns, brushes, etc. There must be a way to do it in UXP.

found it…it is under $PttR object

const { core, action } = require(“photoshop”);

async function listPatterns() {
const result = await action.batchPlay([
{
_obj: “get”,
_target: [
{ _ref: “property”, _property: “presetManager” },
{ _ref: “application” }
],
_options: {}
}
], {});

// The result should contain pattern presets
console.log(JSON.stringify(result, null, 2));
return result;
}

await core.executeAsModal(listPatterns, { commandName: “List Patterns” });

I don’t see a way to get the id though but at least you can check if it exists by the name.
This post has some ideas at the bottom that might be able to be translated into a workaround if you need the id but did not dig in too much.

Thanks, I will play around with it more tomorrow and see if I can get it to work.