Skip navigation
Diego_Pro_Imagem
Currently Being Moderated

Select text frame to get content, save and close - Java - Windows

Feb 2, 2012 8:13 AM

Tags: #java #save #windows #close #js #select_text_frame #get_content #save_and_close

Hi guys!

 

I've done some scripting for Photoshop and now I'm trying to do something for InDesign but this one is kind'a harder.

 

Let me explain it to you:

 

I have this "template" document, its a file where i have all the positions and marks for the "Data Merge" do his work. And it does perfectly, I can create all the merged documents fine, the solution that im working for is to get one of the fields that the "Data Merge" completes to be the filename, then save the file and close it.

 

This text box that's gonna be my filename has a special Paragraph Style that is unique on the document, I don't know if this is helpful.

 

That's it, after the job done by Data Merge is where the script has to start, in this moment I have something like 15 (keep in mind that this is a random number, depends on how many references i have in my Data Source) documents all ready to save and close.

 

I didn't even got started, searched for some examples but nothing came in my mind, any help?

 

Thanks guys, and please forgive for any grammatical errors, I'm brazilian.

 

Cya!

 
Replies
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Feb 7, 2012 12:28 AM   in reply to Diego_Pro_Imagem

    OK, this is pretty straightforward.

    Name the textframe with the filename. Let's say you call it "filename." (Use the layers panel).

    Then, you can use

      app.activeDocument.textFrames.itemByName("filename").contents

    to refer to the filename.

    So you'd do something like:

     

    var i, name, doc;
    for (i=0; i<app.documents.length; i++) {
      doc = app.documents[i];
      name = doc.textFrames.itemByName("filename").contents;
      doc.close(SaveOptions.YES, new File(name));
    }
    
     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Feb 7, 2012 5:15 AM   in reply to Diego_Pro_Imagem

    Try using

     

      app.activeDocument.save(new File(Pasta.fsName+"/"+Ref+".indd"));

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Feb 7, 2012 10:31 AM   in reply to Diego_Pro_Imagem

    Whoops.

    I guess what is going on here is that the numbers are shifting after your save the first document.

     

    I did not notice that you had wrapped the whole thing in a while loop. That's going to make the problem worse. Didn't that seem like it would cause a problem?

     

    The usual answer is to use a decrementing loop, though I'm sure that works here, but I would try it:

     

    for (i=app.documents.length-1; i>=0; i--) {
    

     

    But it might turn out that is insufficient. If so, I guess you should always operating on the active document, using:

     

    while (app.documents.length) {
      doc = app.documents[0];
    ...
    

     

    One or both of those should do it.

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Feb 7, 2012 11:47 AM   in reply to Diego_Pro_Imagem

    And it crashed right in the first save, where did I screwed it up?

    I thought I was reasonably clear when I said:

    I did not notice that you had wrapped the whole thing in a while loop. That's going to make the problem worse. Didn't that seem like it would cause a problem?

    Why did you do that? I told you to use:

     

    var i, name, doc;
    for (i=0; i<app.documents.length; i++) {
      doc = app.documents[i];

     

    And you changed it to something else that I did not tell you to do:

     

    while (app.documents.length) {
      doc = app.documents[0];
      var i, Ref, doc;
      for (i=app.documents.length-1; i>=0; i--) {
        doc = app.documents[i];

     

    This is the cause of some, but not all of your problems. Why did you do it?

     

    In any case, your entire script should look like this (decrementing loop):

     

    var i, Ref, doc,
        Pasta = Folder.selectDialog("Selecione a pasta de destino dos arquivos.");
    if (Pasta == null) { exit(); }
    if (app.documents.length == 0) { exit(); }
     
    for (i=app.documents.length-1; i>=0; i--) {
        doc = app.documents[i];
        Ref = doc.textFrames.itemByName("Modelo").contents;
        app.activeDocument.save(new File(Pasta.fsName+"/"+Ref+".indd"));
        app.activeDocument.close();
    }
    

    Or, like this (operating on the active document):

     

    var i, Ref, doc,
        Pasta = Folder.selectDialog("Selecione a pasta de destino dos arquivos.");
    if (Pasta == null) { exit(); }
    if (app.documents.length == 0) { exit(); }
     
    while (app.documents.length) {
        doc = app.documents[0];
        Ref = doc.textFrames.itemByName("Modelo").contents;
        app.activeDocument.save(new File(Pasta.fsName+"/"+Ref+".indd"));
        app.activeDocument.close();
    }
    

    While we're here, please note that use of "exit()" can be unreliable, especially as scripts get larger. You'll probably find it better to use "return," though it has different semantics.

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Feb 7, 2012 11:41 PM   in reply to Diego_Pro_Imagem

    Oh, I forgot to mention.

    This is Javascript.

    THat's not the same as Java, which is a nearly unrelated language and has no use in InDesign outside the InDesign Server product.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points