Programatically Change the File Name in a 'Save As' Window
How to set a different default file name in a 'Save As' window.
“How can I program a ‘Save As’ button to automatically insert a file name from the value of a form field, or multiple form fields?”
The question above has often been asked on Adobe Acrobat forums and been answered with a definitive "Not possible". Keep reading to find out why I disagree with that answer.
Two Methods
You can either program the button to execute the 'Save As' menu item like this:
app.execMenuItem("SaveAs");
or you can call the doc.saveAs method from a trusted function in a folder level script. When using the doc.saveAs method you must include the cPath parameter, which includes the file name. So while it is possible to pull the file name from form field values, the document saves 'silently' (no Save As browse window opens). The questioners wanted the browse window for selecting a file location, while automatically prefilling the file name in the window so the user does not have to type it in.
If you use the app.execMenuItem method, the user can browse the file system for the location, but the file name defaults to the current document file name, and users must type over the file name if they wish to change it.
Because neither of these methods get both results (user controlled Save As browse window and a default file name obtained from field values), you can see why the "Not possible" answer is so quickly offered.
Thinking Outside The Box
One of the first tools I created when I started pdfautomationstation.com was a custom Save As menu item that 'remembered' the last folder location to which any PDF was saved. It is still available to download free here (free membership signup required), although you'll have to disable New Acrobat, since the menu item parent is File, which is not contained in the menu of the new user interface.
Acrobat and Reader would default to the folder location of the current file when the Save As menu item was used. With lots of drilling down through subfolders, it became extremely annoying for users who had a lot of files to save in a specific location that was not the current location of the document. So another question was asked frequently and was met with the same "Not possible" response:
“How can I get Acrobat to remember the last folder location I saved to when using Save As from the menu so I don’t have to keep drilling down through multiple sub folders?”
My remember-the-location solution pulled the cPath parameter (minus the document file name) from a global variable that was changed every time the user changed the file location, and set to persistent to make it available after program restarts. I had to use some outside-the-box thinking to create this tool, which is the same method that renders the "Not possible" answer to the original question incorrect.
It Looks Just Like A 'Save As' Window
Acrobat provides an app method called browseForDoc( ) with three optional input parameters. The two we are concerned with are these:
bSave - A Boolean value (true or false). If true, the file system browse window is presented as it would be for a save operation.
cFilenameInit - A string that populates a default file name in the window's file name field.
Point 1 makes the window look like a Save As window, while point 2 allows for a default file name to be populated. Successful execution of this method returns an object with three properties. The one we are concerned with is the cPath property which will be passed into the doc.saveAs method to save silently.
This is two methods, but since the second one occurs silently, the end result is that the users see a Save As window with the default file name populated from a form field value. Both methods are privileged, so they should be called from a trusted function in a folder level script.
The Script
This script assumes there’s a text field in the form called File Name. Cancelling the window returns an error, so the script tests for an error using try/catch. If an error is returned the file does not save and an alert lets the user know that the operation was cancelled. The script creates a trusted function that should be saved as a folder level script:
my_SaveAs_ = app.trustedFunction( function (oDoc)
{
app.beginPriv();
try{var pth=app.browseForDoc(true,oDoc.getField("File Name").value+".pdf").cPath}
catch(e){var pth=null};
if(pth)
{oDoc.saveAs(pth)}
else
{app.alert("Operation cancelled by user.",1,0,"\x77\x77\x77\x2E\x70\x64\x66\x61\x75\x74\x6F\x6D\x61\x74\x69\x6F\x6E\x73\x74\x61\x74\x69\x6F\x6E\x2E\x63\x6F\x6D")}
app.endPriv();
})
The function can be called from a Mouse Up action in a button field like this:
my_SaveAs_(this);
Conclusion
Q: How can I program a 'Save As' button to automatically insert a file name from the value of a form field, or multiple form fields?
A: (Hyperlink to this article).