Skip to main content
ahawktom
Participant
October 3, 2014
Answered

Scripting Layer Opacity change

  • October 3, 2014
  • 1 reply
  • 2747 views

Hello!

I want to create a script that decreases current layer opacity by 5. Here's what I have now:

app.activeDocument.activeLayer.opacity -= 5;

The problem is, when I map the script to a hotkey and press it multiple times (e.g. to set opacity to 50%), it goes normal from 100 to 95, from 95 to 90 and so on until 75%. From that point, it goes to 69% (should be 70%). Then it proceeds as it should (69 -> 64, 64 -> 59).

It also breaks at 24% (24 -> 18), if that matters.

I use Adobe Photoshop CS6 on Windows 8.1 (x64).

Why is this happening (decrease by 6 when I have 5 in my code)? Am I doing something wrong? Sorry if this question is stupid, I am just new to Photoshop scripting...

This topic has been closed for replies.
Correct answer pixxxelschubser

ahawktom,

opacity is not a rounded value and opacity works with percent.

Try something like this:

if (app.activeDocument.activeLayer.opacity > 5) {

    app.activeDocument.activeLayer.opacity = Math.round(app.activeDocument.activeLayer.opacity) -5;

    }

Have fun

1 reply

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
October 3, 2014

ahawktom,

opacity is not a rounded value and opacity works with percent.

Try something like this:

if (app.activeDocument.activeLayer.opacity > 5) {

    app.activeDocument.activeLayer.opacity = Math.round(app.activeDocument.activeLayer.opacity) -5;

    }

Have fun

ahawktom
ahawktomAuthor
Participant
October 4, 2014

Thanks a lot! That did it