So I'm trying to set up a script that will merge a group of pdfs for me, but I'm having a bit of trouble with it. What I have is a folder that has a lot of pdfs in it, but they are grouped in groups of 4 based on an id number that is in the filename of each, with a different letter+underscore prefix to distinguish the 4 pdfs for each id.
This is my first attempt at javascript and acrobat w/ batch processing so I'm not familiar with it, but my plan was to have a batch process set up that would go through each pdf in the folder and run this script. The script would only do anything on 1 of the 4 pdfs in each group (easiest way I could find quickly to get the filename to work with, without running multiple times). Basically it would strip off the prefix part of the filename and the extension leaving the id number which would become the filename for the merged pdf. For some reason though the console is saying my local variables in the script are undefined and fails on the try block.
I've also noticed that the debugger doesn't seem very easy to use, and when I check "debug on start" it will come up, but the code shown is not the current code that is in my batch process. It does seem to update, but its always quite a few edits behind in the debugger. This is in acrobat 9 on osx 10.6.4. Thanks for any thoughts/help.
var replacement = /\.pdf$/i;
var filename = this.documentFileName.replace(replacement,"");
if (this.documentFileName.substr(0,1)=="c_" || this.documentFileName.substr(0,1)=="C_"){
var newdoc=app.newDoc();
console.println("RE "+replacement);
console.println("FN "+filename);
filename=filename.substr(2);
try
{
newdoc.insertPages({nPage: -1,cPath:("r_"+filename+".pdf")});
newdoc.insertPages({nPage: -1,cPath:("i_"+filename+".pdf")});
newdoc.insertPages({nPage: -1,cPath:("l_"+filename+".pdf")});
newdoc.insertPages({nPage: -1,cPath:("c_"+filename+".pdf")});
newdoc.saveAs({cPath:(filename+"xx.pdf")});
newdoc.closeDoc(true);
}
catch (e)
{
console.println("Aborted: " + e);
}
}
The script looks good. Are you saying that both "replacement" and "filename" are being printed to the console as "undefined"?
I'd try a couple of things.
1. Open one of your files in Acrobat and run the code, line by line, from the console window. See what happens.
2. Remove the try block from the script and add in more console.println() statements to get a handle on what's happening with the script parameters. You could try replacing "this" with "event.target"
Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script