Skip to main content
Inspiring
October 26, 2022
Question

how to verify doc.links.everyItem().update() before export

  • October 26, 2022
  • 2 replies
  • 537 views

hi it s possible to very if all link are updated before start export?

 

because i feel sometime export start before doc.links.everyItem().update() is finish...

 

picture are sometime low def into the pdf file

This topic has been closed for replies.

2 replies

Jumpenjax
Community Expert
Community Expert
October 27, 2022

Use the package feature, it will let you know of any potential problems with the file and create a pdf.

Lee- Graphic Designer, Print Specialist, Photographer
Inspiring
November 1, 2022

hi, this is my script

try {
  var updated_list = [];
  var defaultFolder =
    "/M/infographie/Dossier Phykidis/base de donnée produit/pkd";

  function findAll(f, flist) {
    var f = Folder(f).getFiles("*.*");
    for (var i = 0; i < f.length; i++) {
      if (f[i] instanceof Folder && f[i].name[0] != ".") findAll(f[i], flist);
      else {
        if (
          f[i].displayName == "étiquette finie.indd" ||
          f[i].displayName == "catalogue - fiche tech.indd" ||
          f[i].displayName == "catalogue.indd"
        )
          flist.push(f[i]);
      }
    }
  }

  var myFolder = Folder(defaultFolder).selectDlg(
    "Select the folder containing InDesign Files"
  );
  if (!myFolder) exit();

  var f = [];
  findAll(myFolder, f);
  var doc;
  app.scriptPreferences.userInteractionLevel =
    UserInteractionLevels.neverInteract;

  //Opens each ID file and runs the
  for (var i = 0; i < f.length; i++) {
    doc = app.open(File(f[i]));
    //chkbox(doc);
    OpenAndUpdate(doc);
  }

  alert("fini !");
} catch (err) {
  alert(err.line + " => " + err.message);
}

/**
 * Updates a document’s links
 * @ param the document to update
 * @ return value 0
 *
 */
function OpenAndUpdate(doc) {
  app.scriptPreferences.userInteractionLevel =
    UserInteractionLevels.neverInteract;
  //$.writeln("ouverture d'un sous lien : " +  doc);
  internal_link = doc.links;
  var nb_link = 0;
  try {
    nb_link = internal_link.length;
  } catch (error) {}
  //$.writeln(internal_link);
  //$.writeln("nombre de liens dans ce fichier : " + internal_link.length + "nombre de link : " + nb_link ) //+ internal_link[k]);
  if (nb_link > 0) {
    // doc.links.everyItem().update();
    // doc.save(); //idem
    for (var k = 0; k < nb_link; k++) {
      // internal_link = doc.links;
      if (k > 0) {
      }
      // On vérifie que le lien est OK :
      if (internal_link[k].status == LinkStatus.LINK_MISSING) {
        var filePath = File.openDialog(internal_link[k].filePath);
        //alert(filePath);
        internal_link[k].relink(filePath);
      } else if (
        !internal_link[k].linkType == "InDesign Format Name" ||
        internal_link[k].name == "Assemblage fond couleur + filigrane.indd"
      ) {
        try {
          internal_link[k].relink(internal_link[k].linkResourceURI);
        }
        catch (err) {
          alert(err.message);
        }
      } else {
        internal_link[k].update();
      }
      // Si le lien est ok. On l'ouvre pour vérifier ses propres lien. La recursivité a lieu ici:
      //$.writeln("Lien numéro " + k + " : type de fichier " + internal_link[k].linkType + " alors que seul les indd sont mis à jour");
      if (internal_link[k].linkType == "InDesign Format Name") {
        // Je ne veux ouvrir que les fichier indd
        //$.writeln("ouverture d'un sous lien. entrée dans une boucle récursive");
        //alert(updated_list);
        if (!isinlist(updated_list, internal_link[k].filePath)) {
          newdoc = app.open(internal_link[k].filePath);
          OpenAndUpdate(newdoc);
          doc = app.activeDocument;
          internal_link = doc.links; // quand tu reviens de la boucle, recharge les liens
          updated_list.push(internal_link[k].filePath);
        } else {
          internal_link[k].update();
        }
      }
    }
  }
  if (doc.links.length > 0) {
    app.scriptPreferences.userInteractionLevel =
      UserInteractionLevels.neverInteract;
    doc.links.everyItem().update();
    try {
      doc.save();
    } catch (err) {
      $.writeln("i = " + i + "  document.name = " + document.name);
      $.writeln(err.line + " =>" + err.message);
      alert(err.line + " => " + err.message);
    }
    if (
      doc.name == "étiquette finie.indd" ||
      doc.name == "cataloguephykidis.indd"
    ) {
      exporttopdf();
    }
    try {
      doc.save(); 
      doc.close();
    } catch (err) {
      $.writeln("i = " + i + "  document.name = " + document.name);
      $.writeln(err.line + " =>" + err.message);
      alert(err.line + " => " + err.message);
    }
  } else {
    doc.close();
  }
  return 0; // fin de la fonction. Elle ne renvoie rien normalement
}

function isinlist(list, element) {
  var flag = false;
  for (var l = 0; l < list.length; l++) {
    if (list[l] == element) {
      flag = true;
    }
  }
  return flag;
}

function exporttopdf() {
  var myDocument = app.activeDocument; //Or set it with or replace it with the document object you might have
  //if (myResult == true){
  for (var myCounter = 0; myCounter < myDocument.spreads.length; myCounter++) {
    myDocument.spreads.item(myCounter).pageTransitionType =
      PageTransitionTypeOptions.wipeTransition;
    myDocument.spreads.item(myCounter).pageTransitionDirection =
      PageTransitionDirectionOptions.down;
    myDocument.spreads.item(myCounter).pageTransitionDuration =
      PageTransitionDurationOptions.medium;
  }
  app.interactivePDFExportPreferences.flipPages = true;
  app.interactivePDFExportPreferences.flipPagesSpeed = 5;
  app.interactivePDFExportPreferences.openInFullScreen = false;
  app.interactivePDFExportPreferences.interactivePDFInteractiveElementsOption =
    InteractivePDFInteractiveElementsOptions.includeAllMedia;
  var docName = myDocument.name.replace(/\.[^\.]+$/, "");
  var outputFolder = Folder(myDocument.filePath);
  var newFile = File(decodeURI(outputFolder) + "/" + docName + ".pdf");
  myDocument.exportFile(ExportFormat.interactivePDF, newFile, false); //Change the file path and file name as needed
  // }
}

function chkbox(doc) {
  var myDialog = app.dialogs.add({
    name: "Export to PDF",
  });

  with (myDialog.dialogColumns.add()) {
    var myCheckBoxField = checkboxControls.add();

    myCheckBoxField.staticLabel = "PDF";
  }

  var myResult = myDialog.show();
}

 

line 119 i call export pdf function, but before line 107,i  call update every item

 

and with the result, i feel export pdf not wait update every item is finish

because, i have sometime file pdf with blank page, or layer not update

when i return check main file, all is ok

Legend
October 26, 2022

Hello @laurence roussel12011930 

 

Give this a try...you may want to modify it a bit to fit in you script.

 

var doc = app.documents[0];

function checkIfOnPage(mItem){
    
    var _OldZeroPoint = app.activeDocument.zeroPoint;     
    app.activeDocument.zeroPoint = [0,0];     
    var _Width =app.activeDocument.documentPreferences.pageWidth;     
    var _Height=app.activeDocument.documentPreferences.pageHeight;     
    var _Bounds = mItem.geometricBounds;
    if ((_Bounds[3] < 0)  || (_Bounds[1] > _Width) || (_Bounds[0] > _Height) || (_Bounds[2] < 0)){
        return false     
    }else{   
        return true 
    }
} 

var mLinks = doc.links; 
for(var i = 0; i < mLinks.length; i++){ 
if ((mLinks[i].status == LinkStatus.LINK_MISSING) && checkIfOnPage(mLinks[i].parent.parent) && mLinks[i].parent.parent.itemLayer.visible == true){ 
alert("Error!\nThis document contains links to files that are missing, use the links panel to fix the problem(s) and try again!");  
exit();
}
else if ((mLinks[i].status == LinkStatus.LINK_OUT_OF_DATE) && checkIfOnPage(mLinks[i].parent.parent) && mLinks[i].parent.parent.itemLayer.visible == true){
mLinks[i].update();
       }
  }

 

 

@laurence roussel12011930, I just edited the code to better fit your needs, so you might want to try again.

Regards,

Mike

Inspiring
October 27, 2022

thanks very much i will try it asap

 

are you able to script with photoshop? because i need to resolve a problem, but i have no solution

Robert at ID-Tasker
Legend
October 27, 2022

What would you like to automate in Photoshop?