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

script for open and update all link with recursion

Explorer ,
Oct 11, 2022 Oct 11, 2022

Copy link to clipboard

Copied

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
};
TOPICS
Scripting

Views

163

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
Advisor ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

@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

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
Explorer ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

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

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
Explorer ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

it not work i have alert windows directly  Done updating and saving files

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
Explorer ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

can you check if all I and J are at good place?

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
Explorer ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

this code work but error at line 61/62

 

// 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

alert("nombre de fichiers dans le dossier et sous dossier : " + fileList.length)

// 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")) {
  // ce if vérifie si l'élement "fileList[i]" est bien un fichier et si le nom de fichier est "A.indd" pas ce qu'on veut encore
  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;
	alert("nombre de liens dans ce fichier : " + fileList.length)
	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:
		alert("Lien numéro " + j + " : type de fichier " + internal_link[j].linkType + " alors que seul les indd sont mis à jour")
		if (internal_link[j].linkType == 'InDesign Format Name') { // Je ne veux ouvrir que les fichier indd
			alert("ouverture d'un sous lien. entrée dans une boucle récursive")
			alert("ouverture ddu sous lien : " + internal_link[j].File)
			OpenAndUpdate(internal_link[j].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
};

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
Explorer ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

we are very close, we hare error line 56, when it open the indd link file, and this indd file have 0 link

// 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

alert("nombre de fichiers dans le dossier et sous dossier : " + fileList.length)

// 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")) {
  // ce if vérifie si l'élement "fileList[i]" est bien un fichier et si le nom de fichier est "A.indd" pas ce qu'on veut encore
  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;
	alert("ouverture d'un sous lien : " +  doc)
	app.open(doc);
	internal_link = app.activeDocument.links;
	var nb_link = 0
	try {
  		nb_link = internal_link.length;
	} catch (error) {}
	alert(internal_link)
	alert("nombre de liens dans ce fichier : " + internal_link.length + "nombre de link : " + nb_link)
	if (nb_link > 0) {
	for(var j = 0; j < nb_link; 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:
		alert("Lien numéro " + j + " : type de fichier " + internal_link[j].linkType + " alors que seul les indd sont mis à jour")
		if (internal_link[j].linkType == 'InDesign Format Name') { // Je ne veux ouvrir que les fichier indd
			alert("ouverture d'un sous lien. entrée dans une boucle récursive")
			alert("ouverture ddu sous lien : " + internal_link[j].filePath)
			OpenAndUpdate(internal_link[j].filePath) // 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
};

 

 

error 45 object invalid line 45 

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
Explorer ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

LATEST

and other thing, it detect 127 link ito my file, but in reality there is 9 "top link" if we open this 9 link, we can update all 127 link

 

how to differenciate  the single link and the group link?

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