Highlighted

Indesign script not relink with the sub folders

Participant ,
Sep 18, 2020

Copy link to clipboard

Copied

Hi Everyone,

The below code does not relink with the sub folders. It links only main folder images. Could anyone help this

// InDesign files to convert linking from Low Res to Hi Res and vice versa
// Notes
// For use in InDesign only
// e-mail: philip.tauro@hogarthww.com

/****************
* Name: Relinker_v2
* Desc: Relinking from low-res image to high or medium resolution and vice versa
* Dev: Barath Balaji
* E-Mail: barath.balaji@hogarthww.com
* Version 2.0.0
* 1. Added options to relink for Active Document, Multiple Files and Batch Process(Folder)
***************/

#target indesign
//page level variables
var fileList = [];
var refOutputPath = "";
var userInputFolder = "";
var regex = /.indd/gi;
var gScriptName = "Relink to Folders";
var myIcon = File ('~/Documents/Adobe Scripts/DO_NOT_DELETE_scriptResources/Icon/FolderSelectIcon_1.png');
var imFiles = []
var relinkTo = "Folder 1 files"
var Proceed = true;
var doc = null;
var files = new Array();
var option = null;
var sourceFolder = null;
var list = [];
var linksFolder = [];

//Folders and SubFolders Selections
var subFolderImageArray =[];

function getsubfolderfiles(folder) {
var filelist = folder;
for (var i = 0; i < filelist.length; i++) {
if (filelist[i] instanceof Folder) {
getsubfolderfiles(filelist[i]);
} else if (filelist[i] instanceof File) {
if (filelist[i].name.match(/\.(eps|ai|psd|jpg|JPEG|TIFF|tif|BMP|PNG|SVG|PDF|SWF|GIF|INDD)$/i) != null) {
subFolderImageArray.push(decodeURI(filelist[i].parent));
} //if
} //else if
} //for
return subFolderImageArray;
} //function

chooseFilesOption();

if (option == "folder") {
if (sourceFolder != null) {
fileType = "*.indd";
files = sourceFolder.getFiles(fileType);
if (files.length > 0) {
var nextStep = getRelinkFolder();
if(nextStep){
openFiles();
}
} else {
alert("No indesign files in the Folder");
exit(0);
}
} else {
alert("You did not select a Folder");
exit(0);
}
} else if (option == "multiple") {
m = $.os[0] == "M";
exts = ["indd"];
mf = function(f) {
return (f instanceof Folder) || RegExp("\.(" + exts.join("|") + ")$").test(f.name);
};
wf = "Indesign Files:*.indd";
filter = m ? mf : wf;
canSelectMultiple = true;
//requesting file selection
files = File.openDialog('Select the Indesign files to relink.', filter, canSelectMultiple);
if (files != null) {
var nextStep = getRelinkFolder();
if(nextStep){
openFiles();
}
} else {
alert("No indesign files selected");
exit(0);
}
} else if(option==null){
alert("You have cancelled.");
exit(0);
}
if(list.length>0){
alert("Operation Completed.")
}

function chooseFilesOption() {
var dialog = new Window("dialog", "Select an option to Relink Files");
dialog.orientation = "column";
dialog.alignChildren = "fill";

var btnGroup = dialog.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "center";
var folder = btnGroup.add("button", undefined, "Batch Process");
var multipleFiles = btnGroup.add("button", undefined, "Multiple Files");
 
folder.onClick = function() {
option = "folder";
sourceFolder = Folder.selectDialog('Select the folder with Indesign files to relink.');
dialog.close();
}
multipleFiles.onClick = function() {
option = "multiple";
dialog.close();
}
dialog.show();
}

