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

Relink to new resource folder , not each file one by one.

Enthusiast ,
Jan 05, 2024 Jan 05, 2024

Could we have an option to re-link all linked files to a new folder  at once.    If  that  new folder have same named files.    it's so tediouce  to do it one by one.    

 

Maybe sombody could write a script  that can do it ?      Something that would show a dialog where we could paste new path  and Photoshop would re-link  selected smart objects to  files of same name in that new folder.       I often need it  when I do versions of same 3d render  I save in a new folder .    So all AOV files could be relinked at once.

Idea No status
TOPICS
Actions and scripting , Windows
919
Translate
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

correct answers 1 Correct answer

Community Expert , Jan 07, 2024 Jan 07, 2024

@kirkr5689 

 

A proof of concept script below. I know that you prefer to paste in the path to the new folder from the clipboard into a prompt, if this script works as you require it should be easy enough to update the code to paste the path rather than selecting it:

 

 

/*
Relink SO to New Folder.jsx
v1.0 - 8th January 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/relink-to-new-resource-folder-not-each-file-one-by-one/idc-p/14336634
*/

#target photoshop

var thePa
...
Translate
10 Comments
Community Expert ,
Jan 05, 2024 Jan 05, 2024
quote

Maybe sombody could write a script  that can do it ?


By @kirkr5689

 

A simple script here to relink to a folder:

 

https://community.adobe.com/t5/photoshop-ecosystem-ideas/is-there-an-option-that-shows-me-a-list-wit...

 

A more complex script with a GUI:
 
 
Translate
Report
Enthusiast ,
Jan 05, 2024 Jan 05, 2024

Thank you very much Stephen .  The first one sort of works  but you still have to do manual  job  by  finding folder and selecting  files one by one.   Better than  default way for sure .  The second link  doesn let me point target folder even if  I choose "select target folder ' from drop down .

 

Would be cool if it was just  copy/paste the path to some input box   and all

Translate
Report
Community Expert ,
Jan 05, 2024 Jan 05, 2024

I wrote the first script. Selecting the folder via a GUI seems less prone to error, but it could be easily changed to a prompt where you paste a path. It should loop over all top level layers and update the link. Can you share a sample file or a screenshot of the layers panel? Do you have nested groups or artboards?

Translate
Report
Enthusiast ,
Jan 05, 2024 Jan 05, 2024

// Photoshop JavaScript to sequentially relink selected smart objects to the same named file in another folder. smart objects should be named as filename.tga

#target photoshop

if (app.documents.length > 0) {
var doc = app.activeDocument;

// Function to read the last used path from a temp file
function readLastUsedPath() {
var tempFile = new File(Folder.temp + "/photoshop_relink_path.txt");
if (tempFile.exists) {
tempFile.open('r');
var path = tempFile.read();
tempFile.close();
return path;
}
return "";
}

// Function to save the path to a temp file
function saveLastUsedPath(path) {
var tempFile = new File(Folder.temp + "/photoshop_relink_path.txt");
tempFile.open('w');
tempFile.write(path);
tempFile.close();
}

// Function to get names of selected smart objects
function getSelectedSmartObjects() {
var selectedSmartObjects = [];
var layers = doc.layers;
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
if (layer.kind == LayerKind.SMARTOBJECT && layer.visible) {
selectedSmartObjects.push(layer.name);
}
}
return selectedSmartObjects;
}

// Function to relink a single smart object by name
function relinkSmartObjectByName(layerName, newFolderPath) {
var layer = doc.artLayers.getByName(layerName);
doc.activeLayer = layer;

if (layer.kind == LayerKind.SMARTOBJECT) {
var newFilePath = new File(newFolderPath + "/" + layerName);
if (newFilePath.exists) {
try {
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc.putPath(idnull, newFilePath);
executeAction(idplacedLayerReplaceContents, desc, DialogModes.NO);
} catch (e) {
alert("Error relinking smart object: " + e.message);
}
} else {
alert("File does not exist: " + newFilePath);
}
}
}

// Function to create a temporary layer to deselect all layers
function createTempLayerToDeselect() {
var tempLayer = doc.artLayers.add();
tempLayer.name = "tempDeselectLayer";
doc.activeLayer = tempLayer;
}

// Function to remove the temporary layer
function removeTempLayer() {
var tempLayer = doc.artLayers.getByName("tempDeselectLayer");
tempLayer.remove();
}

// Main Dialog
var lastUsedPath = readLastUsedPath();
var newFolderDialog = new Window("dialog", "Enter New Folder Path");
var input = newFolderDialog.add("edittext", undefined, lastUsedPath);
input.size = [300, 25];
var okButton = newFolderDialog.add("button", undefined, "OK");

okButton.onClick = function() {
var newFolderPath = input.text;
newFolderDialog.close();
saveLastUsedPath(newFolderPath);

var selectedSmartObjectNames = getSelectedSmartObjects();
createTempLayerToDeselect();

for (var i = 0; i < selectedSmartObjectNames.length; i++) {
relinkSmartObjectByName(selectedSmartObjectNames[i], newFolderPath);
}

removeTempLayer();
}

newFolderDialog.show();
} else {
alert("No document is open");
}

 

 

Here is a code chatGPT made me and it sort of works for me.    You have to have file name and extention be  in linked smart object name   and you have to input the path in a box  with  \  simbol  in the end . You need to add  it  if you copy the path from windows file explorer   and doesn't when copy the path from total commander .

 

The problem is all the selected  linked smart objects ( it works for selected ones)  should be  visiable  , with eye symbol  on and I need it to work for ones with eye off too.     ChatGPt looks like unable  to do it.   

 

Could somebody modify it  for working that way please.     

 

ps. contrary to what first line says it could be any file type not only tga and I have no idea why it makes temporary layer

