Skip to main content
Participating Frequently
August 18, 2009
Answered

In javascript, how do I open a dialog so that the user can input the value of a variable?

  • August 18, 2009
  • 2 replies
  • 3425 views

I'm working on a script that will be used for many images, all of them using the same format (size of the image, layer layout, etc).

All of these images will have a layer of text, and what I want is to have the script modify the horizontal length of the text layer (horizontal percentage scale), shrinking it so that all of the text appears on-screen, in case there's too much of it.

But the text will vary from image to image, and what I want is for the script to display a dialog box were the user can input the text that will be put on the text layer and scaled down.

Can this be done?

This topic has been closed for replies.
Correct answer Michael_L_Hale

If you want the user to both enter then fit the text while your script is running, here is one way to do that.

var typeLayer = activeDocument.activeLayer;
var type = typeLayer.textItem;
var t = prompt ("Enter text" , "new text");
type.contents= t;
transformLayer();

function transformLayer() {
     try{
     var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc.putReference( charIDToTypeID('null'), ref );
    desc.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
        var desc1 = new ActionDescriptor();
        desc1.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), 0.000000 );
        desc1.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), 0.000000 );
    desc.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc1 );
    desc.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 );
    desc.putUnitDouble( charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 );
    desc.putBoolean( charIDToTypeID('Lnkd'), true );
    executeAction( charIDToTypeID('Trnf'), desc, DialogModes.ALL );
     }catch(e){}
};

2 replies

August 21, 2009

You might want to also look at this: http://ps-scripts.com/bb/viewtopic.php?t=34

Michael_L_HaleCorrect answer
Inspiring
August 18, 2009

If you want the user to both enter then fit the text while your script is running, here is one way to do that.

var typeLayer = activeDocument.activeLayer;
var type = typeLayer.textItem;
var t = prompt ("Enter text" , "new text");
type.contents= t;
transformLayer();

function transformLayer() {
     try{
     var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc.putReference( charIDToTypeID('null'), ref );
    desc.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
        var desc1 = new ActionDescriptor();
        desc1.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), 0.000000 );
        desc1.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), 0.000000 );
    desc.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc1 );
    desc.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 );
    desc.putUnitDouble( charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 );
    desc.putBoolean( charIDToTypeID('Lnkd'), true );
    executeAction( charIDToTypeID('Trnf'), desc, DialogModes.ALL );
     }catch(e){}
};

navandAuthor
Participating Frequently
August 19, 2009

Hey, thank you very much!

I wonder... would you be so kind as to explain to me the transformLayer function? I mean, I have the idea of what it does, but I've never seen anything like what you wrote in there.

More importantly... why isn't that prompt thing in the javascript reference?

Inspiring
August 19, 2009

The easy answer first. Abode moved info about alert, prompt, and confirm to the tools guide a while back. Alert is use in the sample scripts so it also appears in the scripting guide.

The function is just a cleaned up output from scriptlistner made into a function. ScriptListner is a plug-in that ships with Photoshop. It seems that details about it are also missing from both the scripting and tools CS4 guides. You copy or move the plug-in into the plug-in folder and restart Photoshop. Now almost every command or action you do is recorded in a log. When you need to script something that you can not find the guide, you do the step(s) in Photoshop and look in the log. Not everything is recorded. Painting would be an example. That is what I did with the posted function. The dialogMode is always set to NO. That lets the command take place with the recorded settings. Because user input is need I changed the mode to ALL.

You might also want to have a look at Xbytor's Xtools( do a forum search ). It has most common scriptlistner only tasks as functions as well as a few uncommon ones.