Dynmically create pattern
I’ve finally worked out how to create a pattern dynamically so that it can be used in the same script.
I used to think that you had to supply the pattern ID or the name - but never got it to work always relying on the ID - which you can’t seem to get dynamically.
So for my future self:
// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF
var srcDoc = app.activeDocument;
// var pattern = "knitting 32 x 32";
// var pattern = "200 x 200";
// var pattern = "efa405da-f126-8b45-a46d-bb2228c1f124";
var pattern = "bananas"
//select all
activeDocument.selection.selectAll();
// save pattern
save_pattern(pattern);
// TEMP add new layer
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc6963 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1222 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref1222.putClass( idLyr );
desc6963.putReference( idnull, ref1222 );
var idUsng = charIDToTypeID( "Usng" );
var desc6964 = new ActionDescriptor();
var idNm = charIDToTypeID( "Nm " );
desc6964.putString( idNm, pattern);
var idLyr = charIDToTypeID( "Lyr " );
desc6963.putObject( idUsng, idLyr, desc6964 );
var idLyrI = charIDToTypeID( "LyrI" );
desc6963.putInteger( idLyrI, 34 );
executeAction( idMk, desc6963, DialogModes.NO );
// Finnaly fill with new pattern
fill_with_pattern(pattern);
// call the source document
var srcDoc = app.activeDocument;
function fill_with_pattern(patternnameorID, x, y)
{
// fill with pattern name or id
if (x == undefined) x = 0;
if (y == undefined) y = 0;
var regEx = (/([a-z0-9]{8}\-)([a-z0-9]{4}\-)([a-z0-9]{4}\-)([a-z0-9]{4}\-)([a-z0-9]{12})/m); //NOT global
var result = patternnameorID.match(regEx);
if (result != null)
{
// match! Therefore it's a ID
var ID = "" + patternnameorID;
// apply some name to the pattern
var patternName = ID; // same as ID
// alert(patternnameorID + "\nis a pattern ID");
}
else
{
// it's a pattern name
var patternName = patternnameorID;
var ID = undefined;
//alert(patternnameorID + "\nis a pattern name");
}
try
{
// =======================================================
var idFl = charIDToTypeID( "Fl " );
var desc2789 = new ActionDescriptor();
var idFrom = charIDToTypeID( "From" );
var desc2790 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc2790.putUnitDouble( idHrzn, idPxl, x ); // x
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc2790.putUnitDouble( idVrtc, idPxl, y ); // y
var idPnt = charIDToTypeID( "Pnt " );
desc2789.putObject( idFrom, idPnt, desc2790 );
var idTlrn = charIDToTypeID( "Tlrn" );
desc2789.putInteger( idTlrn, 0 );
var idUsng = charIDToTypeID( "Usng" );
var idFlCn = charIDToTypeID( "FlCn" );
var idPtrn = charIDToTypeID( "Ptrn" );
desc2789.putEnumerated( idUsng, idFlCn, idPtrn );
var idPtrn = charIDToTypeID( "Ptrn" );
var desc2791 = new ActionDescriptor();
var idNm = charIDToTypeID( "Nm " );
desc2791.putString( idNm, patternName );
if (ID != undefined)
{
var idIdnt = charIDToTypeID( "Idnt" );
desc2791.putString( idIdnt, ID ); // Pattern ID
}
// Otherwise skip ID part
var idPtrn = charIDToTypeID( "Ptrn" );
desc2789.putObject( idPtrn, idPtrn, desc2791 );
executeAction( idFl, desc2789, DialogModes.NO );
}
catch(eek)
{
alert("unable to find " + patternnameorID);
}
}
function save_pattern(patternstr)
{
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc47 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref10 = new ActionReference();
var idPtrn = charIDToTypeID( "Ptrn" );
ref10.putClass( idPtrn );
desc47.putReference( idnull, ref10 );
var idUsng = charIDToTypeID( "Usng" );
var ref11 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idfsel = charIDToTypeID( "fsel" );
ref11.putProperty( idPrpr, idfsel );
var idDcmn = charIDToTypeID( "Dcmn" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref11.putEnumerated( idDcmn, idOrdn, idTrgt );
desc47.putReference( idUsng, ref11 );
var idNm = charIDToTypeID( "Nm " );
desc47.putString( idNm, patternstr ); // pattern name here
executeAction( idMk, desc47, DialogModes.NO );
}
// function SELECT RECTANGLE(top, left, bottom, right)
// --------------------------------------------------------
function select_rectangle(top, left, bottom, right)
{
// Note: co-ordinates are same as script listener
// and not so human-friendly as t,l,r,b.
// deselect EVERYTHING first
app.activeDocument.selection.deselect();
// =======================================================
var id1 = charIDToTypeID( "setd" );
var desc1 = new ActionDescriptor();
var id2 = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var id3 = charIDToTypeID( "Chnl" );
var id4 = charIDToTypeID( "fsel" );
ref1.putProperty( id3, id4 );
desc1.putReference( id2, ref1 );
var id5 = charIDToTypeID( "T " );
var desc2 = new ActionDescriptor();
var id6 = charIDToTypeID( "Top " );
var id7 = charIDToTypeID( "#Pxl" );
desc2.putUnitDouble( id6, id7, top );
var id8 = charIDToTypeID( "Left" );
var id9 = charIDToTypeID( "#Pxl" );
desc2.putUnitDouble( id8, id9, left );
var id10 = charIDToTypeID( "Btom" );
var id11 = charIDToTypeID( "#Pxl" );
desc2.putUnitDouble( id10, id11, bottom );
var id12 = charIDToTypeID( "Rght" );
var id13 = charIDToTypeID( "#Pxl" );
desc2.putUnitDouble( id12, id13, right );
var id16 = charIDToTypeID( "Rctn" );
desc1.putObject( id5, id16, desc2 );
executeAction( id1, desc1, DialogModes.NO );
}
It won’t appear to do much. Seemingly duplicate a layer. However, it’s actually creating and then saving and then using a pattern.
I’m impressed even if no one else is. :D
