These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Beginner's Save As Folder-Level Script

EEET Writer
Registered: Mar 29 2010
Posts: 14

Trying to achieve a crude, but easy to understand, folder-level script that can open a PDF, watermark it, and save it with the person's name within the file name. I'm spelling out the paths for simplicity. The is be temporarily placed in the local path folder to run on Acrobat startup. I can't seem to put together all of the concepts (like trusted functions) needed to make something that actually works. Assuming I'm even approaching this correctly, a working example would seem like it would be helpful to many beginners (like me!). I'd appreciate advice; I'm sure I'm starting with many mistakes.

// Test Doc.pdf has already been set with this.disclosed = true using batch processing.
//
var someName = "Joe Smith"; // name to be used in both filename and for watermark;
var mySaveAs = app.trustedFunction(
function(cpath)
{
app.beginPriv();
this.saveAs(cpath);
app.endPriv();
}
);
app.openDoc("/c/Documents and Settings/LoginMe2/My Documents/MyFolder/Test Doc.pdf");
// Add watermark, reset disclosure, save with new name. Original to remain unchanged;
this.addWatermarkFromText({
cText: someName,
nFontSize: 48,
aColor: [ "G" ,0.8 ],
bOnTop: false,
nRotation: 45
});
this.disclosed = false;
this.mySaveAs("/c/Documents and Settings/LoginMe2/My Documents/MyFolder/Test Doc " + someName + ".pdf");
this.closeDoc(); // close the personalized copy;
// This script is brute force, so repeat many times with different individual names;

Randy C.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
First of all, only the mySaveAs part needs to be in a folder-level script. That rest can be executed from a button, for example.

Also, I spot at least one mistake in your script: You try to launch mySaveAs as method of the "this" object. You should just call it by itself, like so:
mySaveAs("/c/Documents and Settings/LoginMe2/My Documents/MyFolder/Test Doc " + someName + ".pdf");

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

EEET Writer
Registered: Mar 29 2010
Posts: 14
As a beginner, I'm now just trying to focus on achieving a successful saveAs. This opens TargetDoc.pdf, but then the console says oDoc has no properties. Maybe I don't understand the passing of arguments in functions, and/or I apparently don't understand doc objects (where I'm trying to use oDoc). Examples of saveAs that I've found elsewhere are incomplete and/or have embellishments that cloud my understanding of the basics. I'm using Acrobat 8 Pro. Anyone know how to make this work and willing to help?

/*  Folder-Level Script saveAs Attempt  *//* Save a sample PDF to a new file name */// TargetDoc.pdf must already exist in the My Documents folder. // Create a trusted function called mySaveAs.var mySaveAs = app.trustedFunction(function(cPath, oDoc){app.beginPriv();oDoc.saveAs(cPath + "NewCopy.pdf");app.endPriv();}); myPath = "/c/Documents and Settings/login_folder/My Documents/"; // Open the target PDF.var myDoc = app.openDoc(myPath + "TargetDoc.pdf"); mySaveAs(myPath, myDoc);

Randy C.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Is TargetDoc disclosed?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

EEET Writer
Registered: Mar 29 2010
Posts: 14
Disclosure of TargetDoc.pdf makes no difference in the error message "oDoc has no properties".

Randy C.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Did you disclose it by creating a document-level script with
this.disclosed = true;
? Just making sure...

Also, does TargetDoc.pdf open at all?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

EEET Writer
Registered: Mar 29 2010
Posts: 14
I use batch processing to run the disclosure script. It appears to run:
/* change the disclose property */this.disclosed = true;

TargetDoc.pdf opens when I start Acrobat 8 Pro (ver 8.2.2 for Windows).
Then, I open the console. The part of the message, after the startup info is two lines:
oDoc has no properties
11:Folder-Level:User:open_specific_file_example_08.js
...and I'm guessing the "11" means "line 11" of the .js file which happens to be "oDoc.saveAs..." (note that I removed an empty line before posting the code; the posted code line count is different).

The only other thing that I changed for posting is "login_folder" instead of the my actual folder name.

Randy C.

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Have you tested the return value from the action 'app.openDoc(myPath + "TargetDoc.pdf");'?

I do not believe you have a folder for "/c/Documents and Settings/login_folder/My Documents/", because in Windows 'login_folder' would be replaced with the loginName. You can use the 'app.getPath' method to get the path for the user documents:
app.getPath('user', 'documents');
Which for my loginName of 'kaiserg' returns:
Quote:
app.getPath('user', 'documents');
Your code to open the file could read:
console.show();console.clear();// get path to logged in user's documentsvar myPath = app.getPath('user', 'documents');console.println(myPath); // see what document path look like// Open the target PDF.var myFile = myPath + "/TargetDoc.pdf";console.println(myFile); // see what path & file name looks likevar myDoc = app.openDoc(myFile);// if a non-null returnedif (myDoc != '') {// open documentmySaveAs(myPath, myDoc);} else {// document open failedapp.alert('Problem opening ' + myPath + "TargetDoc.pdf", 0, 0);}

