Renaming, moving and few other functions...

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Dariusz1989

Renaming, moving and few other functions...

Post by Dariusz1989 »

Heya

So I'm quite new to JS, I'm more of a Python scripter but in any case can some 1 have a look at my code and suggest few options how can I make it to work?
So here is my 1st basic script:
Code: Select allvar doc = app.activeDocument;
var Test = doc.artLayers.getByName("test_A");
moveLayerToSetLit(doc, Test 'Group_A');
function moveLayerToSetLit(doc, srcLayer, toLayerName) {
   var destLayer = doc.layers[toLayerName];
   srcLayer.move(destLayer, ElementPlacement.INSIDE);
}
The problem with the script above is that if the Group_A is within a group it wont move in to it... so how can I get around that?

Second script I struggle with is a bit more confusing.
Basically I want to select and isoline layer Test_B
Then I want to select Channel R G B and duplicate each R G B channel.
Then I would like to rename that RGB copy to different names I pre setup.
So here is the script so far I got that sort of dont work:
Code: Select allvar doc = app.activeDocument;
var TestB = doc.artLayers.getByName("test_B");
doc.activeChannels=[doc.channels.duplicate('Blue',doc)]; // this select channel but dont duplicate it)
Now I have no idea - yet how to isoline layer but I'll get there aventually. However I dont know why would he not duplicate channel...

No idea how to rename stuff yet so yay...

And the last script I want to use is to select channel by name and then apply it as a mask to folder/layer... I got a code to apply the mask, I got the code to pick channel but I dont know how to select...
Code: Select allvar doc = app.activeDocument;
doc.activeChannels = [doc.channels.getByName('Blue')];
//Missing part :)
makeLayerMask('HdSl');
function makeLayerMask(maskType) {
 if( maskType == undefined) maskType = 'RvlS' ; //from selection
 //requires a selection 'RvlS'  complete mask 'RvlA' otherThanSelection 'HdSl'
    var desc140 = new ActionDescriptor();
    desc140.putClass( charIDToTypeID('Nw  '), charIDToTypeID('Chnl') );
        var ref51 = new ActionReference();
        ref51.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    desc140.putReference( charIDToTypeID('At  '), ref51 );
    desc140.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID(maskType) );
    executeAction( charIDToTypeID('Mk  '), desc140, DialogModes.NO );
}

Any help would be awesome... thanks!
Thanks, bye.

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

Mike Hale

Renaming, moving and few other functions...

Post by Mike Hale »

I have never been able to get the Photoshop Object Model to move one layerSet into another. I use Action Manager.
Code: Select allfunction moveLayerSet( fromLayer, toLayer ){// layerSet objects
    var desc = new ActionDescriptor();
        var sourceRef = new ActionReference();
        sourceRef.putName( charIDToTypeID( "Lyr " ), fromLayer.name );
    desc.putReference( charIDToTypeID( "null" ), sourceRef );
            var indexRef = new ActionReference();
            indexRef.putName( charIDToTypeID("Lyr "), toLayer.name );
            var layerIndex = executeActionGet(indexRef).getInteger(stringIDToTypeID('itemIndex'));
        var destinationRef = new ActionReference();
        destinationRef.putIndex( charIDToTypeID( "Lyr " ), layerIndex-1 );
    desc.putReference( charIDToTypeID( "T   " ), destinationRef );
    desc.putBoolean( charIDToTypeID( "Adjs" ), false );
    desc.putInteger( charIDToTypeID( "Vrsn" ), 5 );
    executeAction( charIDToTypeID( "move" ), desc, DialogModes.NO );
}

Channels is a collection( similar to an array ) of channel objects. The Channels collection does not have a duplicate method. So you have to do something like this.
Code: Select alldoc.activeChannels=[doc.channels.getByName(localize('$$$/ColorModes/RGB/ChannelName/Blue=Blue')).duplicate()];

I think the problem with you last question is that people use 'select' to mean two different things. One means make active and that is what you are doing here. The other means make it a selection. For that function makeLayerMask to make a mask from a channel you have to make a selection of that channel before calling the function.
Code: Select allvar doc = app.activeDocument;
doc.selection.load(doc.channels.getByName('Blue'))
makeLayerMask('HdSl');
function makeLayerMask(maskType) {
 if( maskType == undefined) maskType = 'RvlS' ; //from selection
 //requires a selection 'RvlS'  complete mask 'RvlA' otherThanSelection 'HdSl'
    var desc140 = new ActionDescriptor();
    desc140.putClass( charIDToTypeID('Nw  '), charIDToTypeID('Chnl') );
        var ref51 = new ActionReference();
        ref51.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    desc140.putReference( charIDToTypeID('At  '), ref51 );
    desc140.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID(maskType) );
    executeAction( charIDToTypeID('Mk  '), desc140, DialogModes.NO );
}
Dariusz1989

Renaming, moving and few other functions...

