Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Simple relink script help!

Return to Member Forum

  • Author
    Posts
    • #60757
      ivan_
      Member

      Hello,

      I am relatively new to scripting. I have managed to write one script already to update and append some xml text but have become quite stuck with this one.

      I am trying to run a simple script that is similar to the one written by the mighty Kasyan Servetsky:

      https://forums.adobe.com/thread…..9?tstart=0

      Though mine is much simpler.

      I need to replace the file path of all the graphic links in an indesign document from existing directory A to directory B.

      ============

      #target indesign

      var doc = app.activeDocument;
      var myLinks = doc.links;

      myNewPath = 'c:tempnewproject'

      myOldPath = myLinks.filePath

      for (var i = links.length-1; i >= 0; i–) {
      var myLink = myLinks[i]

      var myLink.filePath.replace(myOldPath, myNewPath);

      myLink.update
      }

      =========

      I have tried lots of other ways but cannot seem to get anything to work.

      Any help would be much appreciated.

      Regards,

      Ivan

    • #60776

      Hi Ivan,

      It doesn't work because you made too many mistakes in your code.

      #target indesign
      var i, link, newFile,
      doc = app.activeDocument,
      links = doc.links,
      newPath = “/c/temp/newproject/”;

      for (i = links.length-1; i >= 0; i–) {
      link = links[i];
      newFile = new File(newPath + link.name);
      if (newFile.exists) link.relink(newFile);
      try {
      link.update();
      }
      catch(err) {}
      }

      BTW if you want to use Windows path names, you should escape backslashes:

      newPath = 'c:tempnewproject';

      I prefer URI path names:

      newPath = “/c/temp/newproject/”;

      Hope this helps.

      Kasyan

      P.S. I don't hang out on Adobe forums any more because they blocked access from my country. If you have more questions to me about scripting links (it's quite a tricky business), feel free to ask them here.

    • #60778

      In my previous post, forum software ate up double backslashes.
      It should be:

      newPath = 'c: backslash backslash temp backslash backslash newproject backslash backslash ';

    • #60779

      Kasyan, it's okay to use forward slashes in an InDesign script – even under Windows.

    • #60781

      Yes, it is. Forward slashes work as well:

      newPath = “c:/temp/newproject/”;

      What I mean is that backslash is the escape character, so we must use a double backslash to indicate it. In the original post I see single backslashes:

      myNewPath = 'c:tempnewproject'

      Probably, initallly they were there, but the forum software removed them.

    • #60782

      Oops! It removed them again. I guess, on this forum, we should type two backslashes for one, and four for two.

    • #60787
      ivan_
      Member

      Thank you Kasyan, your help is greatly appreciated.

      The directory slashes seem to happen when copying and pasting from extendscript to the forum, it also seems to change the script 'i–'.

      Would it be possible to talk me through the script and its actions. This is as how I understand it:

      #target indesign = targets indesign application
      var i, link, newFile, = specifies variables


      doc = app.activeDocument, = specifies the active document as the file name
      links = doc.links, = name of all the links in the file
      newPath = “/c/temp/newproject/”; = new path string

      for (i = links.length-1; i >= 0; i–) { = loops through each link in links from 0 to infinity
      link = links[i]; = each link in the loop is a seperate string
      newFile = new File(newPath + link.name); =concatenates path and file name to create new file
      if (newFile.exists) link.relink(newFile); = loops throguh and relinks files
      try {
      link.update(); = update links
      }
      catch(err) {}
      }

      Was just wondering why you have to use 'try { }' and 'cath(err){}'.

      Regards,

      Iv

    • #60788
      ivan_
      Member

      Ah right just looked up catch(err) am I correct in thinking it supresses any javascript errors?

      The script is running well, though I seem to have a slight glitch.

      Currently the indesign documents relinks embedded linked files to a new file location. The indesign document consists of two links to multi page pdf's with 4 instances each across the document (8 links in total).

      Some times the script runs and only two links remain embedded whilst another time the script may leave four links embedded. Is this something to do with a time out of the script? It seems to execute for several seconds then gives up re-linking.

      Would it be possible to add a function that loops the link.update(); until all links are re-linked?

      iv

    • #60792

      #target indesign
      Main();

      function Main() {
      // the following 4 lines declare variables
      // usually I declare all vars at the beginning of the function
      // when you declare a variable with var, you limit its scope to the function
      // (making them local),
      // otherwise the scope is global which may cause you problems
      // a rule-of-thumb is to use global vars only you really need them
      var i, link, newFile,
      doc = app.activeDocument, // set variable doc to the front most document
      links = doc.links, // set variable links to collection of all links
      newPath = “/c/temp/newproject/”; // path to the folder
      for (i = links.length-1; i >= 0; i–) { // loop through all links backwards
      // from the last to the first item
      // it's very important when you modify or remove items from collection
      // to process them back to front, never use in such cases direct loop like so:
      // for (i = 0; i < links.length; i++) {
      link = links[i]; // set variable link to the current link
      newFile = new File(newPath + link.name); // create file object
      if (newFile.exists) link.relink(newFile); // relink only if the file exists
      try { // if memory serves me right, unlike CS3,
      // CS4 doesn't need update() command after using relink()
      // if you use it , it would cause an error
      // I use try-catch block to make the script compatible with CS3 and above
      // In other words, if an error occurs here, skip it over and go on
      link.update();
      }
      catch(err) {}
      }
      }

      The links placed more than once are far more difficult to deal with — in short, they should be processed separately.

      I can give you a few examples of how I do this:

      Resize images for Power Switch (see the SetLink100percent function — this is the latest algorithm I made)

      Relink Images and Apply “Rich Black” Color for Power Switch (see the RelinkLink function)

      Resize images

    • #60883
      Bill Pitts
      Member

      Hi Kasyan,

      I build a small community newspaper (24 pages) that appears twice a month and I've run across the same problem with my links. Obviously, a lot of the paper's graphics and the majority of the approximately 60 ads are the same from issue to issue, and I find myself putting a lot of time into assuring that the links are current — time that could be better spent with layout.

      As I can't afford an upgrade to my computer (Mac G4 from 2001) or to my software, will this script work with InDesign CS2?

      Thanks,

      Bill

    • #60885

      Hi Bill,

      I just checked the InDesign CS2 Scripting Reference and yes, theoretically it should work. However, I can't check this in practice — don't have CS2 any more.

      Regards,
      Kasyan

Viewing 10 reply threads
  • You must be logged in to reply to this topic.
Forum Ads