Skip to main content
Kevinoneil
Known Participant
August 1, 2014
Question

Need help cleaning up a script and adding loop

  • August 1, 2014
  • 3 replies
  • 5209 views

I have the following script that I pieced together that basically takes the selected layer, gets its name, makes a text object with that name and then places it in a specific place using the original layer as the reference.

I need to be able to select multiple layers and have it loop through all of them one by one.

Here's the script, I'm hopeing someone can help me out! I'm new at this, but learning quickly!

function run(){

   var docRef = app.activeDocument;

var layerRef = docRef.activeLayer;

var docWidth = docRef.width;

var layer = activeDocument.activeLayer; //Grab the currently selected layer

var myLayer = activeDocument.activeLayer.name;

var layerWidth = layer.bounds[2].value - layer.bounds[0].value;

var relUpperCenterX = layerWidth / 2;

//Calculate length and width based on the rectangular bounds of the selected layer

var idsetd = charIDToTypeID( "setd" );

    var desc2045 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1289 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1289.putProperty( idChnl, idfsel );

    desc2045.putReference( idnull, ref1289 );

    var idT = charIDToTypeID( "T   " );

        var ref1290 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idTrsp = charIDToTypeID( "Trsp" );

        ref1290.putEnumerated( idChnl, idChnl, idTrsp );

    desc2045.putReference( idT, ref1290 );

executeAction( idsetd, desc2045, DialogModes.NO );

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

var idDplc = charIDToTypeID( "Dplc" );

    var desc2046 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1291 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1291.putProperty( idChnl, idfsel );

    desc2046.putReference( idnull, ref1291 );

    var idNm = charIDToTypeID( "Nm  " );

    desc2046.putString( idNm, """working""" );

executeAction( idDplc, desc2046, DialogModes.NO );

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

var idsetdd = charIDToTypeID( "setd" );

    var desc2047 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1292 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1292.putProperty( idChnl, idfsel );

    desc2047.putReference( idnull, ref1292 );

    var idT = charIDToTypeID( "T   " );

    var idOrdn = charIDToTypeID( "Ordn" );

    var idNone = charIDToTypeID( "None" );

    desc2047.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetdd, desc2047, DialogModes.NO );

    //Create a text layer

    var textLayer = activeDocument.artLayers.add(); //Make a new layer on the canvas

    textLayer.kind = LayerKind.TEXT; //Make that layer a text layer

    textLayer.name = layer.name; //Name the layer "Dimensions"

var red = new SolidColor();

    red.rgb.red = 255;

red.rgb.green = 0;

    red.rgb.blue = 0;   

    var textReference = textLayer.textItem; //Create a textItem which we'll use to fill the text layer

    textReference.contents = layer.name //Set the contents of that textItem to the length and width

textReference.color = red;

textReference.size = 40;

textReference.position[0] = Array (Math.floor(0, relUpperCenterX));

var idsetd2 = charIDToTypeID( "setd" );

    var desc2051 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1295 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1295.putProperty( idChnl, idfsel );

    desc2051.putReference( idnull, ref1295 );

    var idT = charIDToTypeID( "T   " );

        var ref1296 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        ref1296.putName( idChnl, "working" );

    desc2051.putReference( idT, ref1296 );

executeAction( idsetd2, desc2051, DialogModes.NO );

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

var idAlgn1 = charIDToTypeID( "Algn" );

    var desc2071 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1313 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1313.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2071.putReference( idnull, ref1313 );

    var idUsng = charIDToTypeID( "Usng" );

    var idADSt = charIDToTypeID( "ADSt" );

    var idAdCH = charIDToTypeID( "AdCH" );

    desc2071.putEnumerated( idUsng, idADSt, idAdCH );

executeAction( idAlgn1, desc2071, DialogModes.NO );

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