I have added some debugging code. Also 'app.getPath()' also has security restrictions if not run from batch or console operations. So to use this code anywhere else will require a trusted function.

George Kaiser

EEET Writer
Registered: Mar 29 2010
Posts: 14
To clarify, I was substituting "login_folder" for my actual loginName because my loginName happens to be my company ID number -- which I prefer not to post. Using pieces of your code, the path successfully display as expected in the console. And my document opens successfully (console or folder-level script). But, I'm trying to get my saveAs, and now getPath code, into a folder-level script.

Given your introduction of "getPath", I'm currently struggling with the arguments for making getPath a trusted function. I can make "app.newDoc" work as a trusted function in the console, and I can make a "saveAs" work as a trusted function in the console, but I just don't understand the arguments for getPath work as a trusted function. I barely understand the syntax and concept of passing arguments in general, and this one seem tricky. FYI, I find the API document (and other documents) very cryptic and have found no understandable examples elsewhere. It seems like the goal here is to pass just one thing: a path. However, getPath looks like it has two optional arguments. I hope my "question" makes sense and that I'm using the correct terminology. Here is one of many versions of bad code that I've tried. Can you advise on the arguments?

   var myGetPath = app.trustedFunction(function(cCategory, cFolder){app.beginPriv();app.getPath('user', 'documents');app.endPriv();}); var myPath = myGetPath(cCategory, cFolder);console.println(myPath);

Randy C.

EEET Writer
Registered: Mar 29 2010
Posts: 14
For anyone following this, I did figure out how to make a trusted function for getPath that runs in the console. Maybe it's not the most efficient, but it works:
var myGetPath = app.trustedFunction(function(cCategory, cFolder){app.beginPriv();var myFnPath = app.getPath(cCategory, cFolder);app.endPriv();return myFnPath;}); var myPath = myGetPath('user', 'documents');console.println(myPath);

Randy C.

EEET Writer
Registered: Mar 29 2010
Posts: 14
I'm puzzled. While working toward a folder-level JavaScript that can perform a saveAs, I don't understand what's happening with my attempt to create the variable "myDoc". Here's the code that I'm running both in the console and then as a folder level script. In both cases, TargetDoc.pdf successfully opens in Acrobat 8. I also would have guessed that "this." would be the same as "myDoc" after TargetDoc.pdf has opened.

var myDoc = app.openDoc("/C/Documents and Settings/my login folder name/My Documents/TargetDoc.pdf");console.println("finished opening the document"); // debug statementconsole.println('this is: ' + this.documentFileName);  // debug statementconsole.println('myDoc is: ' + myDoc.documentFileName);  // debug statement

When running this in the console, the console messages are:
finished opening the document
this is: undefined
myDoc is: TargetDoc.pdf
true

When running this as a .js file in the local folder, the console message are:
finished opening the document
this is: undefined
myDoc has no properties
4:Folder-Level:User:open__troubleshoot_21_js.

So, can anyone tell my why "myDoc" has no properties when run as a folder-level script? Help?

Randy C.

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Do you mean you placed this code in a function in a JavaScript file? And then called the function?

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

EEET Writer
Registered: Mar 29 2010
Posts: 14
No. I'm just trying to run those four lines of code as a .js file in a folder.

Randy C.

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
According to your results "myDoc" is a valid document object and "this" is something else. At the time Acrobat loads the file there are no open document so the "this" keyword is a pointer to the app object. That's the context of the code. It doesn't change mid-stream when the document is opened. In another script run independantly at a later time the "this" keyword will be a pointer to the open document object.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

EEET Writer
Registered: Mar 29 2010
Posts: 14
While it appears that myDoc is [b]only[/b] valid when these few lines are run in the console, why does myDoc not appear to be valid when I attempt to run these few lines as a folder-level script? (Console message from folder-level script: "myDoc has no properties".) Does the folder-level script continue to plow ahead before the openDoc command has actually finished opening the PDF?

Also, I see that I had a big misunderstanding of "this". It took me awhile to figure out why you (Thom P.) asked me if the line was used with a function. I mistakenly thought "this" was an object for general use. A hint for other beginners: See a core Javascript Guide, elsewhere, for the "special keyword", "this".

Randy C.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
The special meaning of the "this" object is also explained in the Acrobat JavaScript Scripting Reference, like so:

Quote:
In JavaScript, the special keyword this refers to the current object. In Acrobat, the current object is defined as follows:
●In an object method, it is the object to which the method belongs.
●In a constructor function, it is the object being constructed.
●In a document-level script or field-level script, it is the Document Object and therefore can be used to set or get document properties and functions.
●In a function defined in one of the folder-level JavaScripts files, it is undefined. Calling functions should pass the Document Object to any function at this level that needs it.

etc...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Opening a file with a document level script is not really the best idea. Acrobat is starting up so it is in flux. The folder level scripts are for setup, not for actually doing something. Whether or not it works is probably a timing and version issue.

Put your code in a folder level trusted function. Then run the function after Acrobat is up and going.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script