function getRelinkFolder() {
//Choose Resolution
var nextStep = chooseOption();
//Choose links folder
if(nextStep==true){
relinkAssetFolder = imFiles; //3 Folders path
if (relinkAssetFolder != null) {
for(a=0; a<relinkAssetFolder.length; a++){
var allLinksFolder = relinkAssetFolder[a];
getsubfolderfiles(allLinksFolder)
linksFolder.push(allLinksFolder)
}
if (allLinksFolder.length > 0) {
return true;
} else {
alert("No files found in this folder.");
return false;
}
}else{
alert("You have cancelled");
return false;
}
}else{
alert("You did not choose an option");
exit(0);
}
return linksFolder
}
function openFiles(){
for (var i = 0; i < files.length; i++) {
app.open(files[i]);
var myDocument = app.activeDocument;
var myFileName = myDocument.filePath + "/" + myDocument.name;
myDocument.save(File(myFileName), true);
processFile();
}
}
function processFile() {
doc = app.activeDocument;
var linksCount = reLink(doc, linksFolder);
if (linksCount == 0) {
alert("No links found for this file.");
list.push(app.activeDocument.fullName);
} else {
try {
var filePath = decodeURI(doc.filePath)
//Save document to prevent save as dialog
var fullName = doc.name;
var finalDotPosition = fullName.lastIndexOf(".");
if (finalDotPosition > -1) {
var fileName = fullName.substr(0, finalDotPosition);
} else {
var fileName = fullName;
}
doc.save(new File(filePath + '/' + fileName + ".indd"));
doc.close();
} catch (e) {
alert(e);
}
}
}
//get User input.
function chooseOption() {
var process=false;
var dialog = new Window("dialog");
dialog.text = gScriptName;
dialog.orientation = "column";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16;

var panel1 = dialog.add("panel", undefined, undefined, {name: "panel1"});
panel1.text = "Locations";
panel1.orientation = "column";
panel1.alignChildren = ["left","top"];
panel1.spacing = 10;
panel1.margins = 10;

// GROUP1
// ======
var group1 = panel1.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = -5;

var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "Folder 1:";

var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}');
edittext1.preferredSize.width = 400;
edittext1.text = "";

var iconbutton1 = group1.add("iconbutton", undefined, myIcon, {name: "iconbutton1", style: "toolbutton"});

// GROUP2
// ======
var group2 = panel1.add("group", undefined, {name: "group2"});
group2.orientation = "row";
group2.alignChildren = ["left","center"];
group2.spacing = 10;
group2.margins = -5;

var statictext2 = group2.add("statictext", undefined, undefined, {name: "statictext2"});
statictext2.text = "Folder 2:";

var edittext2 = group2.add('edittext {properties: {name: "edittext2"}}');
edittext2.preferredSize.width = 400;
edittext1.text = "";

var iconbutton2 = group2.add("iconbutton", undefined, myIcon, {name: "iconbutton2", style: "toolbutton"});

// GROUP3
// ======
var group3 = panel1.add("group", undefined, {name: "group3"});
group3.orientation = "row";
group3.alignChildren = ["left","center"];
group3.spacing = 10;
group3.margins = -5;

var statictext3 = group3.add("statictext", undefined, undefined, {name: "statictext3"});
statictext3.text = "Folder 3:";

var edittext3 = group3.add('edittext {properties: {name: "edittext3"}}');
edittext3.preferredSize.width = 400;
edittext1.text = "";
 

var iconbutton3 = group3.add("iconbutton", undefined, myIcon, {name: "iconbutton3", style: "toolbutton"});

// GROUP4
// ======
var group4 = panel1.add("group", undefined, {name: "group4"});
group4.orientation = "row";
group4.alignChildren = ["center","center"];
group4.spacing = 10;
group4.margins = 0;
group4.alignment = ["center","top"];

var button1 = group4.add("button", undefined, undefined, {name: "button1"});
button1.text = "Cancel";

var button2 = group4.add("button", undefined, undefined, {name: "button2"});
button2.text = "OK";

