Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Oh thanks Michael, that's sad to know
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hello Sergey !
The C program only reads a string from a file and simulates a key press event with each char.
File functions are standard in C and only windows functions like getWindow... and PostMessage must be replaced.
I can't help nor test on Mac but there are solutions on the web like perhaps this one :
http://stackoverflow.com/questions/2379867/simulating-key-press-events-in-mac-osx
Copy link to clipboard
Copied
AppleScript & System Events can simulate the keystrokes easy enough even with modifier keys. Its just not as simple from the Photoshop point of view as it lacks InDesign's doScript() method. You can either call out to a shell executable which I can't do (because of my app version) or BridgeTalk message the Bridge app to do the system command. Paul did tell me that Photoshop now has the system command in a previous topic but I have not been able to find this in the PDF documents (that would be better).
Copy link to clipboard
Copied
Photoshop does not have a system() call but it does have a File.execute() call. The only problem on the Mac is that the shell script has to have its execute bit set and you can't set that from within PS JS. The simplest work-around is to create the script ahead of time. If you need something more dynamic,
But if you really want a system call, here's some stuff from xtools:
This shell script creates a macapp thingy. I adapted this from someone else's idea. You run it once and it sets up the macexec.app in the Presets/Scripts folder. Make sure the PS path is correct.
#!/bin/bash
#
# createMacExec.sh
#
# $Id$
#
# This is the Mac exec installation script.
# This creates the necessary files and folders needed to execute shell scripts
# under OSX.
#
# Installation instructions:
#
# To install the OSX shell script support, open a terminal window and change to
# the parent directory, possibly Presets/Scripts
#
# Run this script:
# % ./createMacExec.sh
#
# When this script runs, it creates a folder called 'macexec.app' and some
# folders and files underneath which allows Exec from xexec.js to call
# shell scripts.
#
# Example:
# alert(Exec.system("ls -l ~/"));
#
# Specify the desired parent directory here
# Exec defaults to using this path
IDIR="/Applications/Adobe Photoshop CS4/Presets/Scripts"
if [ ! -e "$IDIR" ]; then
echo Bad directory specified ${IDIR}
exit 1
fi
cd "$IDIR"
mkdir -p macexec.app
mkdir -p macexec.app/Contents
rm -f macexec.app/Contents/macexec
touch macexec.app/Contents/macexec
chmod 755 macexec.app/Contents/macexec
# this is a hook for later so that shell variables (e.g. PATH)
# can be autoloaded from macexec
cat > macexec.app/Contents/macexec.env << EOF
#!/bin/bash
EOF
echo macexec successfully installed
# EOT
xtools/xlib/xexec.jsx contains the Exec.system call which handles the details writing the command to the right file, executing the sciprt, and collecting and returning the results.
Copy link to clipboard
Copied
Both Photoshop CS4 and CS5 have an app.system(); call IE:
app.system("dir|more");
Copy link to clipboard
Copied
Hi Paul,
Where did you find about that and do you have more info. It's not listed as a Photoshop app method in either the CS4 or CS5 javascript pdf nor in either version Object Model Viewer.
Copy link to clipboard
Copied
app.system (probably) exists in other CS apps. It just not documented.
From what I can tell, is a synchronous call to the shell and it returns the error code from the shell. If you
need the output from the command, you need to do something like:
app.system("exiftool -j ~/Desktop/*.jpg > ~/Desktop/metadata-json.txt");
Now if I can just get all of my clients to upgrade...
Copy link to clipboard
Copied
Hello !
Thank you for this discussion about .system().
As a matter of fact on windows, calling CMD.EXE to execute a command that will call another .EXE (because CMD can do so little by itself) is not very effective, and so .execute() is better.
But perhaps it is different with the Mac shell.
To come back to the subject, can you propose a solution on Mac for Sergey to control the opacity by the simulation of key press events, as i did it on windows ?
If you have a better solution to control opacity, i am also client.
Copy link to clipboard
Copied
It was first mentioned in the early days of CS4 on this forum, I think it was Bob or Chris from Adobe that mentioned it.
Copy link to clipboard
Copied
Paul, I searched high n low to find this after you last mentioned it. Trawled PDF's the only thing I found was 'systemInfromation' added.
Why would it NOT be documented? Anyone tested what other suite apps it works with? It would be nice to know… Thought Paul might have had a magic pixie install with merlin and mushrooms…
Copy link to clipboard
Copied
Of the CS4 apps I have it looks like Bridge is the only other to have app.system. InDesign and Illsutrator don't. They may have some other method that does the same thing I didn't sreach. I just ran this
$.writeln(app.reflect.methods);
In ESKT targeting the apps in turn looking for system in the results.
Copy link to clipboard
Copied
The fact that ps has this is good enough news… I do have the big fat rascal CS5 trial downloaded and installed @~ just not started the 30 days going just yet…
Copy link to clipboard
Copied
Pas de chance !
It must be a forever loop on .system() and i don't known how to break it ! Why me ?
Alt-Ctrl-Del is not the solution. We are on Mac !
I hope the battery will be low soon !
Copy link to clipboard
Copied
Kill Photoshop. That's the only way to be sure.
or...
The app.system() call blocks on the process that gets launched. If you can find that process in Activity Monitor (or top(1) or ps(1)) you can kill that.
If you are wanting to launch an app but not block, do something like
app.system("xemacs &");
The '&' will cause 'xemacs' to be launched as a background process and lets the app.system() call return immediately.
Copy link to clipboard
Copied
Hello Xbytor2 !
I stored this info from you into my doc base, and i thank you for it. You often give very good advices and codes.
But my message was mainly metadata about previous messages.
If you know a way to go back to the original subject of this discussion, you are thanked twice.
Copy link to clipboard
Copied
Hi,
Thank you for this
found this really helpful. Do you know how to run the code silently? I mean without the console popping out
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Mark, what I'm trying to accomplish is using only two keys on keyboard for opacity instead of ten, in the same way how brush size. I work mostly with interface hidden and rapidly switching between different brushes/opacity/sizes so any slider on interface wouldn't fasten my work.
I just prefer to paint with one hand and switch what I need with another and changing opacity in this way is kinda uncomfortable. That's why I wanted to change opacity relatively
Copy link to clipboard
Copied
Mark, what I'm trying to accomplish is using only two keys on keyboard for opacity
OK, you are talking about the new Control Alt drag shortcut for resize. In that case yes, I agree - being able to modify this shortcut for different brush parameters would be useful fror some. Feature Requests may be your answer here.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more