Copy link to clipboard
Copied
Dear Community,
I'm new with JS and Acrobat. I'm looking for a small Code for following Task:
I have an open pdf file and now I need a button to do the following:
Only save the open file in the same directory, but with filename = oldfilename_username.pdf so I can
work with this new file. The old file should be closed. I think I need identity.loginname for username.
Is the best was use an Action with JavaScript Code?
Do you have some Code for me?
Best Regards
Josh
Copy link to clipboard
Copied
It didn't work but it was recognized, as you got errors about lines of code within it, unless you were running it all from the Console... This error means the JS file is not recognized. So either it's not really a .js file (Notepad will add .txt to files it saves automatically) or it's not placed in the right place, or it contains an error. I recommend you switch to Notepad++ for a much better code editor, for starters.
Copy link to clipboard
Copied
Also, remove "var" from the first line of code. It's a function, not a variable.
Copy link to clipboard
Copied
It's a simple code in js file. But it doesn't work running in console.
Something must be wrong?
Copy link to clipboard
Copied
Make js file with this function and place it in javascript folder:
var MyDocSave = app.trustedFunction(function(){
app.beginPriv();
var userName = identity.loginName;
var doc = this.documentFileName;
var path = this.path;
var newPath = path.replace(doc, doc.replace(/\.pdf$/,'_'+userName+'.pdf'));
this.saveAs(newPath);
app.endPriv();});
Now you can call function from a console or a field or action...etc
Call a function like this:
MyDocSave(this);
Copy link to clipboard
Copied
Why do you use two replace functions?
Copy link to clipboard
Copied
Hi Nesa,
now I use your function in js File. But it does not work on console. Something is wrong?
Copy link to clipboard
Copied
Have you re-started Acrobat?
Copy link to clipboard
Copied
Yes I have restartet Acrobat. But I get this error:
Copy link to clipboard
Copied
The path at saveAs is incorrect.
Copy link to clipboard
Copied
Hi Bernd,
Nesa send me this two lines. What's incorrect here? I think we only use one replace
var newPath = path.replace(doc, doc.replace(/\.pdf$/,'_'+userName+'.pdf'));
this.saveAs(newPath);
It should be enough. But in the js file from Nesa, we have var MyDocSave ... It must be a function, not variable?
var newPath = path.replace(/\.pdf$/,'_'+userName+'.pdf');
Copy link to clipboard
Copied
Use this:
var newPath = path.replace(/\.pdf$/,'_'+userName+'.pdf');
Copy link to clipboard
Copied
I did not send you those lines, that is your script, I just changed the function.
When I call the function it works correctly, you should make sure you put js file in correct folder, what is the path to your javascript folder and do you have x64 Acrobat?
Copy link to clipboard
Copied
I have adobe acrobat Pro (32 bit).
Here is my path and js file, but you see the error.
Copy link to clipboard
Copied
Before the saveAs add this line:
console.println(newPath);
Copy link to clipboard
Copied
This is the path on 32bit acrobat: C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts
Copy link to clipboard
Copied
This is not the path of the application data.
Copy link to clipboard
Copied
You see my path (user and app).
I don't have C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts
Is this a problem or is there a solution?
Copy link to clipboard
Copied
This is not a problem.
Make sure that you re-start Acrobat after a change of the script file.
Copy link to clipboard
Copied
You can also just create an action, it is much simpler.
Copy link to clipboard
Copied
I re-start acrobat after change js file with adding console.println(newPath);
But there is something wrong?
Copy link to clipboard
Copied
Try running this command from the Console window, with the file open:
this.saveAs(this.path.replace(this.documentFileName, "_test.pdf"));
Does that work? If not, there's an issue with the path of the file.
Could be the fact it's on a network drive, or the accented character in the folder name, for example.
Copy link to clipboard
Copied
If I run your command in console window with file open, it doesn't work on directory.
It's a network drive
But if I run on other directory it works (I see undefined, but I see the correct file with new filename), great.
Is there a possibiltiy to run script on network drive, too?
Copy link to clipboard
Copied
Hi try67, hi Bernd, hi Nesa,
the js file works on this directory. It is great. I put it in action with MyDocSave(this); and it works.
Now we know the problem.
But on this network drive it doesn't work. What can I do to get same result?
Perhaps I have to use a little bit other code then path.saveas ?
Copy link to clipboard
Copied
I will give you full information. Then you can better help me
Here you the directory.
If I click von IDVen then you see network drive.
Perhaps you know how I have to set call auf path?
Copy link to clipboard
Copied
From 6878_AutoMPK there are normal folders.