Skip to main content
Inspiring
October 12, 2022
Question

script for open and update all link with recursion

  • October 12, 2022
  • 7 replies
  • 272 views

hi every body we are trying to do this script i have try it on file with lot link inside, each link . indd need to be update

 

A-> contain B LINK indd files -> contain C link indd file -contain D link docx file.

 

if i update my doc x file, i want update A with script

 

will open B,C

 

update and save and close C, after B, after A

 

// extendScript

var defaultFolder = "/M/infographie/Dossier Phykidis/base de donnée produit/pkd/GAMMES/neutre/403 E"
var topFolder = Folder(defaultFolder).selectDlg("Select the top level folder.");
if (!topFolder) exit();
var fileandfolderArray = scanSubFolders(topFolder,/\.(indd)$/i);

var fileList = fileandfolderArray[0];
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll


// ici on boucle sur tous les fichiers. C'est normalement que les fichiers etiquette
for (var i = 0; i < fileList.length; i++) {
	OpenAndUpdate(fileList[i])
}
alert("Done updating and saving files!")
  

function scanSubFolders(tFolder, scanMask){
    var subFolders = new Array();
    var allFiles = new Array();
    subFolders[0] = tFolder;
    for (var j = 0; j < subFolders.length; j++){    
        var procFiles = subFolders[j].getFiles();
        for (var i = 0; i< procFiles.length; i++){
            if (procFiles[i] instanceof File ){
                if(scanMask == undefined) allFiles.push(procFiles[i]);
                if (procFiles[i].fullName.search(scanMask) != -1) allFiles.push(procFiles[i]);
        }else if (procFiles[i] instanceof Folder){
            subFolders.push(procFiles[i]);
            scanSubFolders(procFiles[i], scanMask);
         }
      }
   }
   return [allFiles,subFolders];
};

function OpenAndUpdate(doc){
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
	app.open(doc);
	internal_link = app.activeDocument.links;

	for(var j = 0; j < internal_link.length; j++){
		// On vérifie que le lien est OK :
		if (internal_link[j].status == LinkStatus.LINK_MISSING){
			var folderPath = (Folder.selectDialog(internal_link[j].filePath))
			var imglist = Folder(folderPath).getFiles(internal_link[j].name);
			if (imglist.length == 1){
				internal_link[j].relink(imglist[0]);
			}
		}

		// Si le lien est ok. On l'ouvre pour vérifier ses propres lien. La recursivité a lieu ici:
		if (internal_link[i].linkType == 'indd') { // Je ne veux ouvrir que les fichier indd
			OpenAndUpdate(internal_link[i].File) // pas sur. peut etre Filepath à la place de File. cf la doc: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Link.html
		}
	}
	app.activeDocument.links.everyItem().update();
	app.activeDocument.save();
	app.activeDocument.close();
   return 0; // fin de la fonction. Elle ne renvoie rien normalement
};
This topic is closed to new replies. Start a new post to keep the conversation going.

7 replies

Legend
October 12, 2022

@laurence roussel12011930,

Give this a try...It's not clear to me on the on the order you wanted, so I took my best guess.

Note: I have not fully tested with your function OpenAndUpdate(doc).

 

// extendScript

var defaultFolder = "/M/infographie/Dossier Phykidis/base de donnée produit/pkd/GAMMES/neutre/403 E"
var topFolder = Folder(defaultFolder).selectDlg("Select the top level folder.");
if (!topFolder) exit();
var fileandfolderArray = scanSubFolders(topFolder,/\.(indd)$/i);

var fileList = fileandfolderArray[0];
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll


// ici on boucle sur tous les fichiers. C'est normalement que les fichiers etiquette
for (var i = 0; i < fileList.length; i++) {
  if (fileList[i] instanceof File && fileList[i].name.match("\\b" + "A.indd" + "\\b")) {
  OpenAndUpdate(fileList[i])
  }
  if (fileList[i] instanceof File && fileList[i].name.match("\\b" + "B.indd" + "\\b") && app.documents[0].name.match("\\b" + "A.indd" + "\\b")) {
  OpenAndUpdate(fileList[i])
  }
  if (fileList[i] instanceof File && fileList[i].name.match("\\b" + "C.indd" + "\\b") && app.documents[0].name.match("\\b" + "B.indd" + "\\b")) {
  OpenAndUpdate(fileList[i])
  } 
}
app.documents.everyItem().save();
app.documents.everyItem().close();
alert("Done updating and saving files!")
  

function scanSubFolders(tFolder, scanMask){
    var subFolders = new Array();
    var allFiles = new Array();
    subFolders[0] = tFolder;
    for (var j = 0; j < subFolders.length; j++){    
        var procFiles = subFolders[j].getFiles();
        for (var i = 0; i< procFiles.length; i++){
            if (procFiles[i] instanceof File ){
                if(scanMask == undefined) allFiles.push(procFiles[i]);
                if (procFiles[i].fullName.search(scanMask) != -1) allFiles.push(procFiles[i]);
        }else if (procFiles[i] instanceof Folder){
            subFolders.push(procFiles[i]);
            scanSubFolders(procFiles[i], scanMask);
         }
      }
   }
   return [allFiles,subFolders];
};

function OpenAndUpdate(doc){
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
	app.open(doc);
	internal_link = app.activeDocument.links;

	for(var j = 0; j < internal_link.length; j++){
		// On vérifie que le lien est OK :
		if (internal_link[j].status == LinkStatus.LINK_MISSING){
			var folderPath = (Folder.selectDialog(internal_link[j].filePath))
			var imglist = Folder(folderPath).getFiles(internal_link[j].name);
			if (imglist.length == 1){
				internal_link[j].relink(imglist[0]);
			}
		}

		// Si le lien est ok. On l'ouvre pour vérifier ses propres lien. La recursivité a lieu ici:
		if (internal_link[i].linkType == 'indd') { // Je ne veux ouvrir que les fichier indd
			OpenAndUpdate(internal_link[i].File) // pas sur. peut etre Filepath à la place de File. cf la doc: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Link.html
		}
	}
	app.activeDocument.links.everyItem().update();
	// app.activeDocument.save();
	// app.activeDocument.close();
   return 0; // fin de la fonction. Elle ne renvoie rien normalement
};

 

Regards,

Mike

Inspiring
October 12, 2022

cool i will test, you are fast

 

idéa is open only "top" file, open each link inside, and open each link inside the new document

 

apfter update all, save and close, and finish for update the top file ,update and save and close