Post by Dariusz1989 »

Heya

Thanks for fast reply !

Right so I tried ur codes... and umh got interesting results...

From 1st code I get this line highlighted with error code "Illegar argument - argument 2"

Code: Select all indexRef.putName( charIDToTypeID("Lyr "), toLayer.name );

I'm having really hard time reading this script atm tbh. I know that Lyr= layer,T=??,null=??,Adjs=adjustments?,Vrsn=version but its really mysterious for me other than that...

Regarding second line of code, that works almost fine. Atm it copies layer with background and if there is no background then it makes it white and dont isoline layer b4 coping it (so I get mixture of layers in channel instead of 1 layer channel data). So in order to fix that I run this script 1st:

Code: Select allfunction selectAllLayers() {
   var ref = new ActionReference();
   ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
   var desc = new ActionDescriptor();
   desc.putReference(cTID('null'), ref);
   executeAction(sTID('selectAllLayers'), desc, DialogModes.NO);
}

function hideLayers() {
   var ref = new ActionReference();
   ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
   var list = new ActionList();
   list.putReference(ref);
   var desc = new ActionDescriptor();
   desc.putList(cTID('null'), list);
   executeAction(cTID('Hd  '), desc, DialogModes.NO);
 }   
   
function cTID(s) {return app.charIDToTypeID(s);}
function sTID(s) {return app.stringIDToTypeID(s);}

selectAllLayers ()
hideLayers()


And now I'm trying to figure out how to unhide the layer I want and then hide it again and unthide another one.I think I should figure out a loop for it ! Hopefully I can crack this one on my own - this one is sorted

As to last script well... it does great job BUT why does it inverse the mask at the end? If I mask Red it mask out red leaving GB data where as I wanted to have visible only Red...

Regarding photoshop scripting is it me or is it totally backwards and retarded?

Thanks for help again !
Mike Hale

Renaming, moving and few other functions...

Post by Mike Hale »

The first function expects a layerSet object as the argument. If you are getting that error I would bet you are just using the layerSet name, which is a string and does not have a name property.

Action Manger code is hard to understand at first by after a while ids like 'Lyr ' are as easy to read as 'layer'.

I am not clear about the next part of you questions. If the layer mask is inverted( hiding areas you want to show ), then you would change the maskType argument from 'HdSl' to 'RvlS'
Dariusz1989

Renaming, moving and few other functions...

Post by Dariusz1989 »

Heya

Humh I gotta thinker with that layerSet a bit longer then.... will get back on that issue !

On another side I'm trying to create and rename group/and or layer....

I wish I could just type

doc.layerSets.add(name = 'Test');

But it dont work and then using commands name/rename dont give my any working results so far so any hints how can I get it to work ? A simple rename example would be awesome. I cant find anything usable on internet

Thanks, bye.
Mike Hale

Renaming, moving and few other functions...

Post by Mike Hale »

If you want to use the Object Model you need to create a named layerSet in two steps.
Code: Select allvar newSet = doc.layerSets.add();
newSet.name = 'Test';

Or you could make an Action Manager to set some of the layerSet properties when it is created.
Code: Select allfunction addLayerSet( setName, opct, mode ){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putClass( stringIDToTypeID( "layerSection" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var setDesc = new ActionDescriptor();
        setDesc.putString( charIDToTypeID( "Nm  " ),setName );
        setDesc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), opct );
        setDesc.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "BlnM" ), mode );
    desc.putObject( charIDToTypeID( "Usng" ), stringIDToTypeID( "layerSection" ), setDesc );
    executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );
}
addLayerSet( 'setName', 20, charIDToTypeID( "Nrml" ) );
Dariusz1989

Renaming, moving and few other functions...

Post by Dariusz1989 »

Heya

Wow that .name example was pretty simple... for some reason I was typing xx.name('test') rather than the xx.name=('test') bah !

Also the Action Manager is beciming slightly ever so cleaner... still dont get half of the code but I can see similarities so yay maybe one day I'll write it myself :O

On the other hand I sort of hit a wall with Photoshop... not sure if its the script or the photoshop or I'm just asking for too much. Basically I have 16 bit file, 30 channel layers at 10x7k resolution. I want to rename them all but once I run all the 30 rename functions PS hangs and all hell break lose... It took him like 30 min to get to start renaming stuff... Very slow lagi and bad... Any hints or am I screwed? :s


Code: Select allBChannel = doc.channels.getByName('Blue copy');
GChannel = doc.channels.getByName('Green copy');
RChannel = doc.channels.getByName('Red copy');

BChannel2 = doc.channels.getByName('Blue copy 2');
GChannel2= doc.channels.getByName('Green copy 2');
RChannel2 = doc.channels.getByName('Red copy 2');

BChannel3 = doc.channels.getByName('Blue copy 3');
GChannel3 = doc.channels.getByName('Green copy 3');
RChannel3 = doc.channels.getByName('Red copy 3');

