Copy link to clipboard
Copied
Hello.
I would like to close by script a document opened from an attachment of another document:
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.
Copy link to clipboard
Copied
Why does you set aFiles when you don't use it?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
app.openDoc("DocToClose.pdf",this);
will open a document at the folder as the open document.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
the file still does not want to close ...
Here is the message I got:
Copy link to clipboard
Copied
This will not work because aFiles[0] is a text string and not a document object.
Copy link to clipboard
Copied
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 ?
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Why not following?
for (var i = 0; i < aFiles.length; i++) if (aFiles[i].info.Title == "Delete") aFiles[i].closeDoc(true);
Copy link to clipboard
Copied
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 ...
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Yes, that's what I meant. It should have worked...
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more