iconbutton1.onClick = function() {
relinkTo = "Folder 1 files";
var ImageFolder = Folder.selectDialog("Please Select Images Folder")
var ImageFolderuri = ImageFolder;
var dcodeImageFolder1 = decodeURI(ImageFolderuri);
edittext1.text = dcodeImageFolder1
imFiles.push(dcodeImageFolder1)
process = true;
}
iconbutton2.onClick = function() {
relinkTo = "Folder 2 files";
var ImageFolder = Folder.selectDialog("Please Select Images Folder")
var ImageFolderuri = ImageFolder;
var dcodeImageFolder2 = decodeURI(ImageFolderuri);
edittext2.text = dcodeImageFolder2
imFiles.push(dcodeImageFolder2)
process = true;
}
iconbutton3.onClick = function() {
relinkTo = "Folder 3 files";
var ImageFolder = Folder.selectDialog("Please Select Images Folder")
var ImageFolderuri = ImageFolder;
var dcodeImageFolder3 = decodeURI(ImageFolderuri);
edittext3.text = dcodeImageFolder3
imFiles.push(dcodeImageFolder3)
process = true;
}
button1.onClick = function() {
proceed = true;
dialog.close();
}
button2.onClick = function() {
proceed = false;
dialog.close();
}
dialog.show();
return process;
}

//Proceed or Stop execution on missing File.
function ProceedOrStop(fileName) {
var dialog = new Window("dialog", gScriptName);
dialog.orientation = "column";
dialog.alignChildren = "fill";

var panel = dialog.add("panel", undefined, "File Missing");
panel.orientation = "column";
panel.alignChildren = "right";

var group1 = panel.add("group");
group1.orientation = "row";
var selectStmt = group1.add("statictext", undefined, fileName);

var btnGroup = dialog.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "center";
var proceed = btnGroup.add("button", undefined, "Proceed");
var skip = btnGroup.add("button", undefined, "Skip");
var stop = btnGroup.add("button", undefined, "Stop");

proceed.onClick = function() {
dialog.close();
Proceed = true;
}
skip.onClick = function() {
dialog.close();
Proceed = false;
}
stop.onClick = function() {
dialog.close();
Proceed = false;
}
dialog.show();
}

function reLink(doc, inputFolder) {
//Get all files and folders
var fileName = doc.name;
doc = app.activeDocument;
var fileLinks = doc.links;
var info = ""
var missingFiles = "";
try {
// change reference links
for (var i = 0; i <= fileLinks.length - 1; i++) {
//getLinks and rename
if(fileLinks[i]!=-1){
for(j=0; j<inputFolder.length; j++){
var returnMsg = renameLinks(fileLinks[i], inputFolder[j]);
}
}
if (returnMsg == "fullstop") {
return 1;
}
missingFiles += returnMsg + "\n\n";
}
//doc.save();
if (missingFiles.replace(/(\r\n|\n|\r)/gm, "") != "") {
logFileName = fileName + "To" + relinkTo + ".txt"
info += "\nRefer to the log file on the Desktop for missing file info.";
fileInfo += "\n\nThe following files could not be found: \n" + missingFiles;
var logPath = File("~").fsName + "/Desktop/" + logFileName;
var logFile = new File(logPath);
logFile.open("w");
logFile.write(fileInfo);
logFile.close();
alert(info);
}
} catch (ex) {
alert("An error occurred. Please try again. Details below. \n" + ex.toString());
}
return fileLinks.length;
}

function renameLinks(refLink, inputFolder) {
var oldRefFilePath = refLink.filePath;
var fileExt = getFileExtension(oldRefFilePath);
var oldLinkFileName = getFileNameFromLink(getNameWithoutExtension(oldRefFilePath));
var newLinkFileName = oldLinkFileName;
 
var newRefFileFullPath = File(inputFolder).fsName + "/" + newLinkFileName + "." + fileExt;
try {
refLink.relink(new File(newRefFileFullPath));
refLink.update();
if(list[list.length-1]!=app.activeDocument.fullName){
list.push(app.activeDocument.fullName);
}
} catch (e) {
//alert(e.number);
if (e.number == 29446) {
ProceedOrStop(newLinkFileName);
if (Proceed) {
return newRefFileFullPath + "." + fileExt;
} else {
alert("Exiting process");
return "fullstop";
}
}
}
return "";
}

