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

Save button script not working

cyfu
Registered: Apr 26 2011
Posts: 32
Answered

Hi guys,
I have this code for a Save button on my form but can not get it to save to a different folder.
 
app.execMenuItem("saveAs", "/c/temp/" + this.documentFileName);
 
app.execMenuItem("saveAs"); //works fine but I would like to bypass the user navigating to the location.
 
Any ideas would be greatly appreciated.

My Product Information:
Acrobat Pro 9.0, Windows
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
execMenuItem only takes one parameter, the name of the menu item to execute.
You can't use it to specify a folder to the Save As command.

I would forget about a script and just place a shortcut in the default save folder to C:\Temp.

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

try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Well actually, what you can do, is use the saveAs command of the Document object (in a trusted context), and attach it to a button. That could work...

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

cyfu
Registered: Apr 26 2011
Posts: 32
Could you describe or give an example of what you mean by in a 'trusted context'? Thanks for the response.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
See this tutorial: Save PDF with JavaScript

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

cyfu
Registered: Apr 26 2011
Posts: 32
Thom,
I created the .js file and I see the JS Ref in the Help menu. But after I add the code on the tutorial:

var mySaveAs = app.trustedFunction(
function(oDoc,cPath,cFlNmame)
{
app.beginPriv();
// Ensure path has trailing "/"
cPath = cPath.replace(/([^/])$/, "$1/");
try{
oDoc.saveAs(cPath + cFlName);
}catch(e){
app.alert("Error During Save");
}
app.endPriv();
});

I get the following error...
SyntaxError: unterminated character class [
11:Folder-Level:User:MM app.jsunterminated character class [
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There is a bug in this code. The input parameter "cFlNmame" should be "cFlName"

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

cyfu
Registered: Apr 26 2011
Posts: 32
Fixed the input parameter name, but the error is still there. The error is on this line of code:

cPath = cPath.replace(/([^/])$/, "$1/");


SyntaxError: unterminated character class [
10:Folder-Level:User:MM app.jsunterminated character class [
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Hmm, yes there is an error in the regular expression:( Obviously the "/" needs to be escaped.

cPath = cPath.replace(/([^\/])$/, "$1/");

Sorry about that.

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

cyfu
Registered: Apr 26 2011
Posts: 32
Thank you Thom. I got it to work using gkaiseril code on the tutorial page. But his code renames the file to MyNewPdf.pdf.

How can I resave over the existing file that I'm working on? I took out making the array, removing file name and rebuilding file name from gkaiseril's original code. When I save now, I get an During Save message.

// mouse up action script to call the trusted mySaveAs function
// First make sure the function exists
if(typeof(mySaveAs) == "function") {
var sPath = this.path; // get path including file name and type
// save this doc, in sPath

mySaveAs(this,sPath, this.path);
} else {
app.alert("Missing Save Function\n" +
"Please contact forms administrator");
}
// end button mouse up action
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The "MySaveAs" function takes the file path and the file name as separate parameters. So, from your first post you should call it like this:

mySaveAs(this, "/c/temp/", this.documentFileName);


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

cyfu
Registered: Apr 26 2011
Posts: 32
Accepted Answer
Thanks Thom, that worked great!
cyfu
Registered: Apr 26 2011
Posts: 32
Thom,
i've read on the forum that it's not possible to have a script to delete a file. Right now, after the file gets saved into another directory, the same file exists in the original directory. Is it possible to Cut or Remove that file?

I've tried the following document level script.

function remove()
{
var myObject;
myObject = new ActiveXObject('this.documentFileName');
var f = myObject.GetFile('this.documentFileName');
f.Delete();
}
and called the function at the end of the Save button, but it's not getting removed.
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Acrobat JavaScript can't delete, cut or paste a file.

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