Skip to main content
StrongBeaver
Legend
September 20, 2016
Answered

prompt for layer name

  • September 20, 2016
  • 1 reply
  • 1473 views

var lyrname = prompt("Please Give the Layer a Name");

lyrname.name = newName;

I'm prompting for a layer name then I want to write that layer name but the above code doesn't work.  I tried adding an argument lyrname.name to prompt but no progress ?

This topic has been closed for replies.
Correct answer StrongBeaver

I wrote that your rename code was wrong that  lyrname.name and newName were undefined in you code.

I wrote I had no idea why you made the layer kind a text layer then resterized the layer to a pixel layer when the layer had not content that could be rasterized.  The layer is empty. 

If you leave out the two statement:

newdoc.kind = LayerKind.TEXT;

newdoc.rasterize(RasterizeType.ENTIRELAYER);

the result may be identical for most likely the layer kind would default to be a normal pixel layer.

I know I do not write well but your reading thing into what I write from far off OZ.

You add 5 layers that the user names via a prompt.  The script will only retain the name of the last layer added in var lyrname and have the newdoc layer object to work on the last layer.  IMO the script shows you have no idea as to what you are doing.


I solve it, I should have called the artlayer name.

1 reply

JJMack
Community Expert
Community Expert
September 20, 2016

Do you want to rename an existing layer or add a layer. If Add a layer what kind of layer do you want to add. Here is how to add a new layer and name it using your prompt or rename a layer.

var lyrname = prompt("Please Give the Layer a Name"); 

var newLayer = activeDocument.artLayers.add(); // Create a new ArtLayer object

newLayer.name = lyrname; // name it for later reference

or Rename the existing activelayer

activeDocument.activeLayer.name= lyrname; // rename the layer

JJMack
StrongBeaver
Legend
September 20, 2016

I'm originally prompting to change the layer name after it's created in a loop, I have the add layer before the prompt as the code posted in my previous message but it doesn't work ?

1. Add Layer

2. Prompt to change the layer name // It prompts to enter in a layer name but once I do, I get newName is undefined ?

3. Creates a layer kind

<- Loops - >

StrongBeaver
Legend
September 21, 2016

That is what you posted to begin with.  That will not work. if you added a new layer with a statement  like

var lyrname = activeDocument.artLayers.add();

the variable lyrname is a layer object and there will be a lyrname.name  value in that object.

If you then have a statement like

var lyrname = prompt("Please Give the Layer a Name");

the variable lyrname is  no longer a layer object.  It is a string of characters the user entered for the prompt.

Since  lyrname is no longer  layer object it been redefined as a string I would think.  I would think that you should get an error message when you try to assign lyrname.name  a value for there is no lyrname.name defined it should be undefined.


Sorry, I have a variable for the art layer, making the art layer separate from the prompt command

The loop works, except after I enter a name for a layer I get the following error;

newName is undefined

lyrname.name = newName;

The variable lyrname contains the prompt.