• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Script error replacing smart object layers if placed in group within layers panel

Community Beginner ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

Hello,

 

I have this script that works to replace smart object layers on the main layers panel, however if I place a smart object(s) into a group or folder, I am getting this error. hopefully there is an easy solution.

(see attachment)

 

Here is the script:

 

#target photoshop

app.activeDocument.suspendHistory("Relink Top-Level Smart Object Layers Using New Name Prefix.jsx", "promptAndReplace()");

//-------- Used in promptAndReplace() --------

function getSmartObjectReference() {
// https://stackoverflow.com/questions/63010107/get-a-smart-objects-layers-files-directory-source-in-js...
try {
var smartObject = {
found: false,
fileRef: '',
filePath: '',
linked: false,
};
var ref, so;
ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));
smartObject.found = true;
smartObject.linked = so.getBoolean(stringIDToTypeID("linked"));
smartObject.fileRef = so.getString(stringIDToTypeID("fileReference"));
if (smartObject.linked) {
smartObject.filePath = so.getPath(stringIDToTypeID("link"));
} else {
smartObject.filePath = Folder.temp + '/' + smartObject.fileRef;
}
return smartObject;
} catch (e) {
alert(e);
return smartObject;
}
}

//------ main function -------
function promptAndReplace() {

// Get the active document
var doc = app.activeDocument;

// Set global team name avariables
var prefixTeam1 = ""
var prefixTeam2 = ""
var prefixTeam3 = ""
var prefixTeam4 = ""

//Check for the existence of layers that have TEAM1 then prompt user
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
activeDocument.activeLayer = activeDocument.artLayers[i];
var layerName = layer.name;

if (layer.kind == LayerKind.SMARTOBJECT) {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));

if (so.getBoolean(stringIDToTypeID("linked"))) {

// Relink file if the layer name contains "AWAY" of "HOME" and is NOT a swatch
if ((layerName.indexOf("TEAM1") !== -1)) {

prefixTeam1 = prompt("Enter the new linked filename prefix for TEAM1:", "TEAM1");
break;
}
}
}
}

//Check for the existence of layers that have TEAM_02 then prompt user
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
activeDocument.activeLayer = activeDocument.artLayers[i];
var layerName = layer.name;

if (layer.kind == LayerKind.SMARTOBJECT) {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));

if (so.getBoolean(stringIDToTypeID("linked"))) {

// Relink file if the layer name contains "AWAY" of "HOME" and is NOT a swatch
if ((layerName.indexOf("TEAM2") !== -1)) {

prefixTeam2 = prompt("Enter the new linked filename prefix for TEAM2:", "TEAM2");
break;
}
}
}
}

//Check for the existence of layers that have TEAM3 then prompt user
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
activeDocument.activeLayer = activeDocument.artLayers[i];
var layerName = layer.name;

if (layer.kind == LayerKind.SMARTOBJECT) {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));

if (so.getBoolean(stringIDToTypeID("linked"))) {

// Relink file if the layer name contains "AWAY" of "HOME" and is NOT a swatch
if ((layerName.indexOf("TEAM3") !== -1)) {

prefixTeam3 = prompt("Enter the new linked filename prefix for TEAM3:", "TEAM3");
break;
}
}
}
}

//Check for the existence of layers that have TEAM4 then prompt user
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
activeDocument.activeLayer = activeDocument.artLayers[i];
var layerName = layer.name;

if (layer.kind == LayerKind.SMARTOBJECT) {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));

if (so.getBoolean(stringIDToTypeID("linked"))) {

// Relink file if the layer name contains "AWAY" of "HOME" and is NOT a swatch
if ((layerName.indexOf("TEAM4") !== -1)) {

prefixTeam4 = prompt("Enter the new linked filename prefix for TEAM4:", "TEAM4");
break;
}
}
}
}

// MAIN - Iterates thru layers and changes path based on layer name
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
activeDocument.activeLayer = activeDocument.artLayers[i];
var layerName = layer.name;

if (layer.kind == LayerKind.SMARTOBJECT) {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));
if (so.getBoolean(stringIDToTypeID("linked"))) {

// Set the file path and name variables
var thePathFull = getSmartObjectReference().filePath.fsName.toString();

// Get the path up to the file itself
var finalSlashIndex = thePathFull.lastIndexOf("\\");
var thePath = thePathFull.substring(0, finalSlashIndex + 1);

// Get the suffix of the file name after the first underscore
var theFileNameFull = thePathFull.substring(finalSlashIndex); // the full filename
var firstUnderscoreIndex = theFileNameFull.indexOf("_"); //location of the first underscore in filename
var theName = theFileNameFull.substring(firstUnderscoreIndex);

var theAD = new ActionDescriptor();

if ((layerName.indexOf("TEAM1") !== -1) && (layerName.indexOf("_") !== -1)) { // TEAM1 non-swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam1 + theName));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM1") !== -1) && (layerName.indexOf("_") == -1)) { // TEAM1 swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam1 + ".png"));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM2") !== -1) && (layerName.indexOf("_") !== -1)) { // TEAM2 non-swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam2 + theName));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM2") !== -1) && (layerName.indexOf("_") == -1)) { // TEAM2 swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam2 + ".png"));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM3") !== -1) && (layerName.indexOf("_") !== -1)) { // TEAM3 non-swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam3 + theName));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM3") !== -1) && (layerName.indexOf("_") == -1)) { // TEAM3 swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam3 + ".png"));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM4") !== -1) && (layerName.indexOf("_") !== -1)) { // TEAM4 non-swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam4 + theName));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

} else if ((layerName.indexOf("TEAM4") !== -1) && (layerName.indexOf("_") == -1)) { // TEAM4 swatch

theAD.putPath(stringIDToTypeID("null"), new File(thePath + prefixTeam4 + ".png"));
executeAction(stringIDToTypeID("placedLayerRelinkToFile"), theAD, DialogModes.NO);

}
}
}
}
}

TOPICS
Actions and scripting

Views

75

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

LATEST

error.png

 

Line 51:

 

var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));

 

As the active layer is a layerSet, there is no smart object value to retrieve.

 

So you would need to add a conditional check to see if the active layer is a layerSet, and if it is loop over the layers in the set.

 

Code snippets (code isn't fully functional, it's just an example):

 

// Check if the active layer is a group
if (app.activeDocument.activeLayer.typename === "LayerSet") {
    // Loop over the contents of the selected layer group
    for (var i = 0; i < app.activeDocument.activeLayer.layers.length; i++) {
    alert('Do stuff for layers in a group');
}
} else {
    alert('Do the same stuff for layers outside of a group');
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines