• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

Hi everybody,

 

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) {
            checkSmartObjectClosure();
            $.sleep(1000); // Wait for 1 second before checking again
            waitForSmartObjectClosure();
        } else {
            callback();
        }
    }

 

 

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? Chatgpt says its not possible as PS cannot listen to when the new smart object window closes or saves...

 

if there is, what function should I use? if not,can this be achieved in any other way? All i want is a pointer i can read and provide to chatgpt if this can be achieved.

 

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! 

TOPICS
Actions and scripting , Windows

Views

681

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

You need to split the script into two parts. The first one ends with the opening of the smart object and setting the second part (the second script) as a script to run on the "file close" event using the ScriptEventManager.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

Simplified code (as a template)

 

if (!arguments.length) 
    part1();
else 
    part2();

function part1()
    {
    try {
        alert("Now we are going to open the smart object");

        executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.ERROR);

        app.notifiers.add("close", new File($.fileName));
        app.notifiersEnabled = true;
        } 
    catch(e) { alert(e); throw(e); }
    }

function part2()
    {
    try {
        app.notifiers.removeAll();
        app.notifiersEnabled = false;

        alert("This is the part2 code");
        } 
    catch(e) { alert(e); throw(e); }
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

Thanks for the assistance here. Much appreciated.

It makes sense.

Although, not sure if doing it like this will serve a purpose for me at the moment.

 

The reason is, if it is opening an image, placing it in the smart object and opening the smart object,

then running the other script (2nd one), it will work only for this first image.

 

But if i run the first script again to open another image, it may take the same image it took at the beginning, without allowing me to loop through them.

 

in that case, i guess i am better off just placing the smart object iamge myself and then running the script after i place it and edit it. But then again, there would be no way to loop through multiple images.

 

Unless I make it so that I add all the images i want inside the smart object and make the script to delete image after image after it finished saving them.

 

But this would be me complicating everything...

 

All i want is for it to wait for me to close the smart object window haha...

 

Is there absolutely no way to achieve this? even with a proper plugin or UXP or anything like that?

 

I dont care if its not possible only with a script, but i want to know if its possible at all.?

Thanks for your input, once again and for taking the time to understand my concern.😁

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

I don’t really understand your process, but you can configure the script so that it only responds to closing the smart object file it previously opened.

 

if (!arguments.length) 
    part1();
else 
    part2();

function part1()
    {
    try {
        alert("Now we are going to open the smart object");

        executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.ERROR);

        var d = new ActionDescriptor();
        d.putInteger(0, app.activeDocument.id);
        app.putCustomOptions("_WORKING_FILES_INFO_", d, true);

        app.notifiers.removeAll();
        app.notifiers.add("close", new File($.fileName));
        app.notifiersEnabled = true;
        } 
    catch(e) { alert(e); throw(e); }
    }

