Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

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

Community Beginner ,
Feb 02, 2012 Feb 02, 2012

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!

TOPICS
Scripting
7.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 07, 2012 Feb 07, 2012

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;

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

while (app.documents.length) {
  doc = app.doc

...
Translate
Community Beginner ,
Feb 06, 2012 Feb 06, 2012

UP!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

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;

  name = doc.textFrames.itemByName("filename").contents;

  doc.close(SaveOptions.YES, new File(name));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 07, 2012 Feb 07, 2012

OK. Naming the object solved one problem, now i changed a bit and im getting an error 48 (cannot find folder).

Heres the code:

var Pasta = Folder.selectDialog("Selecione a pasta de destino dos arquivos.");

if (Pasta == null) exit();

if (app.documents.length == 0) exit();

while (app.documents.length > 1)

{

var i, Ref, doc;

for (i=0; i<app.documents.length; i++) {

  doc = app.documents;

  Ref = doc.textFrames.itemByName("Modelo").contents;

  // doc.close(SaveOptions.YES, new File(name));

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

  app.activeDocument.close();

}

}

Here's the error:

Error Number: 48

Error String: Cannot find the folder “"~/Desktop/Batch/61365.indd"”.

Engine: main

File: C:\Users\Diego Nunes\AppData\Roaming\Adobe\InDesign\Version 7.5\en_GB\Scripts\Scripts Panel\Save Indesign.jsx

Line: 16

Source:   app.activeDocument.save(Pasta+"/"+Ref+".indd");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

Try using

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 07, 2012 Feb 07, 2012

Good job John! Thx again. It worked, for a while...

Here's the result:

var Pasta = Folder.selectDialog("Selecione a pasta de destino dos arquivos.");

if (Pasta == null) exit();

if (app.documents.length == 0) exit();

while (app.documents.length > 1)

{

var i, Ref, doc;

for (i=0; i<app.documents.length; i++) {

  doc = app.documents;

  Ref = doc.textFrames.itemByName("Modelo").contents;

  // doc.close(SaveOptions.YES, new File(name));

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

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

  app.activeDocument.close();

}

}

But after saving a couple of files it gives me this error:

JavaScript Error!

Error Number: 3591

Error String: Cannot save “Carinhoso_line-9” under a new name.

The file “DBTmp302022597587” is damaged (Error code: 0).

Engine: main

File: C:\Users\Diego Nunes\AppData\Roaming\Adobe\InDesign\Version 7.5\en_GB\Scripts\Scripts Panel\Save Indesign.jsx

Line: 13

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

In the routine that gave me this error I had 18 files to save, it managed to save and close 9 files and then the error pop'ed up, still 9 to go now.

But then something even more serious happened, i went to check over the saved files and they were all mixed up. Only one file was saved by it's correct filename, the others were saved with different ones. This was the result:

Content from textbox (correct filename): 61365   -   61364   -   61363   -   61362   -   61361

                                          Saved filename: 61365   -   61363   -   61361   -   61344   -   61342

And so on...

Im guessing that's something wrong with the "timeline" to get the variable?

But were making a great progress, thanks so much for all the help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 07, 2012 Feb 07, 2012

John, as I said, this that im doing is only a Copy/Paste of some examples that I've seen, and using some logic I try to group them. But I'm definetly not a programmer () and I don't quite understand these lines that you quoted. Here's how i tried:

var 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];

var i, Ref, doc;

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

  doc = app.documents;

  Ref = doc.textFrames.itemByName("Modelo").contents;

  // doc.close(SaveOptions.YES, new File(name));

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

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

  app.activeDocument.close();

}

}

And it crashed right in the first save, where did I screwed it up? Can u sintax it all for me plz? If that's not too much to ask...

It crashed as the same error as before:

Error String: Cannot save “Carinhoso_line-18” under a new name.

The file “DBTmp302033213143” is damaged (Error code: 0).

Thank you so much my friend.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

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;

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;

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;

    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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 08, 2012 Feb 08, 2012
LATEST

Thank you John!

The first one crashed right in the first save.

And the second one saved them all as expected. Thank you so much.

Best regards!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines