Skip to main content
tssee
Inspiring
January 1, 2016
Answered

change opacity levels individually

  • January 1, 2016
  • 1 reply
  • 3863 views

Good year

I'm new to scripting and speak little English I hope to be clear.

I would like to create a panel that I change the opacity levels individually

as in this example

second and you can make it happen?

This topic has been closed for replies.
Correct answer c.pfaffenbichler

// change layers’ opacity via scrollbars;

// 2015, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

app.activeDocument.suspendHistory("opacity", "theDialog ()");

};

function theDialog () {

//////////////////////////////////////////

var theLayers = theLayerNames ();

////// create dialog for user-entry //////

var dlg = new Window('dialog', "layeropacity", undefined);

//////////////////////////////

dlg.layer = dlg.add('panel', undefined, "opacity");

dlg.layer.orientation = "column";

dlg.layer.preferredSize = [300, 50];

// populate panel;

for (var m = 0; m < theLayers.length; m++) {

dlg.layer.add('statictext', undefined, theLayers[0]);

dlg.layer.add('scrollbar', undefined, theLayers[2], 0, 255);

dlg.layer.children[m*2+1].preferredSize = [255,20];

//////////////////////////////

// if scrolled;

dlg.layer.children[m*2+1].onChange = function () {

var theCh = dlg.layer.children.length;

for (var n = 0; n < theCh; n++) {

  if (dlg.layer.children == this) {var theNumber = n}

  };

setOpacityByID (theLayers[(theNumber-1)/2][1], Math.round(this.value));

app.refresh();

};

};

//////////////////////////////

// build- and cancel-button;

dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

dlg.center();

var myReturn = dlg.show ();

//////////////////////////////

// reset if canceled;

if (myReturn != 1) {

for (var a = 0; a < theLayers.length; a++) {

setOpacityByID (theLayers[1], theLayers[2])

}

};

$.gc();

};

////////////////////////////////////

function theLayerNames () {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var theOpacity = layerDesc.getInteger(stringIDToTypeID('opacity'));

theLayers.push([theName, theID, theOpacity])

};

}

catch (e) {};

};

return theLayers

};

////// set opacity //////

function setOpacityByID (theID, theOpacity) {

// =======================================================

var idsetd = charIDToTypeID( "setd" );

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

    var idLyr = charIDToTypeID( "Lyr " );

        var ref1 = new ActionReference();

        ref1.putIdentifier( idLyr, theID );

    desc2.putReference( idnull, ref1 );

    var idT = charIDToTypeID( "T   " );

        var desc3 = new ActionDescriptor();

        desc3.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), Math.round(theOpacity/2.55) );

    desc2.putObject( idT, idLyr, desc3 );

executeAction( idsetd, desc2, DialogModes.NO );

};

c.pfaffenbichler
c.pfaffenbichlerCorrect answer
Community Expert
January 2, 2016

// change layers’ opacity via scrollbars;

// 2015, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

app.activeDocument.suspendHistory("opacity", "theDialog ()");

};

function theDialog () {

//////////////////////////////////////////

var theLayers = theLayerNames ();

////// create dialog for user-entry //////

var dlg = new Window('dialog', "layeropacity", undefined);

//////////////////////////////

dlg.layer = dlg.add('panel', undefined, "opacity");

dlg.layer.orientation = "column";

dlg.layer.preferredSize = [300, 50];

// populate panel;

for (var m = 0; m < theLayers.length; m++) {

dlg.layer.add('statictext', undefined, theLayers[0]);

dlg.layer.add('scrollbar', undefined, theLayers[2], 0, 255);

dlg.layer.children[m*2+1].preferredSize = [255,20];

//////////////////////////////

// if scrolled;

dlg.layer.children[m*2+1].onChange = function () {

var theCh = dlg.layer.children.length;

for (var n = 0; n < theCh; n++) {

  if (dlg.layer.children == this) {var theNumber = n}

  };

setOpacityByID (theLayers[(theNumber-1)/2][1], Math.round(this.value));

app.refresh();

};

};

//////////////////////////////

// build- and cancel-button;

dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

dlg.center();

var myReturn = dlg.show ();

//////////////////////////////

// reset if canceled;

if (myReturn != 1) {

for (var a = 0; a < theLayers.length; a++) {

setOpacityByID (theLayers[1], theLayers[2])

}

};

$.gc();

};

////////////////////////////////////

function theLayerNames () {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var theOpacity = layerDesc.getInteger(stringIDToTypeID('opacity'));

theLayers.push([theName, theID, theOpacity])

};

}

catch (e) {};

};

return theLayers

};

////// set opacity //////

function setOpacityByID (theID, theOpacity) {

// =======================================================

var idsetd = charIDToTypeID( "setd" );

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

    var idLyr = charIDToTypeID( "Lyr " );

        var ref1 = new ActionReference();

        ref1.putIdentifier( idLyr, theID );

    desc2.putReference( idnull, ref1 );

    var idT = charIDToTypeID( "T   " );

        var desc3 = new ActionDescriptor();

        desc3.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), Math.round(theOpacity/2.55) );

    desc2.putObject( idT, idLyr, desc3 );

executeAction( idsetd, desc2, DialogModes.NO );

};

tssee
tsseeAuthor
Inspiring
January 2, 2016

Big

Thanks just what I needed

friendly

happy 2016

c.pfaffenbichler
Community Expert
January 3, 2016

It may be a dubious improvement, but you could replace the function theDialog with this, it saves a few lines:

function theDialog () {

//////////////////////////////////////////

var theLayers = theLayerNames ();

////// create dialog for user-entry //////

var dlg = new Window('dialog', "layeropacity", undefined);

//////////////////////////////

dlg.layer = dlg.add('panel', undefined, "opacity");

dlg.layer.orientation = "column";

dlg.layer.preferredSize = [300, 50];

// populate panel;

for (var m = 0; m < theLayers.length; m++) {

dlg.layer.add('statictext', undefined, theLayers[0]);

dlg.layer.add('scrollbar', undefined, theLayers[2], 0, 255);

dlg.layer.children[m*2+1].preferredSize = [256,20];

dlg.layer.children[m*2+1].helpTip = String(m);

//////////////////////////////

// if scrolled;

dlg.layer.children[m*2+1].onChange = function () {

setOpacityByID (theLayers[Number(this.helpTip)][1], Math.round(this.value));

app.refresh();

};

};

//////////////////////////////

// build- and cancel-button;

dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

dlg.center();

var myReturn = dlg.show ();

//////////////////////////////

// reset if canceled;

if (myReturn != 1) {

for (var a = 0; a < theLayers.length; a++) {

setOpacityByID (theLayers[1], theLayers[2])

}

};

$.gc();

};