Copy link to clipboard
Copied
How can I read out the current brush size in javascript ?
Either for the paintbrush or e.g. the clonestamp tool.
I want to read the current value and change it to a different one within a script.
...function getCurrentBrushInfo() {
var brsh = {};
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID('Brsh'));
brsh.diameter = currentBrush.getDouble(charIDToTypeID('Dmtr'));
brsh.hardness = currentBrush.getDouble(charIDToTypeID('Hrdn'));
brsh.angle = currentBrush.getDoubl
Copy link to clipboard
Copied
function getCurrentBrushInfo() {
var brsh = {};
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID('Brsh'));
brsh.diameter = currentBrush.getDouble(charIDToTypeID('Dmtr'));
brsh.hardness = currentBrush.getDouble(charIDToTypeID('Hrdn'));
brsh.angle = currentBrush.getDouble(charIDToTypeID('Angl'));
brsh.roundness = currentBrush.getDouble(charIDToTypeID('Rndn'));
brsh.spacing = currentBrush.getDouble(charIDToTypeID('Spcn'));
return brsh
}
Copy link to clipboard
Copied
Unfortunately
and change it to a different one within a script
might be a bit of a problem.
Copy link to clipboard
Copied
ah, excellent. That looks pretty good. I will try it soon.
If I cannot change all the parameters that I need, I think of using a brush preset that does everything,
and then set back e.g. the brush size to the original value. Therefore I want to read out the current data first.
Setting the brush size should be ok, since it is also recorded by an action. Not so easy to find the individual parameters of the tools.
Thanks a lot for this great and quick help. Frank.
Copy link to clipboard
Copied
hey man old post but do you know anyway to change the flow and opacity of a brush using scripting?
Copy link to clipboard
Copied
What kind of instrument are you talking about? (the brush is used by several tools). This code, for example, changes the paintbrushTool options you specified. It will work for other tools too, just replace the class name
#target photoshop;
var s2t = stringIDToTypeID,
options = {
flow: 99,
opacity: 99,
};
(r = new ActionReference()).putClass(s2t("paintbrushTool"));
(d = new ActionDescriptor()).putReference(s2t("target"), r);
d.putObject(s2t("to"), s2t("target"), objToDesc(options));
executeAction(s2t("set"), d, DialogModes.NO);
function objToDesc(o) {
var d = new ActionDescriptor();
for (var k in o) {
var v = o[k];
switch (typeof (v)) {
case "boolean": d.putBoolean(s2t(k), v); break;
case "string": d.putString(s2t(k), v); break;
case "number": d.putInteger(s2t(k), v); break;
}
}
return d;
}
Copy link to clipboard
Copied
You can change brush size, but I dont know how to change other parameters.
function setCurrentBrushSize(_size) {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(charIDToTypeID('Brsh'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc1.putReference(charIDToTypeID('null'), ref1);
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(stringIDToTypeID("masterDiameter"), charIDToTypeID('#Pxl'), _size);
desc1.putObject(charIDToTypeID('T '), charIDToTypeID('Brsh'), desc2);
executeAction(charIDToTypeID('setd'), desc1, DialogModes.NO);
}
Copy link to clipboard
Copied
Good one!
I seemed to recall that sampled brushes had proved troublesome but this seems to work just fine for those, too.
Copy link to clipboard
Copied
Hey, c.pfaffenbichler‌
I think, your code is works too
setBrushFeatures(100, 50, undefined, undefined, undefined, undefined, );
function setBrushFeatures(Diameter, Hardness, Angle, Roundness, Spacing, Flipy, Flipx) {
// get app object
var appAR = new ActionReference();
appAR.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var app = executeActionGet(appAR);
// get brush settings
var brush = app.getObjectValue(stringIDToTypeID('currentToolOptions'))
.getObjectValue(stringIDToTypeID('brush'));
// set defaults if undefined (copies current brush values)
if( brush.hasKey(stringIDToTypeID('sampledData'))){
var SampleIntr = brush.getBoolean(charIDToTypeID('Intr'));
var SampleData = brush.getString(stringIDToTypeID('sampledData'));
var Name = brush.getString(stringIDToTypeID('name'))
Hardness = undefined;
}else if (Hardness == undefined) Hardness = brush.getDouble(stringIDToTypeID('hardness'));
if (Diameter == undefined) Diameter = brush.getDouble(stringIDToTypeID('diameter'));
if (Angle == undefined ) Angle = brush.getDouble(stringIDToTypeID('angle'));
if (Roundness == undefined) Roundness = brush.getDouble(stringIDToTypeID('roundness'));
if (Spacing == undefined) Spacing = brush.getDouble(stringIDToTypeID('spacing'));
if (Flipy == undefined) Flipy = brush.getBoolean(stringIDToTypeID('flipY'));
if (Flipx == undefined) Flipx = brush.getBoolean(stringIDToTypeID('flipX'));
// action with the new parameters
var paramsAD = new ActionDescriptor();
paramsAD.putDouble(stringIDToTypeID('diameter'), Diameter);
if(Hardness != undefined) paramsAD.putDouble(stringIDToTypeID('hardness'), Hardness);
paramsAD.putDouble(stringIDToTypeID('angle'), Angle);
paramsAD.putDouble(stringIDToTypeID('roundness'), Roundness);
paramsAD.putDouble(stringIDToTypeID('spacing'), Spacing);
paramsAD.putBoolean(stringIDToTypeID('flipY'), Flipy);
paramsAD.putBoolean(stringIDToTypeID('flipX'), Flipx);
if( brush.hasKey(stringIDToTypeID('sampledData'))){
paramsAD.putString(stringIDToTypeID('sampledData'), SampleData);
paramsAD.putString(stringIDToTypeID('name'), Name);
paramsAD.putBoolean(charIDToTypeID('Intr'), SampleIntr);
}
// action to modify brush
var brushAD = new ActionDescriptor();
var brushAR = new ActionReference();
brushAR.putEnumerated( charIDToTypeID( "Brsh" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
brushAD.putReference( charIDToTypeID( "null" ), brushAR );
// execute action to modify brush with the new parameters
brushAD.putObject( charIDToTypeID( "T " ), stringIDToTypeID( "sampledBrush" ), paramsAD );
executeAction( charIDToTypeID( "setd" ), brushAD, DialogModes.NO );
}
Copy link to clipboard
Copied
Sorry, I’m a bit distracted, but that code does not work with sampled brushes.
Copy link to clipboard
Copied
It would most likely also fail with wet brushes for they have different brush tip attributes.
Copy link to clipboard
Copied
Is there a way to increase/decrease brush size by 1 pixels from the current brush size using a script? I tried to use the scripts in How to read out current brush size in javascript, but I can't get it to work. Any help will be appreciated! I'm using Photoshop CC 2014, btw.
Copy link to clipboard
Copied
Please post the exact code you use.
Copy link to clipboard
Copied
function getCurrentBrushInfo() {
var brsh = {};
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID('Brsh'));
brsh.diameter = currentBrush.getDouble(charIDToTypeID('Dmtr'));
return brsh
}
function setCurrentBrushSize(_size) {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(charIDToTypeID('Brsh'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc1.putReference(charIDToTypeID('null'), ref1);
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(stringIDToTypeID("masterDiameter"), charIDToTypeID('#Pxl'), _size);
desc1.putObject(charIDToTypeID('T '), charIDToTypeID('Brsh'), desc2);
executeAction(charIDToTypeID('setd'), desc1, DialogModes.NO);
}
getCurrentBrushInfo();
var _size = brsh.diameter+1;
setCurrentBrushSize(_size);
I attempted to jumble together the script I saw in the thread I linked
Sorry if this looks stupid/makes no sense, I'm really new to scripting
Copy link to clipboard
Copied
You have omitted to define the function’s return as a variable.
#target photoshop
function getCurrentBrushInfo() {
var brsh = {};
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID('Brsh'));
brsh.diameter = currentBrush.getDouble(charIDToTypeID('Dmtr'));
brsh.hardness = currentBrush.getDouble(charIDToTypeID('Hrdn'));
brsh.angle = currentBrush.getDouble(charIDToTypeID('Angl'));
brsh.roundness = currentBrush.getDouble(charIDToTypeID('Rndn'));
brsh.spacing = currentBrush.getDouble(charIDToTypeID('Spcn'));
return brsh
}
function setCurrentBrushSize(_size) {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(charIDToTypeID('Brsh'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc1.putReference(charIDToTypeID('null'), ref1);
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(stringIDToTypeID("masterDiameter"), charIDToTypeID('#Pxl'), _size);
desc1.putObject(charIDToTypeID('T '), charIDToTypeID('Brsh'), desc2);
executeAction(charIDToTypeID('setd'), desc1, DialogModes.NO);
}
brsh = getCurrentBrushInfo();
var _size = brsh.diameter+1;
setCurrentBrushSize(_size);
Copy link to clipboard
Copied
Thank you so much!
Copy link to clipboard
Copied
You’re welcome, good luck with your Scripting efforts!
Copy link to clipboard
Copied
This should be faster the first time its run as its not getting all the keys.
function getCurrentBrushInfo() {
var brsh = {};
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"),stringIDToTypeID("tool"));
ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID("Brsh"));
brsh.diameter = currentBrush.getDouble(charIDToTypeID("Dmtr"));
brsh.hardness = currentBrush.getDouble(charIDToTypeID("Hrdn"));
brsh.angle = currentBrush.getDouble(charIDToTypeID("Angl"));
brsh.roundness = currentBrush.getDouble(charIDToTypeID("Rndn"));
brsh.spacing = currentBrush.getDouble(charIDToTypeID("Spcn"));
return brsh
};
Copy link to clipboard
Copied
It's true, I had trouble with the script as it have a lot of delay the first time it runs. The delay also came back when I alt-tabbed out of Photoshop for while, returned, and executed the script again.
This script is a lot faster with pretty much no delays! Thank you so much!
Copy link to clipboard
Copied
Hello sorry to bother you! could you help me getting this script working? I know very little about scripting but this particular script would be extremely helpful, I've tried copying this into notepad and saving it as .jsx and importing it into photoshop. If I've understood there is actually a problem with this code?
Copy link to clipboard
Copied
What kind of Brush are you tzrying to scale?
Do you get an error message?
Please post meaningful screenshots.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Sampled Brushes have no Hardness.
Copy link to clipboard
Copied
I've realised this, yes my brush is a sampled brush. I have removed this line from the code. now there is no error when i import the script but nothing seems to happen. The script doesn't seem to "save" into the list of scripts after it is loaded either, here is the code I am using :
#target photoshop
function getCurrentBrushInfo() {
var brsh = {};
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID('Brsh'));
brsh.diameter = currentBrush.getDouble(charIDToTypeID('Dmtr'));
brsh.angle = currentBrush.getDouble(charIDToTypeID('Angl'));
brsh.roundness = currentBrush.getDouble(charIDToTypeID('Rndn'));
brsh.spacing = currentBrush.getDouble(charIDToTypeID('Spcn'));
return brsh
}
function setCurrentBrushSize(_size) {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(charIDToTypeID('Brsh'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc1.putReference(charIDToTypeID('null'), ref1);
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(stringIDToTypeID("masterDiameter"), charIDToTypeID('#Pxl'), _size);
desc1.putObject(charIDToTypeID('T '), charIDToTypeID('Brsh'), desc2);
executeAction(charIDToTypeID('setd'), desc1, DialogModes.NO);
}
brsh = getCurrentBrushInfo();
var _size = brsh.diameter+1;
setCurrentBrushSize(_size);
Copy link to clipboard
Copied
»now there is no error when i import the script but nothing seems to happen«
What does this mean? Importing a Script?
Just put the jsx in the correct Folder, then you should be able to trigger it via a Keyboard Shortcut.
Please post MEANINGFUL screenshots (so including ALL the pertinent panels) from before and after running the Script.