Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
À partir d'un ou plusieurs documents InDesign, récupérez tous les textes d'un layer commun et les exportez-les dans un document texte. Ce script est optimisé pour les Mac. Changer l'encodage pour Windows au besoin à la ligne 60.. This script lets you chose multiple InDesign documents, chose a layer (this layer must have the same name through all…
//Script de récupération des textes sur un layer choisis dans plusieurs documents
//Script to collect text from a selected layer in one or more InDesign document
//bencloutier.com
//(Thank to -hans- http://forums.adobe.com/message/5248830#5248830)
var dateRev = "rev.20.12.2013";
var myId = "ben@bencloutier.com";
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
var myFiles = File.openDialog ("Choisir les documents InDesign\n" + myId + " (" + dateRev + ")" , ["txt"], true );
if(myFiles == null){
exit();
}
app.scriptPreferences.enableRedraw = false;
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
var myLayerName = "";
var myDocumentNumber = "";
var myDatas = [];
var myFilesSorted = myFiles.sort();
var myFinalDatas = "";
for(i=0;i<myFilesSorted.length;i++){
app.open(File(myFilesSorted[i]));
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
app.activeDocument.zeroPoint = [0,0];
var myDocument = app.documents.item(0);
var myPages = myDocument.pages;
var myElements = myDocument.allPageItems;
var myLayers = app.activeDocument.layers;
var myDocumentNumber = myDocument.name.match(/^\d{5}/);
if (myDocumentNumber == null){
var myDocumentNumber = "Mon"; ///AJOUTER LA DATE AU NOM SI PAS DE # DE DOSSIER OU QQCHOSE COMME ÇA
}
var myTextFileName = myDocumentNumber + "_Texts.txt";
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if(myLayerName == ""){
getLayerName();
}
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
for(j=0;j<myElements.length;j++){
if(myElements[j].itemLayer.name == myLayerName){
try{
pageName = myElements[j].parent.constructor.name;
if(myElements[j].contents != "" && pageName.toString() == "Page" || pageName.toString() == "Spread"){
checkMyNum(myElements[j].geometricBounds[0]);//ajoute un zéro au chiffre pour le triage
// var myPageItems:PageItems = app.documents.item(0).pages.item(0).pageItems;
myDatas.push(myElements[j].parentPage.name + myNum + "." + Math.round(myElements[j].geometricBounds[1]) + " //" + myElements[j].contents + "\n");
// myDatas.push(app.activeWindow.activePage.name + Math.round(myElements[j].geometricBounds[0]) + "." + Math.round(myElements[j].geometricBounds[1]) + " //" + myElements[j].contents + "\n");
}
}
catch(e){}
}
}
// alert("myDatas: " + myDatas.join("\n"));
myDatasSort = myDatas.sort(function(a,b){return a-b});
myDatasString = myDatasSort.join("\n");
// myStoriesClean = myDatasString;
myStoriesClean = myDatasString.replace(/.*?\/\//gi, "");
myFinalDatas += "**********************************\n" + myDocument.name + "\n**********************************\n\n" + myStoriesClean + "\n----------\n\n";
myDatas = [];
if(i == (myFilesSorted.length - 1)){
exportText ();
myDocument.close(SaveOptions.no);
reEnable();
alert("Terminé.\nLe document «" + myTextFileName + "» se trouve sur votre bureau.\n\n" + myId + " (" + dateRev + ")");
exit();
}else{
myDocument.close(SaveOptions.no);
}
}
function checkMyNum(){
myNum = Math.round(myElements[j].geometricBounds[0]).toString();
switch(myNum.length){
case 1:
myNum = "000" + myNum;
break;
case 2:
myNum = "00" + myNum;
break;
case 3:
myNum = "0" + myNum;
break;
}
/*
if (myNum.length < 3){
myNum = "0" + myNum;
}
*/
// alert(myNum + " (" + myNum.length + ")");
return myNum;
}
function exportText(){
var myTxtFile = File("~/Desktop/" + myTextFileName);
myTxtFile.encoding = "UTF-8";
myTxtFile.lineFeed = "Macintosh"; //One of the values "Windows", "Macintosh", or "Unix".
myTxtFile.open("w");
myTxtFile.write(myFinalDatas);
myTxtFile.close();
}
function getLayerName(){
myLayerList = [];
for(u = 0;u < myLayers.length;u++){
myLayerList.push(myLayers[u].name);
}
var myTmpTextName = myTextFileName;
var myDialog = app.dialogs.add({name:"Extraction des textes" , canCancel:true});
with(myDialog.dialogColumns.add()){
with(dialogRows.add()){
staticTexts.add({staticLabel:"Choisir le layer texte commun:"});
}
with(dialogRows.add()){
var myLayersListMenu = dropdowns.add({stringList:myLayerList, selectedIndex:0});
}
with(dialogRows.add()){
staticTexts.add({staticLabel:myFilesSorted.length + " documents sélectionnés"});
}
with(borderPanels.add()){
with(dialogColumns.add()){
//The following line shows how to set a property as you create an object.
staticTexts.add({staticLabel:"Nom du fichier texte:"});
}
with(dialogColumns.add()){
//The following line shows how to set multiple properties
//as you create an object.
var myTextEditField = textEditboxes.add({editContents:myTmpTextName.replace(/\.txt/, ""), minWidth:180});
}
}
}
var myResult = myDialog.show();
if(myResult == false){
reEnable();
exit();
}
else{
myTextFileName = myTextEditField.editContents + ".txt";
myLayerName = myLayerList[myLayersListMenu.selectedIndex];
}
myDialog.destroy();
}
function reEnable(){
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
app.scriptPreferences.enableRedraw = true;
}
function filterFiles(file){
if(file.name.match(/\.indd$/)){return true}
return false
}
/*
Révisions
27.08.2013
- L'origine est maintenant mise à zéro pour que les textes soient toujours dans le bon ordre
- Il est possible de changer le nom du fichier texte qui sera créé
20.12.2013
- L'ordre des textes est maintenant à la page pour les spreads
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment