Skip to main content
Tilles
Inspiring
November 17, 2021
Answered

Convert to Smart Object script command not working anymore

  • November 17, 2021
  • 6 replies
  • 2083 views

hi guys,
With this last update (23.0.0), I realize that my script that convert layers to smart objects is throwing an error now. The error is attached below.
I try to route again the 'convert to smart object' command, with ScriptListener plugin, but the code output is still the same.
Any insights about this error?

This topic has been closed for replies.
Correct answer Tilles

hi guys! thx for the answers. I took the time to debug the code properly. All these scripts to convert to smart object work just fine. My script loops through selected layers and convert each selection to one Smart Object.

The problem was with the activeLayer property ... passing a layer to this property seems not to work in some situations, so the command "Convert to Smart Object" was not being able to execute without a layer selected.

For example, this code, that should select the first layer:

var docRef = app.activeDocument;
docRef.activeLayer = docRef.layers[0];

It works when:
- One or more layers are selected, but not the first layer.

It doesn't work when:
- No layer is selected
- Some layers are selected, including the first

This is specific to trying to select the first layer. If you change '0' for other index, it works fine.
I did a workaround to fix this. Sorry about the confusion and thanks for the support.

6 replies

Tilles
TillesAuthorCorrect answer
Inspiring
November 18, 2021

hi guys! thx for the answers. I took the time to debug the code properly. All these scripts to convert to smart object work just fine. My script loops through selected layers and convert each selection to one Smart Object.

The problem was with the activeLayer property ... passing a layer to this property seems not to work in some situations, so the command "Convert to Smart Object" was not being able to execute without a layer selected.

For example, this code, that should select the first layer:

var docRef = app.activeDocument;
docRef.activeLayer = docRef.layers[0];

It works when:
- One or more layers are selected, but not the first layer.

It doesn't work when:
- No layer is selected
- Some layers are selected, including the first

This is specific to trying to select the first layer. If you change '0' for other index, it works fine.
I did a workaround to fix this. Sorry about the confusion and thanks for the support.

Legend
November 18, 2021
quote

For example, this code, that should select the first layer:

var docRef = app.activeDocument;
docRef.activeLayer = docRef.layers[0];

It works when:
- One or more layers are selected, but not the first layer.

It doesn't work when:
- No layer is selected
- Some layers are selected, including the first

This is specific to trying to select the first layer. If you change '0' for other index, it works fine.
I did a workaround to fix this. Sorry about the confusion and thanks for the support.


By @Tilles

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/targeting-a-layer-when-none-is-selected/m-p/8535437#M279697

Tilles
TillesAuthor
Inspiring
November 19, 2021

Hi r-bin, thx for your reply.
I was looking for something like this, a layer ID or something, but the official guide doesn't say anything about it.
In fact, scripting for Photoshop is so cumbersome ...
Anyway, your code really worked, thanks. I'm surprised that you have to do some workaround to select a layer that should be selected with the actual code. And it's been 3 years ... I guess no one cares for PS scripting evolution

Legend
November 17, 2021
var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
executeAction( idnewPlacedLayer, undefined, DialogModes.NO );

This code seems to run fine for me on a single layer as well as multiple layers.

Stephen Marsh
Community Expert
Community Expert
November 17, 2021

Strange, I have seen the opposite with other operations in older versions, the run menu item didn't work but the AM code did.

 

Just curious, I converted the recorded command into an action and converted it into a script via xtools, does this work?

 

#target photoshop
//
// Action29.jsx
//
//
// Generated Thu Nov 18 2021 07:25:21 GMT+1100
//
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
//
//==================== Action 29 ==============
//
function Action29() {
  // Convert to Smart Object
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    executeAction(sTID('newPlacedLayer'), undefined, dialogMode);
  };
  step1();      // Convert to Smart Object
};
//=========================================
//                    Action29.main
//=========================================
//
Action29.main = function () {
  Action29();
};
Action29.main();
// EOF
"Action29.jsx"
// EOF
Tilles
TillesAuthor
Inspiring
November 17, 2021

Using runMenuItem method did the job. For some reason, 'newPlacedLayer' is not available anymore, as a command.
Thanks again!

Tilles
TillesAuthor
Inspiring
November 17, 2021

hi Stephen! thanks for the reply.

my bad, I thought of posting my code, but I forgot.
It's the same as your first solution:

var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
executeAction( idnewPlacedLayer, undefined, DialogModes.NO );

It's been working for years, but it's throwing this error now. I'll try your other methods / functions and come back here tomorrow.

Stephen Marsh
Community Expert
Community Expert
November 17, 2021

What is the layer content?

 

I obviously don't know what code you are currently using as you didn't post it, but try this (presuming at least one selected layers):

 

executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);

 

or

 

newPlacedLayer();
function newPlacedLayer() {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
}

 

or

runMenuItem(stringIDToTypeID('newPlacedLayer'));