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

How to close a document opened from attached documents

Participant ,
Jul 05, 2021 Jul 05, 2021

Hello.

I would like to close by script a document opened from an attachment of another document:

PasGlop_0-1625468399451.png

 

I tried with the following code :

this.fileToClose = app.openDoc("DocToClose.pdf",this);
this.fileToClose.closeDoc(true)

 

It works when the document is opened from anywhere, but not when it is an attachment.

Is there a way to do it similarly but for an attachment?

Many thanks in advance for your help.

 

 

TOPICS
JavaScript
2.6K
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 Expert ,
Jul 05, 2021 Jul 05, 2021

Why does you set aFiles when you don't use 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
Participant ,
Jul 05, 2021 Jul 05, 2021

Indeed, there is no reason: I use this part to list all the documents for another functionality ... and I made a copy / paste too generous.

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 Expert ,
Jul 05, 2021 Jul 05, 2021

app.openDoc("DocToClose.pdf",this);

will open a document at the folder as the open document.

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 Expert ,
Jul 05, 2021 Jul 05, 2021

You have to get a reference to this Document object in order to be able to close it.

If the file is disclosed then it should appear in the activeDocs array and then you would be able to close it from there. Otherwise you will have to open it yourself using a script, and maintain a variable that's pointing to it. I also suggest you examine the path property of the attached file to see how it referenced internally.

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
Participant ,
Jul 05, 2021 Jul 05, 2021

The file is disclosed and with this line, I can see the concerned file (among others) when I display the content of "aFiles" :

var aFiles = app.activeDocs.filter(function(a){return this.path!=a.path;}).map(function(a){return a.documentFileName;});

 

But, despite all my efforts today, I am unable to find the way to close THE document I need among all those are opened (the one opened from the master PDF with this code) :

this.exportDataObject({ cName: "TEST.pdf", nLaunch: 2 });

 

Could you tell me, please, where I can find a sample to read, learn and transpose? for now i'm completely stuck on this topic...

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 Expert ,
Jul 05, 2021 Jul 05, 2021

If aFiles is not empty and contains that document as its first item then you all need to do is add this command:

aFiles[0].closeDoc(true);

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
Participant ,
Jul 05, 2021 Jul 05, 2021

the file still does not want to close ...

Here is the message I got:

 

PasGlop_0-1625507063263.png

 

 

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 Expert ,
Jul 05, 2021 Jul 05, 2021

This will not work because aFiles[0] is a text string and not a document object.

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
Participant ,
Jul 05, 2021 Jul 05, 2021

I don't understand :

When I do this ;

var aFiles = app.activeDocs.filter(function(a){return this.path!=a.path;}).map(function(a){return a.documentFileName;});
console.println(aFiles[0])
console.println(aFiles[1])
console.println(aFiles[2])
console.println(aFiles[3])

 

I obtain that :
InsideMaster3.pdf
Outside2.pdf
InsideMaster4.pdf
Outside1.pdf

 

Why "aFiles[0].closeDoc(true);" doesn't work ?

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 Expert ,
Jul 05, 2021 Jul 05, 2021

This will not work because aFiles[0] is a text string and not a document object.

 

In your function use

 return a;

not

return a.documentFileName;

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
Participant ,
Jul 05, 2021 Jul 05, 2021

Crystal clear. I finally understood.

Many thanks for your patience.

 

Therefore, piece of code that works :

var aFiles = app.activeDocs.filter(function(a){return this.path!=a.path;}).map(function(a){return a;});

aFiles[0].closeDoc(true); //will close the first file of the list

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
Participant ,
Jul 05, 2021 Jul 05, 2021

One more question.

I wrote this :

//First part : To bring to front the master document

var d = app.activeDocs;
for (var i = 0; i < d.length; i++)
{
if (d[i].info.Title == "Master")
d[i].bringToFront();
}

 

//Seconde Part : To delete all documents with a specific info.Title

