Skip to main content
Inspiring
April 26, 2013
Question

A "preview" checkbox with JavaScript

  • April 26, 2013
  • 2 replies
  • 3965 views

Hi friends.

I´m writting a JavaScript that sends the active document in an appropriated way to print. Before calling the print command, I wanna display a dialog box asking the number of copies to print. So I thought I could also use this box to show some "auto" adjustments options (for example "auto levels"). If the user active "Apply auto-levels" then image refreshes to show the result (like a live preview). If user disables this box, then auto levels must be retired from image.

Well..hope you do not laught on my solution. The way I found to do it is..apply auto levels and refresh when user enables the field, and return in the history and refresh if the user disables this field. It get the expected result. BUT, if I have more checkboxes in the same dialog box, then this solution will not work so well (sure).

Do you have any other consistent solution to have previews of commands, but retire if the user disables the field?? Would like to learn a little about it.

Well..what I do (the solution I´ve mentioned above) is:

var doc = app.activeDocument;

var c = doc.activeLayer;

var dial = new Window ("dialog", "Print", undefined, {closeButton:false});

dial.orientation = "column";

dial.alignChildren = "fill";

dial.margins = 20;

var dPanel = dial.add ("panel", undefined, "Print options:");

dPanel.orientation = "column";

dPanel.alignChildren = "left";

dPanel.margins = 15;

var aLevels = dPanel.add ("checkbox", undefined, "Apply auto levels");

var dGroup = dial.add ("group", undefined);

dGroup.orientation = "row";

dGroup.alignChildren = "left";

var st = dGroup.add ("statictext", undefined, "Copies:");

var nField = dGroup.add ("edittext", undefined, "1");

nField.characters = 10;

var gButtons = dial.add ("group", undefined);

gButtons.orientation = "row";

gButtons.alignChildren = ["right", "right"];

var exe = gButtons.add ("button", undefined, "Execute", {name:"ok"});

var canc = gButtons.add ("button", undefined, "Cancel", {name:"cancel"});

aLevels.onClick = function (){

   

    if (aLevels.value){

        c.autoLevels();

        app.refresh();

    };

    else

    if (! aLevels.value){

        doc.activeHistoryState = doc.historyStates[doc.historyStates.length-2];

        app.refresh();

    };   

};

   

dial.show();

Any idea to forget all of this and make a real "preview" checkbox?

This topic has been closed for replies.

2 replies

DBarranca
Braniac
September 21, 2013

Hello Gustavo,

I've been using (or seen using) preview in a couple of ways:

1. Nesting when possible adj.layers into Groups and toggling their visibility and/or using smart filters on smart objects;

2. Adding, and switching from, history states. Which is handy because you don't toggle only between original and "filtered", but if the UI of your script permits it, between original, "filtered with settings #1", "filtered with settings #2", etc. giving to users the extra feature of comparison.

An option similar to #2 (that I've adopted when the processing is particularly elaborate) is to duplicate the original doc, work on it and paste back the result as a new layer. You can use the same strategy of multiple settings previews there too, but without the need to resort to snapshots.

I've been working for a while on an embedded preview (in the ScriptUI window) but never found a robust workflow for it.

Cheers

Davide Barranca

www.davidebarranca.com

Inspiring
September 21, 2013

Hey Davide

Thank you for the comments. These ways of working can be very useful (I like the adjustment layers way, I have a script that tries to do similiar thing) haha

Best Regards

Gustavo.

Inspiring
April 26, 2013

I don't know of a good way to create an 'one the fly' preview with javascript that can be used in a dialog.

One thing that might work for you is if you used adjustment layers.Checking the control creates the layer. unchecking either deletes that layer or turns the visibility off.

Or when the script start it creates a snapshot. Then have all the controls that affect the document's state call one function that set the history to the snapshot and applies all the controls that are checked.

Inspiring
April 27, 2013

Hi Michael

Grat idea. Using adjustment layers I can even work with buttons..so I leave the adjustment to the user do before printing. Very good.

I´m trying to create the adjustment layers in the document...but I´m getting an error. See it:

var c2 = doc.artLayers.add();

c2.kind = LayerKind.CURVES //error here

It returns an error telling I can set only "Normal" and "Text" to layer kind. So how to create a Curves Adjustment layer???

Thank you a lot

Best Regards

Gustavo.

Inspiring
April 27, 2013

The guide is wrong or at best mis-leading. You can only change the kind to text using the Object Model. And then only if the layer is a blank layer.

For the other layer kinds you need to use scriptlistener( Action Manager ).

I am not clear about what you want to do, but the user will not be able to change the adjustment layers while the dialog is open.