Skip to main content
Known Participant
January 4, 2024
Answered

Select Last pattern

  • January 4, 2024
  • 5 replies
  • 1320 views

I work in patterns. I have to make 1000 patterns a day. So I am troubled by selecting every time. Can the last pattern be selected?

Correct answer c.pfaffenbichler

This should create a Pattern Layer with the last Pattern. 

// create pattern layer with last pattern;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
try {
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID ("property"), stringIDToTypeID("presetManager") ); 
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var presetManager = applicationDesc.getList(stringIDToTypeID("presetManager"));
var patternNames = presetManager.getObjectValue(4).getList(stringIDToTypeID("name"));
var theNames = new Array;
for (m = 0; m < patternNames.count; m++) {
    theNames.push(patternNames.getString(m))
};
makePatternLayer (theNames[theNames.length-1], 100);
//brickFill (theNames[theNames.length-1], 0.1)
} catch (e) {};
};
////// make pattern layer //////
function makePatternLayer (thePatternName, theScale) {
// =======================================================
var idMk = charIDToTypeID( "Mk  " );
	var desc3 = new ActionDescriptor();
	var idnull = charIDToTypeID( "null" );
		var ref1 = new ActionReference();
		var idcontentLayer = stringIDToTypeID( "contentLayer" );
		ref1.putClass( idcontentLayer );
	desc3.putReference( idnull, ref1 );
	var idUsng = charIDToTypeID( "Usng" );
		var desc4 = new ActionDescriptor();
		var idType = charIDToTypeID( "Type" );
			var desc5 = new ActionDescriptor();
			var idScl = charIDToTypeID( "Scl " );
			var idPrc = charIDToTypeID( "#Prc" );
			desc5.putUnitDouble( idScl, idPrc, theScale );
			var idPtrn = charIDToTypeID( "Ptrn" );
				var desc6 = new ActionDescriptor();
				var idNm = charIDToTypeID( "Nm  " );
				desc6.putString( idNm, "rust400x400" );
				var idIdnt = charIDToTypeID( "Nm  " );
				desc6.putString( idIdnt, thePatternName );
			var idPtrn = charIDToTypeID( "Ptrn" );
			desc5.putObject( idPtrn, idPtrn, desc6 );
		var idpatternLayer = stringIDToTypeID( "patternLayer" );
		desc4.putObject( idType, idpatternLayer, desc5 );
	var idcontentLayer = stringIDToTypeID( "contentLayer" );
	desc3.putObject( idUsng, idcontentLayer, desc4 );
executeAction( idMk, desc3, DialogModes.NO );
return app.activeDocument.activeLayer
};

5 replies

Myra Ferguson
Community Expert
Community Expert
January 4, 2024

If you open the Patterns panel (Window > Patterns), then your most recently used pattern should appear as a small swatch at the to as well as a larger swatch below the other folders of swatches. You can click on a swatch and drag it to a selection on your canvas to apply it. Similarly, you can select a Pattern layer in the Layers panel and click on a different pattern swatch to have that pattern layer use that selected swatch instead.

MJ STAuthor
Known Participant
January 9, 2024

Very nice. This is a correct method but when I use it in action, the same pattern is repeating.

c.pfaffenbichler
Community Expert
Community Expert
January 9, 2024

How you want to use that Pattern? 

What role is it supposed to play in the Action? 

Stephen Marsh
Community Expert
Community Expert
January 4, 2024

@MJ ST 

 

You were not explicitly tagged in the last two replies, so may not be receiving notifications... Please let us know how the script from @c.pfaffenbichler works for you.

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
January 4, 2024

This should create a Pattern Layer with the last Pattern. 

// create pattern layer with last pattern;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
try {
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID ("property"), stringIDToTypeID("presetManager") ); 
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var presetManager = applicationDesc.getList(stringIDToTypeID("presetManager"));
var patternNames = presetManager.getObjectValue(4).getList(stringIDToTypeID("name"));
var theNames = new Array;
for (m = 0; m < patternNames.count; m++) {
    theNames.push(patternNames.getString(m))
};
makePatternLayer (theNames[theNames.length-1], 100);
//brickFill (theNames[theNames.length-1], 0.1)
} catch (e) {};
};
////// make pattern layer //////
function makePatternLayer (thePatternName, theScale) {
// =======================================================
var idMk = charIDToTypeID( "Mk  " );
	var desc3 = new ActionDescriptor();
	var idnull = charIDToTypeID( "null" );
		var ref1 = new ActionReference();
		var idcontentLayer = stringIDToTypeID( "contentLayer" );
		ref1.putClass( idcontentLayer );
	desc3.putReference( idnull, ref1 );
	var idUsng = charIDToTypeID( "Usng" );
		var desc4 = new ActionDescriptor();
		var idType = charIDToTypeID( "Type" );
			var desc5 = new ActionDescriptor();
			var idScl = charIDToTypeID( "Scl " );
			var idPrc = charIDToTypeID( "#Prc" );
			desc5.putUnitDouble( idScl, idPrc, theScale );
			var idPtrn = charIDToTypeID( "Ptrn" );
				var desc6 = new ActionDescriptor();
				var idNm = charIDToTypeID( "Nm  " );
				desc6.putString( idNm, "rust400x400" );
				var idIdnt = charIDToTypeID( "Nm  " );
				desc6.putString( idIdnt, thePatternName );
			var idPtrn = charIDToTypeID( "Ptrn" );
			desc5.putObject( idPtrn, idPtrn, desc6 );
		var idpatternLayer = stringIDToTypeID( "patternLayer" );
		desc4.putObject( idType, idpatternLayer, desc5 );
	var idcontentLayer = stringIDToTypeID( "contentLayer" );
	desc3.putObject( idUsng, idcontentLayer, desc4 );
executeAction( idMk, desc3, DialogModes.NO );
return app.activeDocument.activeLayer
};
MJ STAuthor
Known Participant
January 9, 2024

Ohh my God....it is working..THank U.. THank u SO Much..U are Great Sir..Thank U.

c.pfaffenbichler
Community Expert
Community Expert
January 9, 2024

You’re welcome! 

c.pfaffenbichler
Community Expert
Community Expert
January 4, 2024

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Patterns, Options Bar, …) visible to illustrate what the point is, how you want to use that Pattern? 

MJ STAuthor
Known Participant
January 9, 2024

MJ STAuthor
Known Participant
January 9, 2024

Please help me sir. I have been trying to solve the problem for a year.

Stephen Marsh
Community Expert
Community Expert
January 4, 2024

Selected where and how? What is the context?

 

Please describe in steps and add screenshots to help illustrate.

 

MJ STAuthor
Known Participant
January 9, 2024