var idAlgn2 = charIDToTypeID( "Algn" );

    var desc2072 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1314 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1314.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2072.putReference( idnull, ref1314 );

    var idUsng = charIDToTypeID( "Usng" );

    var idADSt = charIDToTypeID( "ADSt" );

    var idAdTp = charIDToTypeID( "AdTp" );

    desc2072.putEnumerated( idUsng, idADSt, idAdTp );

executeAction( idAlgn2, desc2072, DialogModes.NO );

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

var idsetd3 = charIDToTypeID( "setd" );

    var desc2073 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1315 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1315.putProperty( idChnl, idfsel );

    desc2073.putReference( idnull, ref1315 );

    var idT = charIDToTypeID( "T   " );

    var idOrdn = charIDToTypeID( "Ordn" );

    var idNone = charIDToTypeID( "None" );

    desc2073.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetd3, desc2073, DialogModes.NO );

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

var idmove = charIDToTypeID( "move" );

    var desc2074 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1316 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1316.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2074.putReference( idnull, ref1316 );

    var idT = charIDToTypeID( "T   " );

        var desc2075 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idRlt = charIDToTypeID( "#Rlt" );

        desc2075.putUnitDouble( idHrzn, idRlt, 0.000000 );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idRlt = charIDToTypeID( "#Rlt" );

        desc2075.putUnitDouble( idVrtc, idRlt, -50.000000 );

    var idOfst = charIDToTypeID( "Ofst" );

    desc2074.putObject( idT, idOfst, desc2075 );

executeAction( idmove, desc2074, DialogModes.NO );

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

var idslct1 = charIDToTypeID( "slct" );

    var desc2077 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1318 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        ref1318.putName( idChnl, "working" );

    desc2077.putReference( idnull, ref1318 );

executeAction( idslct1, desc2077, DialogModes.NO );

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

var idDlt2 = charIDToTypeID( "Dlt " );

    var desc2078 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1319 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1319.putEnumerated( idChnl, idOrdn, idTrgt );

    desc2078.putReference( idnull, ref1319 );

executeAction( idDlt2, desc2078, DialogModes.NO );

}

run();

This topic has been closed for replies.

3 replies

Kevinoneil
Known Participant
August 5, 2014

That works like a charm! Thank you so much for all of your help! This has really opened up a nice workflow option for my team.  I have a few more challenges that I've yet to tackle that can likely reuse parts of this script as well. I'd hate to ask you to do more for me, but if you are down to help out let me know and I'll fill you in on what I'm trying to accomplish. Thanks again, I can't really tell you how awesome this is!

Inspiring
August 6, 2014

I'm glad that worked for you and it's helping you . As for the next scripts I can help you, but I can't promise anything as I am doing these scripts in my spare time, so I couldn't do them from one day to another... I think the best way is to make a thread here as there are a lot of guys better than me with scripting and with a lot more experience that are willing to help. Also you can sent me a private message if for some reasons you don't want to share with the community what you want to accomplish....

Kevinoneil
Known Participant
August 12, 2014

Ran into one minor snag that I'm trying to solve.