// create folders in a secure manner
function outputFinalsFolder(outPath) {
var folder1 = new Folder(outPath);
if (!folder1.exists) folder1.create();
}
// get Parent Folder
function getFileNameFromLink(path) {
return path.substr(path.lastIndexOf(":") + 1, path.length);
}
// get Parent Folder
function getParentFolder(path) {
return path.substr(0, path.lastIndexOf("/") + 1);
}
// get filename only without extension and Suffix
function getNameWithoutExtensionAndSuffix(fullName, suffix) {
//-- Locate the final position of the final _LR or _HR before the extension.
var finalLRPosition = fullName.lastIndexOf(suffix);
if (finalLRPosition > -1) {
return fullName.substr(0, finalLRPosition);
}
//-- implied else, return the original name because there is _LR.
return fullName;
}
// get file extension
function getFileExtension(file) {
return file.substr(file.lastIndexOf(".") + 1, file.length);
}
// get filename only without extension
function getNameWithoutExtension(fullName) {
//-- Locate the final position of the final . before the extension.
var finalDotPosition = fullName.lastIndexOf(".");
if (finalDotPosition > -1) {
return fullName.substr(0, finalDotPosition);
}
//-- implied else, return the original name because there is no dot.
return fullName;
}
TOPICS
Scripting

Views

93

Likes

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

Indesign script not relink with the sub folders

Participant ,
Sep 18, 2020

Copy link to clipboard

Copied

Hi Everyone,

The below code does not relink with the sub folders. It links only main folder images. Could anyone help this

// InDesign files to convert linking from Low Res to Hi Res and vice versa
// Notes
// For use in InDesign only
// e-mail: philip.tauro@hogarthww.com

/****************
* Name: Relinker_v2
* Desc: Relinking from low-res image to high or medium resolution and vice versa
* Dev: Barath Balaji
* E-Mail: barath.balaji@hogarthww.com
* Version 2.0.0
* 1. Added options to relink for Active Document, Multiple Files and Batch Process(Folder)
***************/

#target indesign
//page level variables
var fileList = [];
var refOutputPath = "";
var userInputFolder = "";
var regex = /.indd/gi;
var gScriptName = "Relink to Folders";
var myIcon = File ('~/Documents/Adobe Scripts/DO_NOT_DELETE_scriptResources/Icon/FolderSelectIcon_1.png');
var imFiles = []
var relinkTo = "Folder 1 files"
var Proceed = true;
var doc = null;
var files = new Array();
var option = null;
var sourceFolder = null;
var list = [];
var linksFolder = [];

//Folders and SubFolders Selections
var subFolderImageArray =[];

function getsubfolderfiles(folder) {
var filelist = folder;
for (var i = 0; i < filelist.length; i++) {
if (filelist[i] instanceof Folder) {
getsubfolderfiles(filelist[i]);
} else if (filelist[i] instanceof File) {
if (filelist[i].name.match(/\.(eps|ai|psd|jpg|JPEG|TIFF|tif|BMP|PNG|SVG|PDF|SWF|GIF|INDD)$/i) != null) {
subFolderImageArray.push(decodeURI(filelist[i].parent));
} //if
} //else if
} //for
return subFolderImageArray;
} //function

chooseFilesOption();

if (option == "folder") {
if (sourceFolder != null) {
fileType = "*.indd";
files = sourceFolder.getFiles(fileType);
if (files.length > 0) {
var nextStep = getRelinkFolder();
if(nextStep){
openFiles();
}
} else {
alert("No indesign files in the Folder");
exit(0);
}
} else {
alert("You did not select a Folder");
exit(0);
}
} else if (option == "multiple") {
m = $.os[0] == "M";
exts = ["indd"];
mf = function(f) {
return (f instanceof Folder) || RegExp("\.(" + exts.join("|") + ")$").test(f.name);
};
wf = "Indesign Files:*.indd";
filter = m ? mf : wf;
canSelectMultiple = true;
//requesting file selection
files = File.openDialog('Select the Indesign files to relink.', filter, canSelectMultiple);
if (files != null) {
var nextStep = getRelinkFolder();
if(nextStep){
openFiles();
}
} else {
alert("No indesign files selected");
exit(0);
}
} else if(option==null){
alert("You have cancelled.");
exit(0);
}
if(list.length>0){
alert("Operation Completed.")
}

