Why scripting is hard for Adobe

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

delizade

Why scripting is hard for Adobe

Post by delizade »

Hi,

First of all, I am new on PS Scripting thing(But I was Falsh Developer for 10 years before). So, things I wrote down below maybe are not true. Sorry if it is. But, every time when I try to solve some problems and needs via scripting, I jump an ugly, unknown dark sea that is not allow to easy swimming.

I just wanted to get an array for selected layers. Here, some good friends had shared their solutions. Thanks a lot for that but after saw those code lines, I can't resist to wonder why why why we can't get selected layers easily? For 2 days I've been struggling for it. Adobe scripting is kind of a low level situation I think. (It makes remember assembly days that we code tens of lines for just multiply two numbers.)

I know, there is no enough documentation for scripting. There are tons of unknown things like ActionMethod thing(I've learn something about that but still code lines seems a Medusa).

As I understand, I will not solve my needs via scripting. Because 2 days to learn how can I get selected layers, after that probably I will spent some hours may be days for how can I get position values of a layer(because I have to change other layers' in other LayerComps) ...etc and there are 3 step more to be need to solved...

Before that, I had tried to write a script for closing unwrapped groups, erasing blank layers, delete unused effects and unwrap effects lines of layers. I could achieve to make some but after wasting hours I've realised that PS can not get out alive from this process IF you have tons of layers in your document. I mean, script processes is not fast enough.

This is really sad actually. We talk about Adobe here. Is there anybody who worked with 3dsMax scripting? God, what a powerful thing it was. I've made a huge animations and controlled tons of object there in 3 days. I am not super hero, I wanted to say it is easy, easy to understand, easy to control things...

anyway, thanks for all to help here to us. again, my problems and opinions may be not right. I will be glad to see right ones.
I guess I talked much... sorry for my boring reproaches.

thank you and sorry for my poor English.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Why scripting is hard for Adobe

Post by Paul MR »

To remove unused effects
http://www.scriptsrus.talktalk.net/RemoveFX.htm

Some layer Comps scrips
http://www.scriptsrus.talktalk.net/CleanLayerComps.htm

To select selected layers
Code: Select allfunction getSelectedLayersIdx(){
      var selectedLayers = new Array;
      var ref = new ActionReference();
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var desc = executeActionGet(ref);
      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++){
            try{
               activeDocument.backgroundLayer;
               selectedLayers.push(  desc.getReference( i ).getIndex() );
            }catch(e){
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
            }
          }
       }else{
         var ref = new ActionReference();
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
         try{
            activeDocument.backgroundLayer;
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
         }catch(e){
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
         }
     var vis = app.activeDocument.activeLayer.visible;
        if(vis == true) app.activeDocument.activeLayer.visible = false;
        var desc9 = new ActionDescriptor();
    var list9 = new ActionList();
    var ref9 = new ActionReference();
    ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    list9.putReference( ref9 );
    desc9.putList( charIDToTypeID('null'), list9 );
    executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );
    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
        app.activeDocument.activeLayer.visible = vis;
      }
      return selectedLayers;
};



To select a layer by index
Code: Select allfunction selectLayerByIndex(index,add){
   add = (add == undefined)  ? add = false : add;
 var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    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){}
};


N.B. if you move/delete a layer the index is changed!
Each layer has its own layerId that is unique and does not change if a layer is moved.

To get the layer ID by index..
Code: Select allfunction getLayerID(idx) {
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
}


To select a layer by layer ID
Code: Select allfunction selectLayerById(ID, add) {
    add = (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);
   executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
}
Mike Hale

Why scripting is hard for Adobe

Post by Mike Hale »

I was going to say that we understand the problems with scripting Photoshop. This forum was started be be a place to find help from others who have already spent the hours that may be needed to script a task.

And Paul proved my point.

I would suggest that before you spend a lot of time trying to do something that you do a search here. There is a good chance someone else has posted a way to do it. If you can't find exactly what you need, post a question. Only spend a lot of time if no one helps or unless you enjoy the process of doing it all by yourself.
delizade

Why scripting is hard for Adobe

Post by delizade »

thank you for your help.
delizade

Why scripting is hard for Adobe

Post by delizade »

Paul, do you know that who is the owner of http://www.scriptsrus.talktalk.net/ ?
I need to ask how his/her "Apply layer to all layer comps" script run correctly. because it is not processed correctly it seems that.

I have 500 layers, 25 comps. today again, wanted to try it. Because I changed navigation bar totally and It has to be changed on other comps. I realised that this is painful and I have to remove all comps and create again.

When a layer group are changed, I run this script but there is happening something but definitely my selected and changed layers are not implemented to other comps.
is there any idea or who is the owner of that script (apparently he or she doesn't want to get email like this)
Paul MR

Why scripting is hard for Adobe

Post by Paul MR »

Hi, sorry to hear it is failing for you.
The site is mine and what the script does it get the layer ID of the layer that is to be applied, then select each layercomp in turn and make the new layer visible then update the layercomp.
It was written to be able to add a new layer to all layercomps, it is not intended to update an existing layer