Skip to main content
Participant
November 19, 2024
Question

Script (JS) : how to ask user to select something in an image during a script?

  • November 19, 2024
  • 2 replies
  • 272 views

Hi all,

 

In my script, I would like to :

1. load images from a folder

2. ask the user to choose a process

3. upon the choosen process, ask the user to draw a selection (rectangular) in the 1st image of the folder image

4. process all the images taking into account the selection

 

I already wrote the code for steps 1. 2. and 4. but instead of having the step. 3 in the middle of the script, I ask the user to do it before running the script... this is not very user friendly.

 

This is why I would like to know how to insert the step 3 inside the script. I mean, the script pauses, the user makes its selection and then press a button to continue the script.

 

My code is in JS.

 

Thanks for your help.

 

Fred

This topic has been closed for replies.

2 replies

Legend
November 19, 2024

This script first executes the code for before_func().

Then it pauses for you to make a selection.

Then you must click the "Continue" button and the code for the after_func() will be executed.

 

For example, before_func() makes a copy of the layer,

and after_func() executes ctrj+j taking into account your selection.

 

var title = "Pause";

if (!arguments.length)
    {
    var ok = true;
    try { activeDocument; } catch (e) { alert("activeDocument required"); ok = false; }

    ////////////////////////////////////////
    // Tries to delete previously created palettes 
    // (if the script is called multiple times), 
    // otherwise a memory leak may occur on new Photoshops (older than CS6).
    dlg = Window.find("palette", title);
    if (dlg) { dlg = null; }
    ////////////////////////////////////////

    if (ok)
        {
        var bt = new BridgeTalk();
        bt.target = BridgeTalk.appSpecifier;
        bt.body = "$.evalFile("+$.fileName.toSource()+")";

        bt.send(5);
        }
    }
else
    {
    var dlg = new Window("palette", title);

    dlg.add("statictext", undefined,  "Create some secection and press Continue");

    dlg.b = dlg.add("button", undefined,  "Continue");

    dlg.b.onClick = function() 
        {
        try {
            dlg.opacity = 0;
            dlg.hide();
    
            after_func();
            }
        catch (e) { alert(e); }
        }        

    before_func()
    dlg.show();
    }


////////////////////////////////////////
function before_func()
    {
    try {
        // duplicate current layer

        activeDocument.activeLayer = activeDocument.activeLayer.duplicate();
        }
    catch (e) { alert(e); }
    }

////////////////////////////////////////
function after_func()
    {
    try {
        // make ctrl+j with current selection

        executeAction(stringIDToTypeID("copyToLayer"), undefined, DialogModes.NO);         
        }
    catch (e) { alert(e); }
    }

 

Legend
November 19, 2024

Photoshop has something like an eventListener called app.notifiers, so it might be possible to use that. The envisioned flow is as follows.

 

1. load images from a folder
2. ask the user to choose a process
3. define a function “process all the images taking into account the selection”
4. add a notifier in app.notifiers to trigger function 3 after the user has made a selection
5. ask the user to make a selection. Script ends here
6. function 3 is triggered when the user does the triggering event (and function 3 removes the added notifier)

 

I am not sure if this can be done in practice, and there are issues such as how to remove the notifier if the user wants to cancel during the process.