function chooseFilesOption() {
var dialog = new Window("dialog", "Select an option to Relink Files");
dialog.orientation = "column";
dialog.alignChildren = "fill";

var btnGroup = dialog.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "center";
var folder = btnGroup.add("button", undefined, "Batch Process");
var multipleFiles = btnGroup.add("button", undefined, "Multiple Files");
 
folder.onClick = function() {
option = "folder";
sourceFolder = Folder.selectDialog('Select the folder with Indesign files to relink.');
dialog.close();
}
multipleFiles.onClick = function() {
option = "multiple";
dialog.close();
}
dialog.show();
}

function getRelinkFolder() {
//Choose Resolution
var nextStep = chooseOption();
//Choose links folder
if(nextStep==true){
relinkAssetFolder = imFiles; //3 Folders path
if (relinkAssetFolder != null) {
for(a=0; a<relinkAssetFolder.length; a++){
var allLinksFolder = relinkAssetFolder[a];
getsubfolderfiles(allLinksFolder)
linksFolder.push(allLinksFolder)
}
if (allLinksFolder.length > 0) {
return true;
} else {
alert("No files found in this folder.");
return false;
}
}else{
alert("You have cancelled");
return false;
}
}else{
alert("You did not choose an option");
exit(0);
}
return linksFolder
}
function openFiles(){
for (var i = 0; i < files.length; i++) {
app.open(files[i]);
var myDocument = app.activeDocument;
var myFileName = myDocument.filePath + "/" + myDocument.name;
myDocument.save(File(myFileName), true);
processFile();
}
}
function processFile() {
doc = app.activeDocument;
var linksCount = reLink(doc, linksFolder);
if (linksCount == 0) {
alert("No links found for this file.");
list.push(app.activeDocument.fullName);
} else {
try {
var filePath = decodeURI(doc.filePath)
//Save document to prevent save as dialog
var fullName = doc.name;
var finalDotPosition = fullName.lastIndexOf(".");
if (finalDotPosition > -1) {
var fileName = fullName.substr(0, finalDotPosition);
} else {
var fileName = fullName;
}
doc.save(new File(filePath + '/' + fileName + ".indd"));
doc.close();
} catch (e) {
alert(e);
}
}
}
//get User input.
function chooseOption() {
var process=false;
var dialog = new Window("dialog");
dialog.text = gScriptName;
dialog.orientation = "column";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16;

var panel1 = dialog.add("panel", undefined, undefined, {name: "panel1"});
panel1.text = "Locations";
panel1.orientation = "column";
panel1.alignChildren = ["left","top"];
panel1.spacing = 10;
panel1.margins = 10;

// GROUP1
// ======
var group1 = panel1.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = -5;

var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "Folder 1:";

var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}');
edittext1.preferredSize.width = 400;
edittext1.text = "";

var iconbutton1 = group1.add("iconbutton", undefined, myIcon, {name: "iconbutton1", style: "toolbutton"});

// GROUP2
// ======
var group2 = panel1.add("group", undefined, {name: "group2"});
group2.orientation = "row";
group2.alignChildren = ["left","center"];
group2.spacing = 10;
group2.margins = -5;

var statictext2 = group2.add("statictext", undefined, undefined, {name: "statictext2"});
statictext2.text = "Folder 2:";

var edittext2 = group2.add('edittext {properties: {name: "edittext2"}}');
edittext2.preferredSize.width = 400;
edittext1.text = "";

var iconbutton2 = group2.add("iconbutton", undefined, myIcon, {name: "iconbutton2", style: "toolbutton"});

// GROUP3
// ======
var group3 = panel1.add("group", undefined, {name: "group3"});
group3.orientation = "row";
group3.alignChildren = ["left","center"];
group3.spacing = 10;
group3.margins = -5;

var statictext3 = group3.add("statictext", undefined, undefined, {name: "statictext3"});
statictext3.text = "Folder 3:";

