• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

JavaScript SaveAs with renaming filename

Community Beginner ,
Jan 27, 2024 Jan 27, 2024

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

TOPICS
JavaScript

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

Copy link to clipboard

Copied

Also, remove "var" from the first line of code. It's a function, not a variable.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 30, 2024 Jan 30, 2024

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?

 

josuam70602286_0-1706657231916.png

 

 

 

josuam70602286_1-1706657231917.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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);

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2024 Jan 30, 2024

Copy link to clipboard

Copied

Why do you use two replace functions?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 31, 2024 Jan 31, 2024

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?

 

josuam70602286_0-1706693277066.png

 

josuam70602286_1-1706693389640.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

Have you re-started Acrobat?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

Yes I have restartet Acrobat. But I get this error:

 

josuam70602286_0-1706719747292.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

The path at saveAs is incorrect.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 31, 2024 Jan 31, 2024

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');

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

Use this:

var newPath = path.replace(/\.pdf$/,'_'+userName+'.pdf');

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2024 Jan 31, 2024

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

I have adobe acrobat Pro (32 bit).

Here is my path and js file, but you see the error.

 

 

josuam70602286_0-1706736470259.png

 

josuam70602286_1-1706736470263.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

Before the saveAs add this line:

console.println(newPath);

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2024 Jan 31, 2024

Copy link to clipboard

Copied

This is the path on 32bit acrobat: C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

This is not the path of the application data.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 01, 2024 Feb 01, 2024

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?

josuam70602286_0-1706802082489.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

This is not a problem.

Make sure that you re-start Acrobat after a change of the script file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

You can also just create an action, it is much simpler.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2024 Feb 02, 2024

Copy link to clipboard

Copied

I re-start acrobat after change js file with adding console.println(newPath);

But there is something wrong?

 

josuam70602286_0-1706865922328.png

josuam70602286_1-1706866163764.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2024 Feb 02, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2024 Feb 02, 2024

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

josuam70602286_3-1706872221871.png

 

 

josuam70602286_0-1706870398419.png

 

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?

 

josuam70602286_2-1706872033304.png

josuam70602286_1-1706871506947.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2024 Feb 02, 2024

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.

josuam70602286_0-1706908493751.png

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 ?

 

josuam70602286_1-1706908570750.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2024 Feb 02, 2024

Copy link to clipboard

Copied

I will give you full information. Then you can better help me

Here you the directory.

josuam70602286_0-1706911084635.png

If I click von IDVen then you see network drive.

 

Perhaps you know how I have to set call auf path?

josuam70602286_1-1706911084636.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2024 Feb 02, 2024

Copy link to clipboard

Copied

From 6878_AutoMPK there are normal folders.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines