Smart object window listener in Photoshop scripting - is it possible?

Hi everybody,

First post here!

Im not that tech savvy but with the help of chatGPT i managed to build one or two fully functioning scripts that serve my purposes.

My first question is the following:

I am trying to make the script have a GUI where it asks the user to select some options in the form of buttons.

One of the options is to edit an smart_object content.

meaning, the script will open the smart_object specified by name.

the issue im encountering is that once the script takes the user (me) inside the smart object, other actions keep running and i need it to wait until I either save or close the new smart object window.

ChatGPT says it doesnt know or there is no way as of now for Photoshop to listen, understand or to flag when the user saves or closes the smart object window.

It offered a solution which was to use a timer, but when i try that function, it simply freezes and have to close photoshop in order to continue.

This is the function it is suggesting.

 // This function sets a timer to repeatedly call checkSmartObjectClosure
    function waitForSmartObjectClosure() {
        if (!smartObjectClosed) {
            $.sleep(1000); // Wait for 1 second before checking again
        } else {

All i want is to know if there is a way to make it within an script that it must wait for me to save/close the smart object content window and then for it to continue?

if there is, what function should I use? if not,can this be achieved in any other way?

Basically, the script is looping and replacing some images from a source folder and putting them on the smart object, but for some i want to edit them a bit and then after that i want it to continue the process.

Hope this makes sense.

Is this possible? can we create a listener or something? I know chatGPT is not the best but its what i got and if i provide the right resource i can make it happen with your help! Thanks! :smiley:

I’m gonna be really blunt here - don’t use chatGPT to code, it’s confused af.
You’d be much better off just learning JavaScript for real, and then you’ll be able to debug your code yourself.
Coding is like, 5% syntax and 200% understanding what your code does on a granular level.
Sure, you want a one page website, chatGPT will do it. The more complex the problem the less chatGPT can help. Also, it’s knowledge is two years old, UXP has come a long way since then.
It’s worth noting that language models like chatGPT are not “intelligent” they just draw connections between words commonly associated with each other.
I’ve done extensive testing and chatGPT always starts spitting out bullshit after a certain level of complexity.
I recommend this course , it took me from a copy paste coder to a, for want of a better term “a proper coder”.
That all said, you appear to have a logical mind and are already debugging your process, someone in the forum might be able to help, alas it ain’t me.

1 Like

Hey Timothy.
Thanks for the response, appreciate your comment.
Honestly, I’ve tried learning a few years ago from (now linkedin learning) but for some reason i couldnt get past css and Html.

I know how coding structure works so to speak, i just lack the syntax knowledge of the other languages.

Now, putting this apart, i managed to get 2 fully functioning scripts from chatGPT :rofl: Believe it or not, after some troubleshooting and proepr guidance it can give good results but other times, like now, gets stuck since as you said, doesnt know past certain year knowledge.

Having said that, all i want to know basically, is if it is possible to make it within photoshop a function that allows photoshop to get inside the smart object and wait for the user to close the smart object window or save/close it?

that is all i want to know and to know how i can i use this in the code, meaning, whats necesssary for this to run properly.

Whats the proper functionality i should include here for that?

I can learn how to code but still i lack what photoshop can do and what functions does it recognize.

Hope you can guide me in the right direction regarding this!

Note: I’ll take another chance at that udemy course, seems interesting. thanks! :smiley:

In case this helps, here’s the logic of my script:

Basically we are automating the process of applying images to a PSD template. Currently,

The script places the selected images into smart objects and fills a solid color layer to match the image color.

Here’s a summary of the logic:

  1. The script initializes the process by asking the user to select folders containing the PSD files, image files, and a folder for exporting the output.

  2. It then opens each PSD file, searching for a layer named “color,” and a “smart_object”.

  3. It processes each image file placing it into the corresponding smart object.

  4. Next, it prompts the user to select a color using the color picker. The color is then applied to a “color” layer (regular normal layer) using a fill layer with color function.

  5. A Window dialog appears and asks the user to confirm if the selected color is okay or change the color.

Mission: Having said this, i would like to add an additional step between steps 3 and 4, where after the image has been placed into the smart object layer, it allows the user to edit the placed image and only after the user saves or exits/closes the recently opened smart object window, then the script continues to step 4. Hopefully this makes more sense. Thats why i’d like to not having this split into 2 scripts.

function waitForSmartObjectClosure() {
        if (!smartObjectClosed) {
            $.sleep(1000); // Wait for 1 second before checking again
        } else {

JavaScript doesn’t have a sleep method due to its asynchronous nature. Whilst you could replicate this approach using setTimeout and checking if app.documents contains the smart object document it would be more elegant to use an event listener for the document close event and then in the callback function compare the document id to that of your smart object document.

I don’t think it’s as clear cut as that though.
A smart object won’t have a document id until it’s been opened so you’d need an event listener for that.
You’re also probably going to need to store open smart object details in an array to handle the user opening multiple smart objects simultaneously.
Would the user be able to open a second document and start the whole process from scratch (and as such meaning there are two instances of your workflow)? If so, you’re going to need to track which document each smart object originates from.

I think as a high level wizard/pipeline this sounds pretty reasonable. But the problem is that user interactions are inherently messy. It’s one thing to show a user a modal dialog and all they can do is enter something and click ‘ok’ or ‘cancel’ but it’s another thing to allow the user to take over control of the app again. That’s where the difficult part lies: you yielding control back to the user. While you envision that the user at that point is properly doing everything as they where part of your recipe, they can choose to do whatever, e.g. (as @Timothy_Bennett pointed out) opening your process a second time, adding new layers to your smart object which are smart object themselves, taking a break and opening a completely unrelated image by mistake, etc., etc.). Users may do all of these things not out of bad intent but simply out of mistake. The point is: you will have to handle all of these cases or make sure they can’t happen. This is where the core of the issue lies. While you run the first part of your script you are in charge and can track what’s happening. Once you yield back to the user, they are essentially in charge. Assuming you can get back in charge (e.g. through an event listener as @Timothy_Bennett suggested) you’ll need a way to reconcile the new application state to the state before yielding.

To answer your original question and sum this up: I don’t think there is a high-level construct that would make this straight forward. What I would highly recommend is, if you have an hour or so to spare, read through the existing UXP documentation, e.g. its high level concepts, modules, etc. It may look dry but it might also give you ideas on what infrastructure exists. And it might actually help you come up with an alternative plan to solve this.

1 Like

@Timothy_Bennett @dotproduct I appreciate you both’s comments and thoughts on the current process im trying to achieve.

After thorough reading here and there and asking, i think this is simply out of my knowledge scope.

I managed to solve my main issue by applying a Mask on the smart object that is going to be replaced by the images, so that the part that i want to hide, is hidden in most of them, since the part i wanted to edit is mostly on the same spot on all of them.

Although, being able to achieve the functionality of editing the smart object content, then closing and the script continuing on doing something else would be a nice addition to my current workflow.

I guess i will let experts solve this in the future for now.

Cheers everyone!

This is recursion… it will take about 10-20 minutes to get a call stack overflow error

1 Like