var edittext3 = group3.add('edittext {properties: {name: "edittext3"}}');
edittext3.preferredSize.width = 400;
edittext1.text = "";
 

var iconbutton3 = group3.add("iconbutton", undefined, myIcon, {name: "iconbutton3", style: "toolbutton"});

// GROUP4
// ======
var group4 = panel1.add("group", undefined, {name: "group4"});
group4.orientation = "row";
group4.alignChildren = ["center","center"];
group4.spacing = 10;
group4.margins = 0;
group4.alignment = ["center","top"];

var button1 = group4.add("button", undefined, undefined, {name: "button1"});
button1.text = "Cancel";

var button2 = group4.add("button", undefined, undefined, {name: "button2"});
button2.text = "OK";

iconbutton1.onClick = function() {
relinkTo = "Folder 1 files";
var ImageFolder = Folder.selectDialog("Please Select Images Folder")
var ImageFolderuri = ImageFolder;
var dcodeImageFolder1 = decodeURI(ImageFolderuri);
edittext1.text = dcodeImageFolder1
imFiles.push(dcodeImageFolder1)
process = true;
}
iconbutton2.onClick = function() {
relinkTo = "Folder 2 files";
var ImageFolder = Folder.selectDialog("Please Select Images Folder")
var ImageFolderuri = ImageFolder;
var dcodeImageFolder2 = decodeURI(ImageFolderuri);
edittext2.text = dcodeImageFolder2
imFiles.push(dcodeImageFolder2)
process = true;
}
iconbutton3.onClick = function() {
relinkTo = "Folder 3 files";
var ImageFolder = Folder.selectDialog("Please Select Images Folder")
var ImageFolderuri = ImageFolder;
var dcodeImageFolder3 = decodeURI(ImageFolderuri);
edittext3.text = dcodeImageFolder3
imFiles.push(dcodeImageFolder3)
process = true;
}
button1.onClick = function() {
proceed = true;
dialog.close();
}
button2.onClick = function() {
proceed = false;
dialog.close();
}
dialog.show();
return process;
}

//Proceed or Stop execution on missing File.
function ProceedOrStop(fileName) {
var dialog = new Window("dialog", gScriptName);
dialog.orientation = "column";
dialog.alignChildren = "fill";

var panel = dialog.add("panel", undefined, "File Missing");
panel.orientation = "column";
panel.alignChildren = "right";

var group1 = panel.add("group");
group1.orientation = "row";
var selectStmt = group1.add("statictext", undefined, fileName);

var btnGroup = dialog.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "center";
var proceed = btnGroup.add("button", undefined, "Proceed");
var skip = btnGroup.add("button", undefined, "Skip");
var stop = btnGroup.add("button", undefined, "Stop");

proceed.onClick = function() {
dialog.close();
Proceed = true;
}
skip.onClick = function() {
dialog.close();
Proceed = false;
}
stop.onClick = function() {
dialog.close();
Proceed = false;
}
dialog.show();
}

function reLink(doc, inputFolder) {
//Get all files and folders
var fileName = doc.name;
doc = app.activeDocument;
var fileLinks = doc.links;
var info = ""
var missingFiles = "";
try {
// change reference links
for (var i = 0; i <= fileLinks.length - 1; i++) {
//getLinks and rename
if(fileLinks[i]!=-1){
for(j=0; j<inputFolder.length; j++){
var returnMsg = renameLinks(fileLinks[i], inputFolder[j]);
}
}
if (returnMsg == "fullstop") {
return 1;
}
missingFiles += returnMsg + "\n\n";
}
//doc.save();
if (missingFiles.replace(/(\r\n|\n|\r)/gm, "") != "") {
logFileName = fileName + "To" + relinkTo + ".txt"
info += "\nRefer to the log file on the Desktop for missing file info.";
fileInfo += "\n\nThe following files could not be found: \n" + missingFiles;
var logPath = File("~").fsName + "/Desktop/" + logFileName;
var logFile = new File(logPath);
logFile.open("w");
logFile.write(fileInfo);
logFile.close();
alert(info);
}
} catch (ex) {
alert("An error occurred. Please try again. Details below. \n" + ex.toString());
}
return fileLinks.length;
}