It's scaling the smart object down a hundredth of a percent on one side. Not sure why it's doing this. I actually force the percentage to be equal on both sides before running. Any ideas? the easy fix (if it's possible) would be to add that lock at the end of the process.

Inspiring
August 5, 2014

ok I think I've found what the problem is, your canvas actually is bigger than the layer for the smart object, I will adjust that but it means I have to create a layer in the smart object to match the canvas...

Inspiring
August 1, 2014

I've added 3 more functions the main runMulti() will get the selected layersIDX and select each one in the loop than just run your funtion:

runMulti();

// run();     

function runMulti(){

    var selLay = getSelectedLayersIdx();

    if( selLay.constructor != Array ) selLay = [ selLay ];

    for( var i=0; i<selLay.length; i++){

        multiSelectByIDx(selLay);

        run();

    }

}

function run(){

   var docRef = app.activeDocument;

var layerRef = docRef.activeLayer;

var docWidth = docRef.width;

var layer = activeDocument.activeLayer; //Grab the currently selected layer

var myLayer = activeDocument.activeLayer.name;

var layerWidth = layer.bounds[2].value - layer.bounds[0].value;

var relUpperCenterX = layerWidth / 2;

//Calculate length and width based on the rectangular bounds of the selected layer

var idsetd = charIDToTypeID( "setd" );

    var desc2045 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1289 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1289.putProperty( idChnl, idfsel );

    desc2045.putReference( idnull, ref1289 );

    var idT = charIDToTypeID( "T   " );

        var ref1290 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idTrsp = charIDToTypeID( "Trsp" );

        ref1290.putEnumerated( idChnl, idChnl, idTrsp );

    desc2045.putReference( idT, ref1290 );

executeAction( idsetd, desc2045, DialogModes.NO );

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

var idDplc = charIDToTypeID( "Dplc" );

    var desc2046 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1291 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1291.putProperty( idChnl, idfsel );

    desc2046.putReference( idnull, ref1291 );

    var idNm = charIDToTypeID( "Nm  " );

    desc2046.putString( idNm, """working""" );

executeAction( idDplc, desc2046, DialogModes.NO );

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

var idsetdd = charIDToTypeID( "setd" );

    var desc2047 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1292 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1292.putProperty( idChnl, idfsel );

    desc2047.putReference( idnull, ref1292 );

    var idT = charIDToTypeID( "T   " );

    var idOrdn = charIDToTypeID( "Ordn" );

    var idNone = charIDToTypeID( "None" );

    desc2047.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetdd, desc2047, DialogModes.NO );

    //Create a text layer

    var textLayer = activeDocument.artLayers.add(); //Make a new layer on the canvas

    textLayer.kind = LayerKind.TEXT; //Make that layer a text layer

    textLayer.name = layer.name; //Name the layer "Dimensions"

var red = new SolidColor();

    red.rgb.red = 255;

red.rgb.green = 0;

    red.rgb.blue = 0;   

    var textReference = textLayer.textItem; //Create a textItem which we'll use to fill the text layer

    textReference.contents = layer.name //Set the contents of that textItem to the length and width

textReference.color = red;

textReference.size = 40;

textReference.position[0] = Array (Math.floor(0, relUpperCenterX));

var idsetd2 = charIDToTypeID( "setd" );

    var desc2051 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1295 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1295.putProperty( idChnl, idfsel );

    desc2051.putReference( idnull, ref1295 );

    var idT = charIDToTypeID( "T   " );

        var ref1296 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        ref1296.putName( idChnl, "working" );

    desc2051.putReference( idT, ref1296 );

executeAction( idsetd2, desc2051, DialogModes.NO );

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

var idAlgn1 = charIDToTypeID( "Algn" );

    var desc2071 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1313 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1313.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2071.putReference( idnull, ref1313 );

    var idUsng = charIDToTypeID( "Usng" );

    var idADSt = charIDToTypeID( "ADSt" );

    var idAdCH = charIDToTypeID( "AdCH" );

    desc2071.putEnumerated( idUsng, idADSt, idAdCH );

executeAction( idAlgn1, desc2071, DialogModes.NO );

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

var idAlgn2 = charIDToTypeID( "Algn" );

    var desc2072 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1314 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1314.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2072.putReference( idnull, ref1314 );

    var idUsng = charIDToTypeID( "Usng" );

    var idADSt = charIDToTypeID( "ADSt" );

    var idAdTp = charIDToTypeID( "AdTp" );

    desc2072.putEnumerated( idUsng, idADSt, idAdTp );

executeAction( idAlgn2, desc2072, DialogModes.NO );

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

var idsetd3 = charIDToTypeID( "setd" );

    var desc2073 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1315 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref1315.putProperty( idChnl, idfsel );

    desc2073.putReference( idnull, ref1315 );

    var idT = charIDToTypeID( "T   " );

    var idOrdn = charIDToTypeID( "Ordn" );

    var idNone = charIDToTypeID( "None" );

    desc2073.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetd3, desc2073, DialogModes.NO );

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