BChannel4 = doc.channels.getByName('Blue copy 4');
GChannel4 = doc.channels.getByName('Green copy 4');
RChannel4 = doc.channels.getByName('Red copy 4');

BChannel5 = doc.channels.getByName('Blue copy 5');
GChannel5 = doc.channels.getByName('Green copy 5');
RChannel5 = doc.channels.getByName('Red copy 5');

BChannel6 = doc.channels.getByName('Blue copy 6');
GChannel6 = doc.channels.getByName('Green copy 6');
RChannel6 = doc.channels.getByName('Red copy 6');

BChannel7 = doc.channels.getByName('Blue copy 7');
GChannel7 = doc.channels.getByName('Green copy 7');
RChannel7 = doc.channels.getByName('Red copy 7');

BChannel8 = doc.channels.getByName('Blue copy 8');
GChannel8 = doc.channels.getByName('Green copy 8');
RChannel8 = doc.channels.getByName('Red copy 8');

BChannel9 = doc.channels.getByName('Blue copy 9');
GChannel9 = doc.channels.getByName('Green copy 9');
RChannel9 = doc.channels.getByName('Red copy 9');

BChannel10 = doc.channels.getByName('Blue copy 10');
GChannel10= doc.channels.getByName('Green copy 10');
RChannel10 = doc.channels.getByName('Red copy 10');

BChannel11 = doc.channels.getByName('Blue copy 11');
GChannel11 = doc.channels.getByName('Green copy 11');
//RChannel11 = doc.channels.getByName('Red copy 11');



BChannel.name = ("Rims_Screws ");
GChannel.name  = ("Rims_Disk");
RChannel.name  = ("Rims_P_Matte ");
(there is a lot of RGChannelX.name missing here... hopefully this is enought to see the problem for pros...
Thanks, bye.
Mike Hale

Renaming, moving and few other functions...

Post by Mike Hale »

This is just a guess but I think you are running out of resources. Channel object have a histogram property which is an array. I think that may be one reason.

What I would try is something like this to avoid creating a lot of Channel object references and avoid the histogram altogether.
Code: Select alldoc.activeChannels = [doc.channels.getByName('Blue copy')];
renameActiveChannel( "Rims_Screws " );
doc.activeChannels = [doc.channels.getByName('Green copy')];
renameActiveChannel( "Rims_Disk" );
// repeat as needed
// until all channels in list have been renamed
function renameActiveChannel( channelName ){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var chnlDesc = new ActionDescriptor();
        chnlDesc.putString( charIDToTypeID( "Nm  " ), channelName );
    desc.putObject( charIDToTypeID( "T   " ),charIDToTypeID( "Chnl" ), chnlDesc );
    executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
}
Dariusz1989

Renaming, moving and few other functions...

Post by Dariusz1989 »

Hey Mike!

So I worked on my script a bit more today. Gotta say thanks to ur help a lot of pieces started to work together. Its such an massive relieve to get my comping automated! Its just awesome

However I have strange error that I cant crack... the script on its own work. But the moment I add second version or so all die... can you have a look? I googled and so far it appears to be massive pain to get masking to work

Code: Select allmakeLayerMask('RvlS');
function makeLayerMask(maskType) {
    var desc140 = new ActionDescriptor();
    desc140.putClass( charIDToTypeID('Nw  '), charIDToTypeID('Chnl') );
        var ref51 = new ActionReference();
        ref51.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    desc140.putReference( charIDToTypeID('At  '), ref51 );
    desc140.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID(maskType) );
    executeAction( charIDToTypeID('Mk  '), desc140, DialogModes.NO );
    }



doc.selection.load(doc.channels.getByName('Window'))
doc.activeLayer.Interior
makeLayerMask('RvlS');

This is just part of the code. What I'm trying to do is to add 3 masks. 2 RvlS and 1 HdSl Not sure why I get this error : "General Photoshop error occured. This functionality may not be available in this version of Photoshop" on line

Code: Select allexecuteAction( charIDToTypeID('Mk  '), desc1400, DialogModes.NO );

Now I've been troubleshooting it and it goes down to (I think) the fact that he dont select another layerset after he apply the 1st mask.. I have no idea why and I spend past 50 minutes trying to google what is the command for select/highlight and I'm either blind or its not there. Or its so simple no1 bother to ask for it...

Is that it? Just select.something? Help..
Thanks, bye.
Mike Hale

Renaming, moving and few other functions...

Post by Mike Hale »

It may help to look at the Photoshop JavaScript Ref.pdf that is installed with Photoshop. This list the methods and properties in the Object Model. For example you would have seen that this line

doc.activeLayer.Interior

may not work. Interior is not a standard Object Model property. So unless you set that property somewhere else it would be undefined. Even if you did set it to some value, using it like this doesn't do anything.


To select a layer( make it active ) you do something like this

app.activeDocument = app.activeDocument.layers.getByName('Layer 1');