function renameLinks(refLink, inputFolder) {
var oldRefFilePath = refLink.filePath;
var fileExt = getFileExtension(oldRefFilePath);
var oldLinkFileName = getFileNameFromLink(getNameWithoutExtension(oldRefFilePath));
var newLinkFileName = oldLinkFileName;
 
var newRefFileFullPath = File(inputFolder).fsName + "/" + newLinkFileName + "." + fileExt;
try {
refLink.relink(new File(newRefFileFullPath));
refLink.update();
if(list[list.length-1]!=app.activeDocument.fullName){
list.push(app.activeDocument.fullName);
}
} catch (e) {
//alert(e.number);
if (e.number == 29446) {
ProceedOrStop(newLinkFileName);
if (Proceed) {
return newRefFileFullPath + "." + fileExt;
} else {
alert("Exiting process");
return "fullstop";
}
}
}
return "";
}

// create folders in a secure manner
function outputFinalsFolder(outPath) {
var folder1 = new Folder(outPath);
if (!folder1.exists) folder1.create();
}
// get Parent Folder
function getFileNameFromLink(path) {
return path.substr(path.lastIndexOf(":") + 1, path.length);
}
// get Parent Folder
function getParentFolder(path) {
return path.substr(0, path.lastIndexOf("/") + 1);
}
// get filename only without extension and Suffix
function getNameWithoutExtensionAndSuffix(fullName, suffix) {
//-- Locate the final position of the final _LR or _HR before the extension.
var finalLRPosition = fullName.lastIndexOf(suffix);
if (finalLRPosition > -1) {
return fullName.substr(0, finalLRPosition);
}
//-- implied else, return the original name because there is _LR.
return fullName;
}
// get file extension
function getFileExtension(file) {
return file.substr(file.lastIndexOf(".") + 1, file.length);
}
// get filename only without extension
function getNameWithoutExtension(fullName) {
//-- Locate the final position of the final . before the extension.
var finalDotPosition = fullName.lastIndexOf(".");
if (finalDotPosition > -1) {
return fullName.substr(0, finalDotPosition);
}
//-- implied else, return the original name because there is no dot.
return fullName;
}
TOPICS
Scripting

Views

94

Likes

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
Sep 18, 2020 0
Adobe Community Professional ,
Sep 18, 2020

Copy link to clipboard

Copied

Update this function:

function renameLinks(refLink, inputFolder) {
    var oldRefFilePath = refLink.filePath;
    var fileExt = getFileExtension(oldRefFilePath);
    var oldLinkFileName = getFileNameFromLink(getNameWithoutExtension(oldRefFilePath));
    var newLinkFileName = oldLinkFileName;
    var newRefFileFullPath = File(inputFolder).fsName + "/" + newLinkFileName + "." + fileExt;
    if(!File(newRefFileFullPath).exists){
        var fileFound = false;
        var tempInputFolder = inputFolder;
        while(!fileFound){
            var allFiles = Folder(File(tempInputFolder).fsName.replace(/\\/g,'/')).getFiles();
            for(var i = 0; i < allFiles.length; i++){
                if(allFiles[i].constructor.name == "Folder"){
                    if(File(allFiles[i].fsName.replace(/\\/g,'/')++ "/" + newLinkFileName + "." + fileExt).exists){
                        var newRefFileFullPath = allFiles[i].fsName.replace(/\\/g,'/')++ "/" + newLinkFileName + "." + fileExt;
                        }
                    }
                }
            }
        }
    try {
        refLink.relink(new File(newRefFileFullPath));
        refLink.update();
        if(list[list.length-1]!=app.activeDocument.fullName){
            list.push(app.activeDocument.fullName);
            }
        }
    catch (e) {
        //alert(e.number);
        if (e.number == 29446) {
            ProceedOrStop(newLinkFileName);
            if (Proceed) {
                return newRefFileFullPath + "." + fileExt;
                }
            else {
                alert("Exiting process");
                return "fullstop";
                }
            }
        }
    return "";
    }

