1

I Have photoshop generated script which change current layer name to NewName

Question : It is posible to change that static value to variable value

Here is Code

function Rename() {
  // Set
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    desc1.putReference(cTID('null'), ref1);
    var desc2 = new ActionDescriptor();
    desc2.putString(cTID('Nm  '), "NewName");
    desc1.putObject(cTID('T   '), cTID('Lyr '), desc2);
    executeAction(cTID('setd'), desc1, dialogMode);
  };

All i want is to remove that "NewName" String and put variable defined from outside of function like

var DynamicName = prompt("Your Name","");

so instead of that NewName i want to call DynamicName Variable! is that possible? I want this cuz sometimes i need to change document name or layer name from my old stored variable from script.

5
  • Semi off-topic, but looking at where this piece of code is going... Maybe you'd be interested in this script I put in github recently: github.com/joonaspaakko/Photoshop-Rename-Layers-Script
    – Joonas
    Commented Apr 18, 2019 at 12:24
  • @Sergey Kristiy where did you learnt javascript on high level??and did you learned all languages like html css to make extensions?
    – Mr.Online
    Commented Apr 18, 2019 at 12:35
  • @Joonas thanks but i hate script UI ( well i love it but i hate user input part) i made around 15 scripts and all of them are least user input type; when it comes to raname i always inherit it from original file or psd name etc but your script it great though :)
    – Mr.Online
    Commented Apr 18, 2019 at 12:37
  • I sorta linked the script in hopes you could use it as an example in building your own. Like for instance the way I rename a layer in my script is with a one liner: app.activeDocument.activeLayer.name = "New name";, which does exactly the same thing as your Action Manager code in the question. That said, Action Manager code is typically faster. — People use scriptui because they need some input from the user... There's not many other uses. The only one I can think of is a progress bar... I think you are telling me you do after all dislike all of scriptui ;)
    – Joonas
    Commented Apr 18, 2019 at 13:55
  • @Joonas i don't dislike scriptUI at all in fact even i need to use it sometimes :) i.sstatic.net/FFhGz.png but the thing i avoid it to use it at least as possible XD
    – Mr.Online
    Commented Apr 19, 2019 at 3:24

1 Answer 1

2

Like in any other language, in javascript you can pass parameters to functions:

rename(DynamicName); //you're passing a parameter DynamicName to function rename()

function rename(newName) { //newName will be a variable name for parameter
    //babla
    desc2.putString(cTID('Nm  '), newName);
    //blabla
};

and you can send a number of variables:

rename("beautiful layer", 10);

function rename(newName, id) {
    //babla
    desc2.putString(cTID('Nm  '), newName); // your variable
    ref.putIdentifier(cTID('Lyr '), id); //second variable
    //blabla
};

or if your variable is defined outside rename() scope you can use it directly:

function rename() {
    //babla
    desc2.putString(cTID('Nm  '), DynamicName); // your variable
    //blabla
};

I'd recommend to do course on the JS on codeacademy or something similar to learn about it, it won't take more than an hour for basics like this.

2
  • well for current script the stuff working but i used the same thing few days ago while making my extension and oddly don't know why i was failing :) anyway this one works now! btw did you tried making extensions or generators??
    – Mr.Online
    Commented Apr 18, 2019 at 7:27
  • 1
    extensions yes: gumroad.com/kritskiy ; generators no Commented Apr 18, 2019 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.