function part2()
    {
    try {
        var d = app.getCustomOptions("_WORKING_FILES_INFO_");
        var id  = d.getInteger(0);

        for (var i = 0; i < app.documents.length; i++)
            {
            if (app.documents[i].id == id) return;
            }

        app.notifiers.removeAll();
        app.notifiersEnabled = false;

        alert("Our smart object is closed");
        } 
    catch(e) { alert(e); throw(e); }
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

I understand. Thanks for the update. I understand the logic, although, here is a summary of what i am trying to achieve. 

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 the updateColorFillLayer 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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

Wouldn’t 

activeDocument.save();
activeDocument.close();

suffice to make sure the edited SO is closed? (At least if saving in Actions and Scripts wouldn’t have been made impossible by a bug in Photoshop 24.3.0) 

https://community.adobe.com/t5/photoshop-ecosystem-bugs/p-program-error-with-actions-that-have-save-...

Which Photoshop version are you using? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

Hey c.pfaffenbichler, thanks for chiming in.

 

Im using PS version 24.1. 

 

How exactly should i be using these ?

 

activeDocument.save();
activeDocument.close();

 

 
Could you please provide me with a very short script example on photoshop on doing something like the following? Or pointing me into the documentation to achieve this, like how should the code be writtent to achieve this something like the following:

1. opening the smart object

2. Waiting for me to edit the contents of the SO and waiting for me to close or save/exit this window

3. Continuing to do something else?

 

From what i have read, photoshop does not have a way for me to make it wait for the new window that opens from the smart object contents to be closed, in order to continue with other process within the same script.

 

Does this make sense? 🙂 I thought it was possible.

 

I found this text below, but not sure if it is entirely true. I thought someone from the community could provide some pointers into how to achieve this? spliting the script into 2 would make no sense for me to be using a script at all then haha

 

quoteUnfortunately, there is no built-in event listener or callback mechanism in Photoshop scripting to detect when a Smart Object is closed. This limitation makes it difficult to automatically continue the script execution after the Smart Object window has been closed.
 

However, as an alternative, you can use a workaround that involves splitting the script into two parts. In this approach, the user would need to run the first part of the script to replace the smart object and apply the color. Then, they can manually open the smart object to edit it. After saving and closing the smart object, the user would run the second part of the script to export the result as a PNG.

 

This alternative is of no use to me unfortunately, i need it all in one script, ideally of course.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

Try this; SO needs to be selected, it gets openend, a Solid Color Layer is added, it is saved and closed, then another Solid Color Layer is added in the now active document. 

I didn’t roll back from 24.3.0 so I can’t test it myself at current. 

openSmartObject ();
createSolidColorLayer (128,128,128);
activeDocument.save();
activeDocument.close();
var theSolidColorLayer = createSolidColorLayer (255, 0, 0);
////// open smart object //////
function openSmartObject () {
    var desc2 = new ActionDescriptor();
    executeAction( stringIDToTypeID( "placedLayerEditContents" ), desc2, DialogModes.NO );
    return activeDocument;
    };
////// make solid color layer //////
function createSolidColorLayer (theRed, theGreen, theBlue) {
    var desc8 = new ActionDescriptor();
        var ref6 = new ActionReference();
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        ref6.putClass( idcontentLayer );
    desc8.putReference( charIDToTypeID( "null" ), ref6 );
        var desc9 = new ActionDescriptor();
            var desc10 = new ActionDescriptor();
                var desc11 = new ActionDescriptor();
                desc11.putDouble( charIDToTypeID( "Rd  " ), theRed );
                desc11.putDouble( charIDToTypeID( "Grn " ), theGreen );
                desc11.putDouble( charIDToTypeID( "Bl  " ), theBlue );
            var idRGBC = charIDToTypeID( "RGBC" );
            desc10.putObject( charIDToTypeID( "Clr " ), idRGBC, desc11 );
        desc9.putObject( charIDToTypeID( "Type" ), stringIDToTypeID( "solidColorLayer" ), desc10 );
    desc8.putObject( charIDToTypeID( "Usng" ), idcontentLayer, desc9 );
    executeAction( charIDToTypeID( "Mk  " ), desc8, DialogModes.NO );
    return activeDocument.activeLayer
    };

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

Hey @c.pfaffenbichler , thanks once again!

I tried it and the script does work but doesnt really serve me at this moment as is.

 

Basically, it does create a layer inside the smart object, which is great.

But my main issue and goal , is to be able to edit the smart object myself.

 

For instance, for the script to open the SO, let me edit whatever is inside, with an eraser, brush, add an object, whatever, and then after either i save or exit this new SO window, for the rest of the script to continue. 

 

This functinoality is what i am aiming to accomplish. 
To be able to edit freely inside the smart object in some way or another. 

is this possible? 😁😁

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

LATEST

I do apologize, I had misunderstood the issue. 

 

Just go with the approach @r-bin elaborated on or use a prefs-file to store the »position« within one Script (boolean should do, for example; do part one if »true« [or if no prefs file exists yet] and part two if »false«). 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines