Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vérifie si les images d'un dossier choisis sont corrompues Check if images in a chosen folder are corrupted
/*
Vérifie si les images d'un dossier choisis sont corrompues
Check if images in a chosen folder are corrupted
par Ben Cloutier
Inspiré du script:
ImageCatalog.jsx, An InDesign C4 JavaScript, 2.0.0 5-December-2007
*/
nomDuScript = "Verificateur d'images"
dateRev = "rev.6.08.2012"
myId = "ben@bencloutier.com";
badImages = [];
myExtensions = [".jpg", ".jpeg", ".eps", ".pdf", ".tif", ".tiff", ".gif", ".psd", ".ai", ".png"];
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
app.scriptPreferences.enableRedraw =0;
var myFolder = Folder.selectDialog("Indiquez le dossier images", "");
if(myFolder != null){
var myDocument = app.documents.add();
var myPage = myDocument.pages.item(0);
if(File.fs == "Macintosh"){
myFilteredFiles = myMacOSFileFilter(myFolder);
}
else{
myFilteredFiles = myWinOSFileFilter(myFolder);
}
getFileNames(myFilteredFiles);
app.scriptPreferences.enableRedraw =1;
//Si des images corrompues sont trouvées, il crée un bloc texte et insère la liste
if (badImages.length > 0){
var myTextFrame = myPage.textFrames.add();
myTextFrame.geometricBounds = [36 + "pt",36 + "pt",756 + "pt",576 + "pt"];
if(badImages.length == 1){
alert("Une image corrompue a été trouvée.");
mess = badImages.length + " seule image est corrompue:\r\r\t"
}else{
alert(badImages.length + " images corrompues ont étés trouvées.");
mess = badImages.length + " images sont corrompues:\r\r\t"
}
myTextFrame.contents = mess + badImages.join("\r\t") +
"\r\r----------------------------------------------------\r" +
"Ceci n'est qu'un rapport préliminaire. Il se peut que d'autres images corrompue sans que InDesign ne les détectes.\r" +
"CheckImages-benc (" + dateRev + ")";
myTextFrame.textFramePreferences.verticalJustification = VerticalJustification.topAlign;
var myText = myTextFrame.parentStory.paragraphs.item(0);
myText.justification = Justification.leftAlign;
//Remplace les «%20» par des espaces
setFindGREPPrefs();
app.findGrepPreferences.findWhat = "%20";
app.changeGrepPreferences.changeTo = " ";
changeGREPText();
//Efface le path (ne garde que le nom de l'image)
setFindGREPPrefs();
app.findGrepPreferences.findWhat = "\\t.+\\/";
app.changeGrepPreferences.changeTo = "~m";
changeGREPText();
}else{
alert("Toutes les images semblent OK!");
myDocument.close(SaveOptions.no);
}
}
function getFileNames(){
for(i=0;i<myFilteredFiles.length;i++){
var myRectangle = myPage.rectangles.add();
try{
//Essai de placer l'image dans InDesign
myRectangle.place(File(myFilteredFiles[i]));
}catch(e){
//S'il ne réussi pas, il envoi le nom dans une liste
var tmp = myFilteredFiles[i].toString();
badImages.push (tmp);
}
myRectangle.remove();
}// for i
}// getFileNames ----------|
//Windows version of the file filter.
function myWinOSFileFilter(myFolder){
var myFiles = new Array;
var myFilteredFiles = new Array;
for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++){
myExtension = myExtensions[myExtensionCounter];
myFiles = myFolder.getFiles("*"+ myExtension);
if(myFiles.length != 0){
for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++){
myFilteredFiles.push(myFiles[myFileCounter]);
}
}
}
return myFilteredFiles;
}
function myMacOSFileFilter(myFolder){
var myFilteredFiles = myFolder.getFiles(myFileFilter);
return myFilteredFiles;
}
//Mac OS version of file filter
//Have to provide a separate version because not all Mac OS users use file extensions
//and/or file extensions are sometimes hidden by the Finder.
function myFileFilter(myFile){
var myFileType = myFile.type;
switch (myFileType){
case "JPEG":
case "EPSF":
case "PICT":
case "TIFF":
case "8BPS":
case "GIFf":
case "PDF ":
return true;
break;
default:
for(var myCounter = 0; myCounter<myExtensions.length; myCounter++){
var myExtension = myExtensions[myCounter];
if(myFile.name.indexOf(myExtension)>-1){
return true;
break;
}
}
}
return false;
}
function setFindGREPPrefs(){
//Clear the find/change grep preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
}
function changeGREPText(){
var foundTexts = myDocument.findGrep();
// Fais le tour de tour les résultats et applique les changement demandés
if(foundTexts.length != 0) {
myDocument.changeGrep();
}
//Clear the find/change preferences after the search.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment