Skip to main content
Inspiring
March 4, 2010
Question

brush opacity for scripting

  • March 4, 2010
  • 2 replies
  • 9056 views

Hi everyone!

I want to change opacity of brush relatively, similar to what Photoshop have for brush size, so I could just increase/ decrease opacity.

Script listener doesn't 'hear' changes in brush opacity so I wonder if there is a way somehow to accomplish this.

Thanks!

This topic has been closed for replies.

2 replies

Reynolds__Mark_
Inspiring
March 11, 2010

Brush Opacity can be very easily controlled with the scrubby sliders (permanently visible in the Options Bar whilst using the Brush tool). Alt dragging these and Shift dragging them decreases and increases their rate. No Script based approach could be more efficient than this.

Inspiring
March 11, 2010

Reynolds (Mark) wrote:

No Script based approach could be more efficient than this.

That is true IF setting the opacity is the only thing the script does. But as part of a more complex script it would be useful to be able to control tool options like opacity.

The main purpose of a script is to automate some workflow in Photoshop. Not having full access to the save features as the GUI limits what can be done.

It's clear from your post that you don't find scripting useful. Why are you in the scripting forum if you do not think script are worthwhile?

Reynolds__Mark_
Inspiring
March 11, 2010
That is true IF setting the opacity is the only thing the script does.

Thats what's been asked, as far as I'm aware.

It's clear from your post that you don't find scripting useful. Why are
you in the scripting forum if you do not think script are worthwhile?

Nonsense, at no point have I ever said that Scripting is not useful.  Just 95% of the time - there are much better, less involved solutions.

Inspiring
March 4, 2010

The only way I know to control brush settings like opacity with scripting is by using tool presets.

You can control some settings of a computed brush with a script such as diameter, hardness, angle, roundness, and spacing. But even those settings can not be set if the brush is a sampled brush.

Inspiring
May 31, 2010

Hello !

Michael L Hale wrote:

The only way I know to control brush settings like opacity with scripting is by using tool presets.

.


For his patient and (almost) always helpful answers, i would like to thank Michael with this little contribution: How to set the current opacity of brushes.

The strategy is to simulate the hit of numeric keys, because it modifies the current opacity (tool or layer otherwise).

This is the JS script:

/************************************************
   OPACITY of a brush
*************************************************
30/05/2010 V1r01 HABAKI: Creation
*************************************************/
/*----------------------------------------------*
                       Select tool 
*-----------------------------------------------*/
function ToolSelect(Name)
{
  var AD = new ActionDescriptor();
  var AR = new ActionReference();
 
  AR.putClass(stringIDToTypeID(Name));
  AD.putReference(charIDToTypeID("null"), AR);
  executeAction(charIDToTypeID("slct"), AD, DialogModes.NO);
}
/*----------------------------------------------*
                        BrushOpacitySet
Selects the paintbrushTool
*-----------------------------------------------*/
function BrushOpacitySet(/* Integer */ Percent)
{
  var NumSt = String(Math.floor(Percent));
  NumSt = "000".slice(0, -NumSt.length) + NumSt;
 
  var farg = new File("/c/temp/keyhiti.txt");
  farg.open("w:");
  farg.writeln(NumSt);
  farg.close();
 
  ToolSelect("paintbrushTool");
 
  (new File("/c/temp/keyhit.exe")).execute();
}
/*-----------------------------------------------*
                        BrushOpacityGet
*------------------------------------------------*/
function BrushOpacityGet()
{
  var ref = new ActionReference();
  ref.putEnumerated(charIDToTypeID("capp"),
                    charIDToTypeID("Ordn"),
                    charIDToTypeID("Trgt")
                    );
  var AD = executeActionGet(ref);
  AD = AD.getObjectValue(stringIDToTypeID("currentToolOptions"));
  return(AD.getInteger(stringIDToTypeID("opacity")));
}
/*-----------------------------------------------*
   Main
*------------------------------------------------*/
try{
 
  var Op = BrushOpacityGet();
  //alert("Opacity=" + Op + ", =>" + Op/2);
 
  BrushOpacitySet(Op / 2);

} catch(ex) {
  alert(ex.message);
}


This script calls an .EXE file : KEYHIT.EXE.

Here is the C source for windows:

/************************************************
                        KEY HIT
Simulates keys into the focused window.
*************************************************
30/05/2010 V1R01 HABAKI : Creation
*************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

#define ArgFilePath "c:/temp/keyhiti.txt"
/*----------------------------------------------*
                MAIN
Input in file keyhiti.txt
*-----------------------------------------------*/
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    char * Params,
    int nCmdShow
    )
{
  HWND hWnd;
  FILE * farg;
  unsigned c;
  char Str[256], * p;
 
  hWnd = GetForegroundWindow();
 
  do {
    farg = fopen(ArgFilePath, "r");
    if (farg == 0) break;
    if (fgets(Str, 16, farg) == 0) break;
    Str[16] = '\0';
    p = Str;
    for (;c = (unsigned)*p++;) {
      PostMessage(hWnd, WM_KEYDOWN, (WPARAM)c, (LPARAM)((c << 16 ) + 1));
    } // for
    fclose(farg);
    DeleteFile(ArgFilePath);
  } while(0);
 
  TerminateProcess(GetCurrentProcess(),0);
  return(0);
}
/*-----------------------------------------------*/


I hope it can also help Sergey if  he always needs this feature.

Inspiring
May 31, 2010

Oh! Habaki, thank you so much for answering!

Still I'm not sure that this will work for me because I'm on Mac so I probably cannot compile the C code you provided (even if I do the fact of using C scares me a bit).

I found out the other way, but it hasn't worked out too: simulate pressing 'enter' + 'shift+down/up' + 'enter' with Apple Script (which selects opacity, decreases or increases it by 10% and than deselects it), but I haven't figured out how to execute AppleScript with JS (the doScript function available for inDesign lacks in Photoshop) so I kept using Intuos4 Radial Menu for Opacity.

Kind regards, Sergey.