Copiar vínculo al Portapapeles
Copiado
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
Copiar vínculo al Portapapeles
Copiado
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.
Copiar vínculo al Portapapeles
Copiado
Also, remove "var" from the first line of code. It's a function, not a variable.
Copiar vínculo al Portapapeles
Copiado
It's a simple code in js file. But it doesn't work running in console.
Something must be wrong?
Copiar vínculo al Portapapeles
Copiado
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);
Copiar vínculo al Portapapeles
Copiado
Why do you use two replace functions?
Copiar vínculo al Portapapeles
Copiado
Hi Nesa,
now I use your function in js File. But it does not work on console. Something is wrong?
Copiar vínculo al Portapapeles
Copiado
Have you re-started Acrobat?
Copiar vínculo al Portapapeles
Copiado
Yes I have restartet Acrobat. But I get this error:
Copiar vínculo al Portapapeles
Copiado
The path at saveAs is incorrect.
Copiar vínculo al Portapapeles
Copiado
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');
Copiar vínculo al Portapapeles
Copiado
Use this:
var newPath = path.replace(/\.pdf$/,'_'+userName+'.pdf');
Copiar vínculo al Portapapeles
Copiado
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?
Copiar vínculo al Portapapeles
Copiado
I have adobe acrobat Pro (32 bit).
Here is my path and js file, but you see the error.
Copiar vínculo al Portapapeles
Copiado
Before the saveAs add this line:
console.println(newPath);
Copiar vínculo al Portapapeles
Copiado
This is the path on 32bit acrobat: C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts
Copiar vínculo al Portapapeles
Copiado
This is not the path of the application data.
Copiar vínculo al Portapapeles
Copiado
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?
Copiar vínculo al Portapapeles
Copiado
This is not a problem.
Make sure that you re-start Acrobat after a change of the script file.
Copiar vínculo al Portapapeles
Copiado
You can also just create an action, it is much simpler.
Copiar vínculo al Portapapeles
Copiado
I re-start acrobat after change js file with adding console.println(newPath);
But there is something wrong?
Copiar vínculo al Portapapeles
Copiado
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.
Copiar vínculo al Portapapeles
Copiado
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?
Copiar vínculo al Portapapeles
Copiado
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 ?
Copiar vínculo al Portapapeles
Copiado
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?
Copiar vínculo al Portapapeles
Copiado
From 6878_AutoMPK there are normal folders.