Skip to main content
Participant
December 15, 2019
Question

How to make a simple script that copy a layer name and paste in a new text layer.

  • December 15, 2019
  • 2 replies
  • 1315 views

Hello!, I'm using PS CC 2014, I want to make a script that copy the name of an active layer and paste it in a new text layer.

the main idea is to set many names in many number of images to make a secuence, 

example 

at the corner "Image n#1", then  at the second "Image n#2" ,etc..

 

if it is possible to use with Actions and make sure the copy name can be change in several images and it doesn't make the error " the file (Image 1) is not found, I mean it can copy but it doesn't matter the characters that are written in it...

thank you !!!

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
December 16, 2019

I hacked away at Chuck's script from the links above, not sure if this is helpful or not... I know there is a better (shorter) way to align a layer to the canvas edge/s, however, I only know how to do it as I would in an action (select all, align, deselect).

 

/* Initial setup */
var myDoc = app.activeDocument;
var myRulers = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;

/* Text layer related stuff */
var myLayerName = 'PrefixTEXT - ' + myDoc.activeLayer.name + ' - SuffixTEXT'; // Remove the prefix/suffix as required
var myLayerText = myDoc.artLayers.add()
myLayerText.kind = LayerKind.TEXT
var myText = myLayerText.textItem
myColor = new SolidColor
myColor.rgb.red = 255
myColor.rgb.green = 0
myColor.rgb.blue = 0
myLayerText.textItem.color = myColor
myText.position = [0,20] // Upper Left
myText.justification = Justification.LEFT
myText.size = 12
myText.contents = myLayerName;
myLayerText.name = myLayerName // Or add a fixed string in quotes i.e. 'My Great Layer Name!'

/* Select All for the align steps */
myDoc.selection.selectAll();

/* Align to right edge of canvas */
alignRight(false);
function alignRight(alignToCanvas) {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};

	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};

	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();

	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( c2t( "null" ), reference );
	descriptor.putEnumerated( s2t( "using" ), s2t( "alignDistributeSelector" ), s2t( "ADSRights" )); // Right
	descriptor.putBoolean( s2t( "alignToCanvas" ), alignToCanvas );
	executeAction( c2t( "Algn" ), descriptor, DialogModes.NO );
}

/* Align to top edge of canvas */
alignTop(false);
function alignTop(alignToCanvas) {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};

	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};

	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();

	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( c2t( "null" ), reference );
	descriptor.putEnumerated( s2t( "using" ), s2t( "alignDistributeSelector" ), s2t( "ADSTops" )); // Top
	descriptor.putBoolean( s2t( "alignToCanvas" ), alignToCanvas );
	executeAction( c2t( "Algn" ), descriptor, DialogModes.NO );
}

/* Deselect */
myDoc.selection.deselect();

/* Move the layer in 5px */
myDoc.activeLayer.translate( -5, 5 );

/* Reset the ruler units */
app.preferences.rulerUnits = myRulers
Stephen Marsh
Community Expert
Community Expert
December 17, 2019

Cool, with a bit of research it appears that I don't need to run two separate align steps, they can both be called from a single function:

 

// Align Active Layer

app.activeDocument.selection.selectAll();

/* 
//macscripter.net/viewtopic.php?id=38890

AdLf = Align Left
AdRg = Align Right
AdCH = Align Centre Horizontal
AdTp = Align Top
AdBt = Align Bottom
AdCV = Align Centre Vertical
*/

//www.ps-scripts.com/viewtopic.php?f=66&t=7036&p=35273&hilit=align+layer#p35273

align('AdTp'); align('AdRg'); //Change as required

function align(method) { 
app.activeDocument.selection.selectAll();
   var desc = new ActionDescriptor();
           var ref = new ActionReference();
           ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) ); 
       desc.putReference( charIDToTypeID( "null" ), ref ); 
       desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
      try{
   executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO ); 
   }catch(e){}
   app.activeDocument.selection.deselect();
};

 

This makes the code a little less verbose, however, it is still not the answer that I was looking for!