Best

Sunil

Likes

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
Reply
Loading...
Sep 18, 2020 1
Participant ,
Sep 18, 2020

Copy link to clipboard

Copied

Hi Sunil,

Thanks for your help really appreciate it. I have tried the function but it won't work for me. The relinking is not happened. Is there any issue in getsubfolder function?

Likes

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
Reply
Loading...
Sep 18, 2020 0
Adobe Community Professional ,
Sep 19, 2020

Copy link to clipboard

Copied

Try this method:

function renameLinks(refLink, inputFolder) {
    var oldRefFilePath = refLink.filePath;
    var fileExt = getFileExtension(oldRefFilePath);
    var oldLinkFileName = getFileNameFromLink(getNameWithoutExtension(oldRefFilePath));
    var newLinkFileName = oldLinkFileName;
    var newRefFileFullPath = File(inputFolder).fsName + "/" + newLinkFileName + "." + fileExt;
    if (!File(newRefFileFullPath).exists) {
        var fileFound = false;
        var tempInputFolder = inputFolder;
        while (!fileFound) {
            var allFiles = Folder(File(tempInputFolder).fsName.replace(/\\/g, "/")).getFiles();
            for (var i = 0; i < allFiles.length; i++) {
                if (allFiles[i].constructor.name == "Folder") {
                    if(File(allFiles[i].fsName.replace(/\\/g,'/')+ "/" + newLinkFileName + "." + fileExt).exists){
                        var newRefFileFullPath = allFiles[i].fsName.replace(/\\/g,'/')+ "/" + newLinkFileName + "." + fileExt;
                    }
                }
            }
        }
    }
    if (!File(newRefFileFullPath).exists) {
        try {
            refLink.relink(new File(newRefFileFullPath));
            refLink.update();
            if (list[list.length - 1] != app.activeDocument.fullName) {
                list.push(app.activeDocument.fullName);
                }
            }
        catch (e) {
            //alert(e.number);
            if (e.number == 29446) {
                ProceedOrStop(newLinkFileName);
                if (Proceed) {
                    return newRefFileFullPath + "." + fileExt;
                    }
                else {
                    alert("Exiting process");
                    return "fullstop";
                    }
                }
            }
        ProceedOrStop(newLinkFileName);
        if (Proceed) {
            return newRefFileFullPath + "." + fileExt;
            }
        else {
            alert("Exiting process");
            return "fullstop";
            }
        return "";
        }
    return "";
    }

Best

Sunil

Likes

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
Reply
Loading...
Sep 19, 2020 0
Participant ,
Sep 19, 2020

Copy link to clipboard

Copied

for (var i = 0; i < allFiles.length; i++) {

                if (allFiles[i].constructor.name == "Folder") {
                    if(File(allFiles[i].fsName.replace(/\\/g,'/')+ "/" + newLinkFileName + "." + fileExt).exists){

                        var newRefFileFullPath = allFiles[i].fsName.replace(/\\/g,'/')+ "/" + newLinkFileName + "." + fileExt;
//The above line code catch the single file in the sub folder
                    }
                }
            }
        }
    }
    if (!File(newRefFileFullPath).exists) {
//From here the script not work, The Indesign not responding
        try {
            refLink.relink(new File(newRefFileFullPath));
            refLink.update();
            if (list[list.length - 1] != app.activeDocument.fullName) {
                list.push(app.activeDocument.fullName);
                }
            }

Likes

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
Reply
Loading...
Sep 19, 2020 0
Adobe Community Professional ,
Sep 19, 2020

Copy link to clipboard

Copied

Did you ask the script's two authors? Their e-mail addresses are in the script.

Likes

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
Reply
Loading...
Sep 19, 2020 0
Participant ,
Sep 19, 2020

Copy link to clipboard

Copied

NO I cant able to get them.

Likes

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
Reply
Loading...
Sep 19, 2020 0