var idmove = charIDToTypeID( "move" );

    var desc2074 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1316 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1316.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2074.putReference( idnull, ref1316 );

    var idT = charIDToTypeID( "T   " );

        var desc2075 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idRlt = charIDToTypeID( "#Rlt" );

        desc2075.putUnitDouble( idHrzn, idRlt, 0.000000 );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idRlt = charIDToTypeID( "#Rlt" );

        desc2075.putUnitDouble( idVrtc, idRlt, -50.000000 );

    var idOfst = charIDToTypeID( "Ofst" );

    desc2074.putObject( idT, idOfst, desc2075 );

executeAction( idmove, desc2074, DialogModes.NO );

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

var idslct1 = charIDToTypeID( "slct" );

    var desc2077 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1318 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        ref1318.putName( idChnl, "working" );

    desc2077.putReference( idnull, ref1318 );

executeAction( idslct1, desc2077, DialogModes.NO );

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

var idDlt2 = charIDToTypeID( "Dlt " );

    var desc2078 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1319 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1319.putEnumerated( idChnl, idOrdn, idTrgt );

    desc2078.putReference( idnull, ref1319 );

executeAction( idDlt2, desc2078, DialogModes.NO );

}

function hasBackground(){// function to check if there is a background layer

    var res = undefined;

    try{

        var ref = new ActionReference();

        ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID("Nm  "));

        ref.putIndex( charIDToTypeID("Lyr "), 0 );

        executeActionGet(ref).getString(charIDToTypeID("Nm  ") );

        res = true;

    }catch(e){ res = false}

    return res;

}

function getSelectedLayersIdx(){// get the selected layers index( positon in layer editor)

     var selectedLayers = new Array;

     var ref = new ActionReference();

     ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

     var desc = executeActionGet(ref);

     var add = 1;

     if(hasBackground()){add = 0}

     if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

          desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

          var c = desc.count

          var selectedLayers = new Array();

          for(var i=0;i<c;i++){

               selectedLayers.push(  (desc.getReference( i ).getIndex()) + add);

          }

     }else{

          var ref = new ActionReference();

          ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));

          ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

          srs = hasBackground()?executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1:executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ));

          selectedLayers.push( srs);

     }

     return selectedLayers;

}

function multiSelectByIDx(idx) {

  if( idx.constructor != Array ) idx = [ idx ];

    var layers = new Array();

    var id54 = charIDToTypeID( "slct" );

    var desc12 = new ActionDescriptor();

    var id55 = charIDToTypeID( "null" );

    var ref9 = new ActionReference();

    for (var i = 0; i < idx.length; i++) {

          layers = charIDToTypeID( "Lyr " );

          ref9.putIndex(layers, idx);

    }

    desc12.putReference( id55, ref9 );

    var id58 = charIDToTypeID( "MkVs" );

    desc12.putBoolean( id58, false );

    executeAction( id54, desc12, DialogModes.NO );

}

Kevinoneil
Known Participant
August 1, 2014

works like a charm thanks!

Now I need to figure out how to read the scaling percentage of a smart object and make that part of the layer label.  any chance you've ever done this?

Inspiring
August 4, 2014

Hi,

so reading the scale of a smart object is kind of tricky, the information doesn't appear anywhere or at least I don't know where to search for it , so I've tried to get it by event listener. This is a link for the scripts, you need all these three scripts to be in the same directory, and then just run test_kevinoneil.jsx

The script will tell you when it's done, the best way is to let it work even it seams for one sec that it finished.

And one more thing you need to be sure that you have the "enableEvents" on from the File>Scripts>Scripts Events Manager....

Right now it only works for windows only, just let me know if you need it for mac as well.....

Here are the scripts:

http://www.mediafire.com/download/tdxwm97t0niooiv/test_Kevinoneil.rar