Translate
Report
Enthusiast ,
Jan 05, 2024 Jan 05, 2024

https://www.dropbox.com/scl/fi/0quvq0kqqrjod7c617d7o/relink.7z?rlkey=g19ccm7atvuwbeddoj22h7z4h&dl=0

 

Thanks Stephen_A_Marsh .       Here is a test file and two folders.    The script chatGPT made me works only if selected   linked smart objects are not inside a group  . It makes it useless for me actually since I need it to work for clipping group stacks .  Although I love how simple it works with just a path box and copy paste  from file manager .  

 

ps. well . not totally useless  since you can keep it in main stack ungrouped  somewhere below    and linked copies inside clipping  groups . But too many conditions to follow indeed.       Surprised it works at all . 

Translate
Report
Community Expert ,
Jan 07, 2024 Jan 07, 2024

@kirkr5689 

 

A proof of concept script below. I know that you prefer to paste in the path to the new folder from the clipboard into a prompt, if this script works as you require it should be easy enough to update the code to paste the path rather than selecting it:

 

 

/*
Relink SO to New Folder.jsx
v1.0 - 8th January 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/relink-to-new-resource-folder-not-each-file-one-by-one/idc-p/14336634
*/

#target photoshop

var thePath = Folder.selectDialog('Relink to Folder');

processAllLayersAndSets(app.activeDocument);

function processAllLayersAndSets(obj) {
    // Process Layers 
    for (var i = obj.artLayers.length - 1; 0 <= i; i--) {
        activeDocument.activeLayer = obj.artLayers[i];
        relinkSO();
    }
    // Process Layer Set Layers 
    for (var i = obj.layerSets.length - 1; 0 <= i; i--) {
        processAllLayersAndSets(obj.layerSets[i]);
    }
}

function relinkSO() {
    if (activeDocument.activeLayer.kind === LayerKind.SMARTOBJECT) {
        var theName = activeDocument.activeLayer.name;
        //alert(thePath + "/" + theName);
        var idplacedLayerRelinkToFile = stringIDToTypeID( "placedLayerRelinkToFile" );
        var desc242 = new ActionDescriptor();
        var idnull = stringIDToTypeID( "null" );
        desc242.putPath( idnull, new File( thePath + "/" + theName ) );
        executeAction(idplacedLayerRelinkToFile, desc242, DialogModes.NO);
    }
}

 

 

EDIT:

Here is a version with a prompt to paste the new folder path into. There is no need to put an ending slash after the folder name, the script will do that:

 

C:\Users\username\Desktop\relink\folder2

 

 

/*
Relink SO to New Folder.jsx
v1.1 - 8th January 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/relink-to-new-resource-folder-not-each-file-one-by-one/idc-p/14336634
*/

#target photoshop

var thePath = prompt("Paste the path to the new folder:", "");

processAllLayersAndSets(app.activeDocument);

function processAllLayersAndSets(obj) {
    // Process Layers 
    for (var i = obj.artLayers.length - 1; 0 <= i; i--) {
        activeDocument.activeLayer = obj.artLayers[i];
        relinkSO();
    }
    // Process Layer Set Layers 
    for (var i = obj.layerSets.length - 1; 0 <= i; i--) {
        processAllLayersAndSets(obj.layerSets[i]);
    }
}

function relinkSO() {
    if (activeDocument.activeLayer.kind === LayerKind.SMARTOBJECT) {
        var theName = activeDocument.activeLayer.name;
        //alert(thePath + "/" + theName);
        var idplacedLayerRelinkToFile = stringIDToTypeID( "placedLayerRelinkToFile" );
        var desc242 = new ActionDescriptor();
        var idnull = stringIDToTypeID( "null" );
        desc242.putPath( idnull, new File( thePath + "/" + theName ) );
        executeAction(idplacedLayerRelinkToFile, desc242, DialogModes.NO);
    }
}

 

NOTE: I did test in Windows, one usually needs to escape back-slashes \\ in paths in code, such as:

 

C:\\Users\\username\\Desktop\\relink\\folder2

 

But that doesn't seem to be required when pasting into a prompt? They are both strings so I'm not sure why. I usually develop on the Mac for cross-platform use, so this is a bit of a mystery to me! But it appears to work without any further coding to make \\ or to change \ into /

 

It is OK to use the cross-platform forward-slash in the code, so no need to make this platform-specific just for aesthetics:

 

thePath + "/" + theName

Translate
Report
Community Expert ,
Jan 09, 2024 Jan 09, 2024

@kirkr5689 

 

So how does my latest 1.1 script work for you?

Translate
Report
Enthusiast ,
Jan 11, 2024 Jan 11, 2024

I try to use Photoshop as a sort of 3d render compositing soft   for game assets . It's usually a doesen of AOVs, masks  and render layers  from Blender  I need to relink to a new version of render, like other camera  renders or a new scene.  It's still too many manual selects and I do select wrong image sometimes with your script.       The  one chatGPT did for me  does it all at once with a single click  . I just have to keep all render layers in the main stack  ungrouped  somewhere bellow  actual compositing and work with thier copies.

 

   Nevertheless thank you very much  for your script . It's still helpfull when I need to relink just a couple of things.

 

 

 

 

Translate
Report
Community Expert ,
Jan 11, 2024 Jan 11, 2024

In my test the script loops over all layers and groups and nested groups. So all you need to do is select the new folder or paste in the path and all links with the same name will have the new path. So I'm not understanding your problem.

Translate
Report
Enthusiast ,
Jan 12, 2024 Jan 12, 2024
LATEST

Yeah. it works perfectly. Thanks again Stephen . Not sure why oother day it asked me to point  to each file manually.  I mixed it it with eralier one probably .

You are the  best  help  here 🙂 

Translate
Report