Copy link to clipboard
Copied
I would like to write a script to cycle through a set of Tool Presets like this:
I run the script and it selects brush_2 then next time I run it, it selects brush_3 then next time I run it it selects brush_1 again and so on, looping like that forever, each time I run the script.
I imagine this could be done with a script if it could update a variable each time I run it (select tool preset brush_x, add 1 to x unless X>2 then x=1)
But the variable x would have to be in memory, outside of the script...is such a thing possible or do variables get reset each time you run a script?
Thanks!
Here is an example of cycling through three brushes using thier names.
N.B. make sure brushes are selected and the brushes are loaded.
...#target photoshop;
app.bringToFront();
main();
function main(){
//Name of Brushes to cycle through
var Brushes = ['Flat Angle Left Hand Pose','Chalk 36 pixels','Triangle Pastel'];
//Set up Photoshop to remember variables
var desc = new ActionDescriptor();
try{
var desc = app.getCustomOptions ( 'toggleBrush' );
}catch(e){
desc.putInteger ( 0,0 );
//this line save th
Copy link to clipboard
Copied
I have only seem brush tool options shown on the Photoshop tool option bar scripted thanks to Tom. You can change brushes in a script by selecting tool Presets. Like you want to do. So A script could have a list of presets it could cycle through. There are ways to have variables outside a script. I have use two different ways and I sure there are more. I think I read some use some kind of Photoshop.Option you can script. If you just need a global variable you can use a system environment variable. If you do a search on toggle brush it will hit some code that uses an environment variable. If you need variables associated to documents you working on you can use document metadata. I use documents instructions metadata fields for that.
They can be use as toggle and for storing data. Here is one I use Metadata. CC 2015.5 has a new bug that could may not set a metadata field back to empty. I do not use CC 2015.5 the bugs breaks several of my scripts.. It easy to program around you just need some in the metadata field like x marks the spot. Adobe is working on a fix.
/* ======================================================================================
// 2009 John J. McAssey (JJMack) http://www.mouseprints.net/
//
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
//
// This script is designed to be used by a Photoshop Action twice
// A good pratice to use when creating an actions that use this scipt is for the action
// not to do a save or play some other action between its two useages of this Script.
//
// The first time this script is used by an action the documents current resolution
// and ruler units are saved into the document's meta-data Info Instructions field.
//
// The second time this script used by the action the script retreives what was
// saved in the meta-data during the first usage, resolution and ruler units.
// The document is resized to the saved resolution,
// Photoshop's ruler units are set to saved units
// and the saved data is removed from the document's meta-data Info Instructions field.
//
// ===================================================================================== */
/*
<javascriptresource>
<about>$$$/JavaScripts/SaveAndRestoreResolution/About=JJMack's SaveAndRestoreResolution.^r^rCopyright 2009 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^r^rFirst Run records Photoshop's preferences Units and documents DPI resolution.^rSecond Run restores the recorded setting and removes the recording.</about>
<category>JJMack's Action Run Twice Utility</category>
</javascriptresource>
*/
if (app.documents.length > 0) {
if (app.activeDocument.info.instructions.indexOf("<Resolution>") == -1 ){ // no footprint fisrt useage
//alert("first");
// Retreive Document information for Foot Print
var units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS; // set ruler units to PIXELS
var typeunits = app.preferences.typeUnits;
var res = app.activeDocument.resolution;
// put footprint in metadata info instructions
app.activeDocument.info.instructions = app.activeDocument.info.instructions + "<Units>" + units + "</Units>" + "<Tunits>" + typeunits + "</Tunits>" + "<Resolution>" + res + "</Resolution>";
//alert( "Saved =" + "<Units>" + units + "</Units>" + "<Tunits>" + typeunits + "</Tunits>" + "<Resolution>" + res + "</Resolution>" );
app.preferences.rulerUnits = units; // restore ruler units
}
else {
//alert("second");
// Retreive saved information
unitsOffset = app.activeDocument.info.instructions.indexOf("<Units>") + "<Units>".length;
unitsLength = app.activeDocument.info.instructions.indexOf("</Units>") -unitsOffset;
savedUnits = app.activeDocument.info.instructions.substr(unitsOffset, unitsLength);
tunitsOffset = app.activeDocument.info.instructions.indexOf("<Tunits>") + "<Tunits>".length;
tunitsLength = app.activeDocument.info.instructions.indexOf("</Tunits>") -tunitsOffset;
savedTunits = app.activeDocument.info.instructions.substr(tunitsOffset, tunitsLength);
resOffset = app.activeDocument.info.instructions.indexOf("<Resolution>") + "<Resolution>".length;
resLength = app.activeDocument.info.instructions.indexOf("</Resolution>") + -resOffset;
savedResolution = app.activeDocument.info.instructions.substr(resOffset, resLength);
//alert("Resolution = " + savedResolution + " Units = " + savedUnits );
// Restore resolution
app.preferences.rulerUnits = Units.PIXELS;
activeDocument.resizeImage(null, null, savedResolution, ResampleMethod.NONE);
// Restore ruler units
// I get a message Enumerated value expected if I try to use var savedUnits app.preferences.rulerUnits = savedUnits;
// perhaps if I knew Javascript I would not need to use the following if else if .....
if ( savedUnits == "Units.INCHES" ){ app.preferences.rulerUnits = Units.INCHES;}
else if ( savedUnits == "Units.CM" ){ app.preferences.rulerUnits = Units.CM;}
else if ( savedUnits == "Units.PERCENT" ){ app.preferences.rulerUnits = Units.PERCENT;}
else if ( savedUnits == "Units.MM" ){ app.preferences.rulerUnits = Units.MM;}
else if ( savedUnits == "Units.PIXELS" ){ app.preferences.rulerUnits = Units.PIXELS;}
else if ( savedUnits == "Units.POINTS" ){ app.preferences.rulerUnits = Units.POINTS;}
else if ( savedUnits == "Units.PICAS" ){ app.preferences.rulerUnits = Units.PICAS;}
// Restore Type units
if ( savedTunits == "TypeUnits.PIXELS" ){ app.preferences.typeUnits = TypeUnits.PIXELS;}
else if ( savedTunits == "TypeUnits.POINTS" ){ app.preferences.typeUnits = TypeUnits.POINTS;}
else if ( savedTunits == "TypeUnits.MM" ){ app.preferences.typeUnits = TypeUnits.MM;}
// Remove footprint from metadata info instructions
before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexOf("<Units>"));
afterOffset = app.activeDocument.info.instructions.indexOf("</Resolution>") + "</Resolution>".length;
after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);
//alert ("before = " + before + " after = " + after);
app.activeDocument.info.instructions = before + after;
}
}
else { alert("You must have at least one open document to run this script!"); }
Copy link to clipboard
Copied
Here is an example of cycling through three brushes using thier names.
N.B. make sure brushes are selected and the brushes are loaded.
#target photoshop;
app.bringToFront();
main();
function main(){
//Name of Brushes to cycle through
var Brushes = ['Flat Angle Left Hand Pose','Chalk 36 pixels','Triangle Pastel'];
//Set up Photoshop to remember variables
var desc = new ActionDescriptor();
try{
var desc = app.getCustomOptions ( 'toggleBrush' );
}catch(e){
desc.putInteger ( 0,0 );
//this line save the variable while Photoshop is open,
//change false to true and it will remember next time Photoshop is opened
app.putCustomOptions( 'toggleBrush', desc, false );
desc = app.getCustomOptions ( 'toggleBrush' );
}
var brushNumber = desc.getInteger(0);
if(brushNumber < Brushes.length){
desc.putInteger ( 0, brushNumber + 1);
}else{
brushNumber = 0;
desc.putInteger ( 0,1);
}
app.putCustomOptions( 'toggleBrush', desc, false );
selectBrushByName(Brushes[brushNumber]);
};
function selectBrushByName( brushName ){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putName( charIDToTypeID('Brsh'), brushName );
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
};
Copy link to clipboard
Copied
Thank you both for these thoughtful answers. I wasn't sure it was worth pursuing as I wasn't sure it was possible to even remember a variable after running once but from your replies it seems that's not an issue at all and it's perhaps even possible to even save it between sessions.
I am trying to figure out a way to make some brushes that look good when animating natural media, and a key part of that will be switching brushes in between drawing each frame, I wanted to make sure I was not going down a dead end road.
Thanks so much!
Copy link to clipboard
Copied
Did you ever write that script? If so, is it available anywhere?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more