Skip to main content
Participant
May 14, 2012
Answered

Creating a photoshop action to save a batch of images as patterns

  • May 14, 2012
  • 2 replies
  • 10646 views

I am trying to use Photoshop actions to script about a thousand image files into PAT files. I am running into a problem with actions when it comes to defining a pattern. The program naturally selects the filename as the name of the pattern when doing it manually, but when I do this while recording an action Photoshop logs the filename used in creating the action and then uses that filename for all sripted patterns. Is there a way to alter that setting to select the current file's name instead?

http://i.imgur.com/VVfo9.png

This topic has been closed for replies.
Correct answer Paul Riggott

This should be all you need...

createPattern();

function createPattern() {
    var desc6 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putClass( charIDToTypeID('Ptrn') );
    desc6.putReference( charIDToTypeID('null'), ref3 );
        var ref4 = new ActionReference();
        ref4.putProperty( charIDToTypeID('Prpr'), charIDToTypeID('fsel') );
        ref4.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc6.putReference( charIDToTypeID('Usng'), ref4 );
    desc6.putString( charIDToTypeID('Nm  '), app.activeDocument.name.replace(/\.[^\.]+$/, '') );
    executeAction( charIDToTypeID('Mk  '), desc6, DialogModes.NO );
};

2 replies

Participant
July 15, 2018

If you want also to export the last pattern defined (as I needed), then this is how it should be done:

var patternName = 'PatternName';

var patternPath = 'somePath/' + patternName + '.pat';

definePattern(patternName);

var lastPatternIndex = getPatternIndexByName(patternName);

exportPattern(lastPatternIndex, patternPath);

function definePattern(patternName) {

   var dialogMode = DialogModes.NO;

   var desc1 = new ActionDescriptor();

   var ref1 = new ActionReference();

   ref1.putClass(cTID('Ptrn'));

   desc1.putReference(cTID('null'), ref1);

   var ref2 = new ActionReference();

   ref2.putProperty(cTID('Prpr'), sTID("selection"));

   ref2.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));

   desc1.putReference(cTID('Usng'), ref2);

   desc1.putString(cTID('Nm '), patternName);

   executeAction(cTID('Mk '), desc1, dialogMode);

}

function exportPattern(patternIndex, patternPath) {

   var idsetd = charIDToTypeID( "setd" );

   var desc74 = new ActionDescriptor();

   var idnull = charIDToTypeID( "null" );

   desc74.putPath( idnull, new File( patternPath ) );

   var idT = charIDToTypeID( "T " );

   var list10 = new ActionList();

   var ref31 = new ActionReference();

   var idPtrn = charIDToTypeID( "Ptrn" );

   ref31.putIndex( idPtrn, patternIndex );

   list10.putReference( ref31 );

   desc74.putList( idT, list10 );

   executeAction( idsetd, desc74, DialogModes.NO );

}    

function getPatternIndexByName(match) {

    try {

        var r = new ActionReference();

        r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("presetManager"));

        r.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

        var list = executeActionGet(r).getList(stringIDToTypeID("presetManager"));

        for (var i = 0; i < list.count; i++) {

            if (list.getObjectType(i) == charIDToTypeID("PttR")) {

                var list2 = list.getObjectValue(i).getList(stringIDToTypeID("name"));

                for (var x = 0; x < list2.count; x++) {

                    var name = list2.getString(x);

                    if (name.indexOf(match) >= 0) return x + 1;

                }

                break;

            }

        }

        return -1;

    }

    catch (e) {

        alert(e);

        return -1;

    }

}

Kukurykus
Legend
July 15, 2018

You probably will find one more theard to post it 😉

JJMack
Community Expert
Community Expert
May 14, 2012

You might want to move this thread to the general photoshop forum if tou want an action.  However your not going to be able to do create the action you want for when you record menu Edit>Define Pattern the "Pattern Name Dialog" will pop up and whatever is used in it will be recorded into the action.

Also Photoshop Scripting DOM does not have a interface for creating and nameing patterns.  However you should bne ale to write a script to do what you want using code generated with Photoshop Scriptlistner Plug-in and modifying the hard coded name to a variable you set with somthing like the active document name.

Before defining the patern you may be able to see if the name is all ready use for a pattern name so you can add something to the name to avoid duplicateing name.

ScriptListner Code difine pattern

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc14 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref12 = new ActionReference();

        var idPtrn = charIDToTypeID( "Ptrn" );

        ref12.putClass( idPtrn );

    desc14.putReference( idnull, ref12 );

    var idUsng = charIDToTypeID( "Usng" );

        var ref13 = new ActionReference();

        var idPrpr = charIDToTypeID( "Prpr" );

        var idfsel = charIDToTypeID( "fsel" );

        ref13.putProperty( idPrpr, idfsel );

        var idDcmn = charIDToTypeID( "Dcmn" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref13.putEnumerated( idDcmn, idOrdn, idTrgt );

    desc14.putReference( idUsng, ref13 );

    var idNm = charIDToTypeID( "Nm  " );

    desc14.putString( idNm, "Pattern 5" );      <------ change "Pattern 5" to be a variable

executeAction( idMk, desc14, DialogModes.NO );

JJMack
munsonbhAuthor
Participant
May 14, 2012

Thanks JJ for the forewarning on the action capabilities.

I'm now officially in over my head if we are getting into this kind of scripting. Is there a good marketplace to hire script specialists?

Paul Riggott
Paul RiggottCorrect answer
Inspiring
May 14, 2012

This should be all you need...

createPattern();

function createPattern() {
    var desc6 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putClass( charIDToTypeID('Ptrn') );
    desc6.putReference( charIDToTypeID('null'), ref3 );
        var ref4 = new ActionReference();
        ref4.putProperty( charIDToTypeID('Prpr'), charIDToTypeID('fsel') );
        ref4.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc6.putReference( charIDToTypeID('Usng'), ref4 );
    desc6.putString( charIDToTypeID('Nm  '), app.activeDocument.name.replace(/\.[^\.]+$/, '') );
    executeAction( charIDToTypeID('Mk  '), desc6, DialogModes.NO );
};