var aFiles = app.activeDocs.filter(function(a){return this.path!=a.path;}).map(function(a){return a;});
for (var i = 0; i < aFiles.length; i++)
if (aFiles[i].info.Title == "Delete")
aFiles[i].closeDoc(true)

 

When I run the first part, then, the second part, all the documents with the info.Title "Delete" are closed, only the Master remains open.

 

BUT, when I run both parts at once (in 1 script), the Master file + one file remain open. I have to run the 2nd part a 2nd time to close the recalcitrant file.

 

Can you tell me why the behavior is different, but shouldn't from my point of view?

 

I have attached the test documents (two files are attached to the Master to test :

   - the opening of "annexed documents" of the Master,

   - and files opened from another source)

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 Expert ,
Jul 05, 2021 Jul 05, 2021

Since you're removing items from the array while traversing it, you should start from the end and work your way back to the beginning.

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
Participant ,
Jul 05, 2021 Jul 05, 2021

You mean

this :              for ( var i = aFiles.length-1; i>=0; i--)

instead of :     for (var i = 0; i < aFiles.length; i++)   ?

 

Same result. 😞

 

I tried inserting buttons containing separate scripts, but it also doesn't work.
I only get the result by running the two scripts consecutively from the console.

 

 

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
Participant ,
Jul 06, 2021 Jul 06, 2021

Here is the solution I found (not sure it's the simplest and the most "professional", but it works!)

If anyone comes up with a more ... academic solution, I would be happy to take it.

 

//Part 1

var d = app.activeDocs;
for (var i = 0; i < d.length; i++)
if (d[i].info.Title == "Master") d[i].bringToFront();

 

//Part 2

var aFiles = app.activeDocs.filter(function(a){return this.path!=a.path;}).map(function(a){return a;});
var count = aFiles.length
console.println(count)


if (count == 1)
{
if (aFiles[0].info.Title == "Delete")
aFiles[0].closeDoc(true)
}
else if (count == 2)
{
if (aFiles[1].info.Title == "Delete")
aFiles[1].closeDoc(true)
if (aFiles[0].info.Title == "Delete")
aFiles[0].closeDoc(true)
}

else if (count == 3)
{
if (aFiles[2].info.Title == "Delete")
aFiles[2].closeDoc(true)
if (aFiles[1].info.Title == "Delete")
aFiles[1].closeDoc(true)
if (aFiles[0].info.Title == "Delete")
aFiles[0].closeDoc(true)
}


else if (count == 4)
{
if (aFiles[3].info.Title == "Delete")
aFiles[3].closeDoc(true)
if (aFiles[2].info.Title == "Delete")
aFiles[2].closeDoc(true)
if (aFiles[1].info.Title == "Delete")
aFiles[1].closeDoc(true)
if (aFiles[0].info.Title == "Delete")
aFiles[0].closeDoc(true)
}

etc.

 

 

 

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 Expert ,
Jul 06, 2021 Jul 06, 2021

Why not following?

for (var i = 0; i < aFiles.length; i++) if (aFiles[i].info.Title == "Delete") aFiles[i].closeDoc(true);

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
Participant ,
Jul 06, 2021 Jul 06, 2021

Nicer, better 🙂

 

but when I run both parts of the script in one action (console, or document script), 2 files stay open instead of only one.

I need to create a button in the documents opened with part 1 (to focus on the master document) and create another button in the master file to run part 2.

Cannot run a single script to a) focus on the master file and b) delete other files ...

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
Participant ,
Jul 06, 2021 Jul 06, 2021

This code seems OK (I don't really know why): 

 

if (this.info.Title != "Master")
this.closeDoc(true)

var aFiles = app.activeDocs.filter(function(a){return this.path!=a.path;}).map(function(a){return a;});
for (var i = 0; i < aFiles.length; i++) if (aFiles[i].info.Title == "Delete") aFiles[i].closeDoc(true);

 

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 Expert ,
Jul 06, 2021 Jul 06, 2021
LATEST

Yes, that's what I meant. It should have worked...

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