• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Script to input and select and move layers based on the input

Explorer ,
Dec 31, 2023 Dec 31, 2023

Copy link to clipboard

Copied

I'm not entirely sure of the scope of what's possible but I'm trying to do the following:

Pretext: I have 52 layers named a1 to z1 and also a2 to z2. Each layer has a letter corrseponding to its name. A2 is just a different letter A. The intention is to input a name and generate that name based on the text. I'm planning to print out peoples names that they input. But I don't want to manually sort out each name and each layer on every single order, and since print on demand sites wouldn't allow me to upload 52 pictures, sort and print based on someone's input I would have to at least somewhat do this normally. The name would display based one 1 then 2 then 1 then 2.

for example inputting ADAM, would select layer A1 then move, D2 then move, A2 then move, M1 then move. This is how I envision it, let me know if I'm on the right track please:

  1. Upon clicking action input a name.
  2. Append a 1 corresponding to the first letter of that input and select that layer,
  3. Move layer.
  4. Check for input length.
  5. if more than 1, select the next letter in the input and append a 2,
  6. Move layer,
  7. Continue to input length finishes.

 

to simplify id set up each position from one to ten and just select that as a position based on the input length in the sequencer.

 

Can this be done in photoshop?

TOPICS
Actions and scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jan 01, 2024 Jan 01, 2024

It will take time and effort to create automation for such a task. The investment in time and effort has to be balanced against the time and effort of doing this manually for the volume of orders received.

 

A different approach would be to create a separate square canvas file for each letter in the alphabet (all using the same canvas size and common baseline, same pixel size and resolution etc). 

 

A script could open each required letter and stack the files horizontally to create a word.

 

Thi

...

Votes

Translate

Translate
Community Expert , Jan 03, 2024 Jan 03, 2024

I have no access to the files on the onedrive server so I cannot test with them. 

With a simple test file (with layers only for »a« through »f«) I was able to use the Script below to get an Array of Arrays (containing the Layer’s name, the index [which can be used to address/select the layer], the bounds [which can be used to calculate the necessary offsets]) in which the layers starting with the intended letter are being »rotated« (see the multiple »e«s for example). 

Screenshot 2024-01-03 at 15.30.06.pngScreenshot 2024-01-03 at 15.31.49.png

 

// 2023, use it at your
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 04, 2024 Jan 04, 2024

Copy link to clipboard

Copied

In my test that code performed fine, could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 04, 2024 Jan 04, 2024

Copy link to clipboard

Copied

It's no problem but for your knowledge seek I suppose. I run the code I specified earlier. Without trim. It changes the canvas size to the letters that have been chosen. Then I run the trim script, looks like this:

Trim problem.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2024 Jan 04, 2024

Copy link to clipboard

Copied

I would recommend doing away with the Background Layer and Delete Hidden Layers. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2024 Jan 05, 2024

Copy link to clipboard

Copied

LATEST

If you want to invest more time please try the following code. 

With a simple test-file it seems to perform fairly speedy. 

edit: I had forgotten to remove the revert I used while testing and I updated the code accordingly. 

arrangeLettersLayers_01_scr.gif

Screenshot 2024-01-05 at 15.14.43.pngScreenshot 2024-01-05 at 15.14.51.png

 

 

 

 

// collect layers named »letterNumber« and get an array according to a string, then duplicate and move the layers accordingly;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
// revert;
//executeAction( stringIDToTypeID( "revert" ), undefined, DialogModes.NO );
var myDocument = activeDocument;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
////////////////////////////////////
var theString = prompt("Enter a name:", "badebefefefe");
if (theString) {
var theLayers = collectLayersBounds ();
theLayers.sort();
var theRegExp = /^\D\d$/i;
// create arrays sorted by initial letter;
var theArrays = new Array;
for (var m = 0; m < theLayers.length; m++) {
    var thisOne = theLayers[m];
    var theCheck = false;
    if (thisOne[0].match(theRegExp) != null) {
        var theLetter = String(thisOne[0].match(/^\D/i));
        for (var n = 0; n < theArrays.length; n++) {
            if (theArrays[n][0][0][0] == theLetter) {
                theArrays[n].push(thisOne);
                var theCheck = true
            }
        };
        if (theCheck != true) {theArrays.push([thisOne])};
    }
};
// check string against array;
var theResult = new Array;
for (var o = 0; o < theString.length; o++) {
    var thisOne = theString[o];
    for (var p = 0; p < theArrays.length; p++) {
        if (theArrays[p][0][0][0].match(new RegExp (thisOne, "i")) != null) {
            theResult.push (theArrays[p][0])
            var theStuff = theArrays[p].splice(0,1);
            theArrays[p].push(theStuff[0]);
        }
    };
};
if (theResult.length > 0) {
app.togglePalettes;
////////////////////////////////////
var theX = 0;
var theY = 0;
for (var q = 0; q < theResult.length; q++) {
    var thisOne = theResult[q];
    duplicateMoveRotateScale(true, thisOne[1], theX-thisOne[2][0], thisOne[2][1]*(-1), 100, 100, 0);
    theX = theX+(thisOne[2][2]-thisOne[2][0]);
    theY = Math.max(theY, thisOne[2][3]-thisOne[2][1]);
    var thisLayerId = getLayerId();
    moveLayerTo (thisLayerId, getTopLayerIndex ()+1);
};
if (theX != 0 && theY != 0) {myDocument.crop([0,0,theX,theY])};
};
app.togglePalettes;
};
////////////////////////////////////
app.preferences.rulerUnits = originalRulerUnits;
};
////// collect layers //////
function collectLayersBounds () {
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
theLayers.push([theName, theID, theseBounds])
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
try {
add = undefined ? add = false:add 
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
} catch (e) {}
};
////// duplicate layer and move, rotate and scale it //////
function duplicateMoveRotateScale (copyOrNot, theID, theX, theY, theScaleX, theScaleY, theRotation) {
selectLayerByID(theID,false);
/*smartify (activeDocument.activeLayer);
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), charIDToTypeID ("LyrI"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); 
d = executeActionGet(ref); 
var theID = d.getInteger(charIDToTypeID('LyrI'));*/
try{
var idTrnf = charIDToTypeID( "Trnf" );
    var desc10 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        //ref6.putIdentifier( charIDToTypeID( "Lyr " ), theID );
        ref6.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc10.putReference( idnull, ref6 );
    var idFTcs = charIDToTypeID( "FTcs" );
    var idQCSt = charIDToTypeID( "QCSt" );
    var idQcsa = charIDToTypeID( "Qcsa" );
    desc10.putEnumerated( idFTcs, idQCSt, idQcsa );
    var idOfst = charIDToTypeID( "Ofst" );
        var desc11 = new ActionDescriptor();
        var idHrzn = charIDToTypeID( "Hrzn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc11.putUnitDouble( idHrzn, idPxl, theX );
        var idVrtc = charIDToTypeID( "Vrtc" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc11.putUnitDouble( idVrtc, idPxl, theY );
    var idOfst = charIDToTypeID( "Ofst" );
    desc10.putObject( idOfst, idOfst, desc11 );
    var idWdth = charIDToTypeID( "Wdth" );
    var idPrc = charIDToTypeID( "#Prc" );
    desc10.putUnitDouble( idWdth, idPrc, theScaleX );
    var idHght = charIDToTypeID( "Hght" );
    var idPrc = charIDToTypeID( "#Prc" );
    desc10.putUnitDouble( idHght, idPrc, theScaleY );
    var idAngl = charIDToTypeID( "Angl" );
    var idAng = charIDToTypeID( "#Ang" );
    desc10.putUnitDouble( idAngl, idAng, theRotation );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idbicubicAutomatic = stringIDToTypeID( "bicubicAutomatic" );
    desc10.putEnumerated( idIntr, idIntp, idbicubicAutomatic );
    var idCpy = charIDToTypeID( "Cpy " );
    desc10.putBoolean( idCpy, copyOrNot );
executeAction( idTrnf, desc10, DialogModes.NO );
return theID
} catch (e) {}
};
////// by mike hale, via paul riggott //////
function getLayerIndex(theId){
var ref = new ActionReference(); 
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('itemIndex'));
//  ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); 
ref.putIdentifier(charIDToTypeID("Lyr "), theId); 
d = executeActionGet(ref);
return (d.getInteger(stringIDToTypeID('itemIndex'))-1); 
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
    var desc58 = new ActionDescriptor();
        var ref19 = new ActionReference();
        ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
    desc58.putReference( stringIDToTypeID( "null" ), ref19 );
        var ref20 = new ActionReference();
        ref20.putIndex( idlayer, theIndex );
    desc58.putReference( stringIDToTypeID( "to" ), ref20 );
    desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
    desc58.putInteger( stringIDToTypeID( "version" ), 5 );
        var list11 = new ActionList();
        list11.putInteger(thisLayerId);
    desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// get active layer’s id //////
function getLayerId () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var layerDesc = executeActionGet(ref);
//var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
/*var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];*/
//return [theName, theID, theseBounds, theWidth, theHeight]
return theID
};
////// number of layers //////
function getTopLayerIndex () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var docDesc = executeActionGet(ref);
var numberOfLayers = docDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var background = docDesc.getBoolean(stringIDToTypeID("hasBackgroundLayer"));
if (background == false) {numberOfLayers--};
return numberOfLayers
};

 

 

 

updated 2024-01-07

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines