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

Betcha can't Batch Process.

Return to Member Forum

  • Author
    Posts
    • #57565
      Duncan Comics
      Participant

      Howdy,

      I was wondering if anybody knows of a way to batch process a directory using a custom indesign script?

      Thanks,

      Duncan

    • #57567

      Sure. The getFiles command returns all files in a folder, possibly filtered by a mask. Do your Thing inside the loop; the rest ought to go all by itself.

      You might want to try your processing first on only a few files, before letting it rip into several thousands.

      Note that the final line ensures the file always gets overwritten. If you don't always want to do that, set a flag if it's to be saved, and use SaveOptions.NO when not. And, of course, you can create a new file and write to that instead.

      myList = Folder(Folder.myDocuments.fsName+”/someSubFolder”).getFiles(“*.indd”);
      alert (myList.join(“; “));

      for (list=0; list<myList.length; list++)
      {
      // Skip folders that accidentally can creep in
      if (myList[list] instanceof Folder)
      continue;
      // Make sure dialogs are not interfering
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
      try {
      currDoc = app.open (myList[list]);
      } catch (e)
      {
      // This happens on files for newer versions. Skip.
      alert ('Could not open “'+myList[list]+'”..?');
      continue;
      }
      // .. we want to see the 'real' errors
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
      // .. do something
      alert (currDoc.pages.length);
      currDoc.close(SaveOptions.NO); // SaveOptions.YES);
      }

    • #57575
      Duncan Comics
      Participant

      Hey Jongware.

      You've been an amazing help with the scripting. First you provided me with a link to all the “verbs” associated with indesign, which i'm slowly goning thorugh and picking apart which is amazing. Then you show me this, and i've had to make a few tweaks in the code.

      myList = Folder (“/C/trial/*/”).getFiles(“*.indd”);

      I'm trying to get “myList” to see all the subdirectories within the trial folder.

      If i remove the “*” and put my “indd” files into trial the script works perfectly.

      Do i have to put the subfolders into an array?

      Or do i need to write a loop that goes into the folder and scans until it's done?

      (Folder.myDocuments.fsName+”/someSubFolder”) i'm not sure as to what this line is doing?

      I always find im trying to reinvent the wheel, and usually there is an easy answer with stuff.

      Thanks,

      Duncan

    • #57578

      The getFiles command returns anything that matches the mask you provide. In my example, I limited it to “”*.indd” to get only InDesign files, but — after some deliberating — I decided to add an explicit check because it may also return a Folder named “my files.indd”. (Don't laugh. Of course, me personally would never name a folder something like that, but you'd be amazed.)

      This 'explicit check' is right below the for..loop:

      if (myList[list] instanceof Folder)
      continue;

      .. so the entire rest of the loop will be skipped ('continue') if the item under consideration is a folder. To make it scan for just folders, you need to make two crucial changes:

      1. Change the pattern of 'getFiles' to (“*”), or omit it entirely — its default is in fact “*”. This will make it pick up any and all files and folders.

      2. Change the line after the for.. command to

      if (myList[list] instanceof File)

      continue;

      so the commands below it will only be processed when 'myList[list]' is a folder.

      .. Or do i need to write a loop that goes into the folder and scans until it's done?

      Well … it depends on what you are planning to do. If you want to 'process' all InDesign files in a certain folder and all the ones in subfolders, possibly nested, you need a recursive routine. If you look up “Recursive” in any good dictionary, it'll read “see Recursive” — that sums it up, I guess.

      A good way would be:

      1. Don't use the for..loop I gave as an example, but instead define two functions: processFile and processFolder.

      2. Start your script with 'processFolder'.

      3. Have this function scan for all files, and call the function 'processFile' for each.

      4. Then have it scan for folders, and call the function 'processFolder' for each.

      You see, hopefully, that #4 is the recursive part: if there is a folder inside your main (starting) folder, the function will call itself and process the files in there. If there is a folder in that, it will call itself and process the files in that one. Et cetera ad infinitum, or until all files & folders are processed — whatever comes first.

      Tip: If part of your 'processFile' involves saving the file under a new name, take care not to process these in turn! That might lead to an endless loop, not quite ad infinitum but merely ad full hard disk.

    • #57579

      .. It's actually quite simple. This quickie fills a text frame (your cursor has to be in it) with all file names, from a certain starting folder, including files in sub(-sub-sub-sub)-folders. I would've posted it sooner, were it not that I (stupidly) tried it out on a folder that contained several thousands of files and folders … (It made me think I made a mistake somewhere…)

      I also use another property of the 'getFiles' function: it can accept a simple text string, which is a file/folder mask such as “*.indd” or “*”, but it may also accept a function that somehow determines if a file or folder should be included. See the JS Help for details — in this case, all I do is check whether it is a file or a folder, and process only the ones I need.

      For demonstration purposes, the entire folder structure gets saved into an array, so you can tell it works — you won't need any of the 'myResult' stuff.

      It also demonstrates a crucial point on working with recursive functions: all variables inside that function need to be 'local', that is, declared using 'var xxx'. If not, its previous contents are overwritten by the function when it calls itself again!

      result = processFolder (Folder(“/SomeFolder”));
      app.selection[0].contents = result.join (“r”);
      exit(0);

      function processFolder (startingPoint)
      {
      var tempFileList, tempFolderList, count;

      // only for debugging:
      var myResult = [];
      myResult.push (“rStarting with “+startingPoint.fsName);

      tempFileList = startingPoint.getFiles( function(x) { return x instanceof File; } );
      // tempFileList now contains just files.
      for (count=0; count<tempFileList.length; count++)
      {
      myResult.push (tempFileList[count].fsName);
      processFile (tempFileList[count]);
      }

      tempFolderList = startingPoint.getFiles( function(x) { return x instanceof Folder; } );
      // tempFolderList contains just folders.
      for (count=0; count<tempFolderList.length; count++)
      {
      myResult = myResult.concat(processFolder (tempFolderList[count]));
      }
      return myResult;
      }

      function processFile (filename)
      {
      // .. do whatever you need to here.
      }

    • #57580

      .. That “join(“r”)” line near the top should actually read “join(“r”)”, to insert a return between each line. If I call up the Edit Post, I get the entire script on a single line :(

      (It's of no effect to the inner workings of the script but I thought you might to try the sample as-is.)

    • #57595
      Duncan Comics
      Participant

      Oh yeah, this is amazing!

      This is better then bread butter and jam. Thanks so much!

      If you ever want a cartoon https://www.duncancomics.ca

      go there and send me an email with your address and i'll mail you something EPIC!

      Thanks again!

      -Duncan

    • #57609

      That's some funny stuff you got there — I think I'm gonna hold you to that promise!

      All created on the computer?

    • #57612
      Duncan Comics
      Participant

      I ink everything by hand, but then i use the computer for the colour and texture stuff.

      -Duncan

    • #59629
      heyjodi
      Member

      Hello Jongware,

      I just did a Google search on InDesign batch processing, found this thread and signed up as a member so I could write to you. I am hoping that you can steer me toward a more 'dumbed down' solution. I don't write code and am unfortunately confused by your and duncancomics exchanges. I'm smart, but not in this world.

      Basically I'm working with a solar company and we want to set up an InDesign template to publish a nice looking report for each of our clients. We want them to see images of their house, the install, reports of their purchase, maintenance information, etc. We are getting a FLOOD of customers right now and don't want to manually insert these graphics for each client. How can we put the graphics into folders and/or name them and flip the switch to have them populate the template.

      I keep reading about Watch Folders, PowerSwitch, AppleScript… is there a basic way of doing what we need to have done??

      I appreciate your help!

    • #59630
      David Blatner
      Keymaster

      @heyjodi: You might consider using InDesign's Data Merge feature, which doesn't require any scripting. We have a lot of posts about data merge on our site, including: https://creativepro.com/aut&#8230;..ndling.php

    • #59641
      heyjodi
      Member

      Thanks so much David! this series of tutorials looks very thorough and easy to understand. Thanks also for getting back to me so quickly! Unfortunately I am in CS3 and I don't see under the Window tool palatte Automation>Data Merge. The post mentioned it was originally installed in CS if one installed PageMaker… and it came in as a “stealth” feature in CS2… but I can't locate the option in CS3.

    • #59645
      David Blatner
      Keymaster

      Data Merge was definitely in CS3, but I honestly don't remember what submenu its in… keep looking in the Window menu!

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