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

Dynamic file name if a specific named field exists or not.

New Here ,
May 01, 2023 May 01, 2023

I have created folder level scripts with corresponding buttons to create unique file names each time based on information in the file and the users who saved the file to a specified location. 

Ocassionally there are files that will go to the same location, but the file itself does not have the named fields in my script and instead of created a second set of buttons and script for these types of files I would like the script to be able to identify if the file has the named field to use one naming convention and if not use another. 

I can't seem to find the correct syntax or functions to do this. Below is a sample script with my line of thinking to complete this. 

//Create Filename
function Name(){
if (doesObjectExists("Original Accession")==true){
var myFileName = this.getField("Original Accession").value + " " + getLoginName() +" " + myDateString();
}
else{
var myFileName = myIdString() + " " + getLoginName() +" " + myDateString();
}}

 

Later in the script i have a save as comand to a location with " +myFileName+".pdf". I do understand these need to be trusted functions and I have that set in place the bug I am ecountering is that the script doesn't understand myFileName. If I get rid of the if/else statement i recognizes one or the other of the naming conventions.  

TOPICS
JavaScript
2.1K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
May 01, 2023 May 01, 2023

What your current code is trying to do is basically this:

- If the field "Original Accession" doesn't exist, use its value in the new file name.

That doesn't make sense.

View solution in original post

Translate
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 ,
May 01, 2023 May 01, 2023

If this.getField("Original Accession") is null then the field doesn't exists.

Translate
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
New Here ,
May 01, 2023 May 01, 2023

It doesn't seem to work for me. I still get the same reference error of myFileName is not definted. 

I wrote 

function Name(){
if (this.getField("Original Accession")==null){
var myFileName = this.getField("Original Accession").value + " " + getLoginName() +" " + myDateString();
}
else{
var myFileName = myIdString() + " " + getLoginName() +" " + myDateString();
}}

 

is the problem that I need to definte myFileName outside the statement? Maybe something along the lines of 

//Create Filename
var myFileName = function(){
if (this.getField("Original Accession")==null){
var Name = this.getField("Original Accession").value + " " + getLoginName() +" " + myDateString();
}
else{
var Name = myIdString() + " " + getLoginName() +" " + myDateString();
}}

Translate
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 ,
May 01, 2023 May 01, 2023

Have a closer look at your code... This part has a clear issue with it:

 

if (this.getField("Original Accession")==null){
var Name = this.getField("Original Accession").value + " " + getLoginName() +" " + myDateString();
}

Translate
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
New Here ,
May 01, 2023 May 01, 2023

Please forgive me as my knowledge in coding is patchy at best. I am also constrained by using the Adobe Standard and not the Pro version so the debuging tool is hard for me to access and truly use. I am just unable to wrap my head around the problem to think of the solution.

If only use a single naming convention 

var myFileName = this.getField("Original Accession").value + " " + getLoginName() +" " + myDateString();  where "Original Accession is a field in the file. the LoginName is the users windows username, and the Date String is a time stamp it works when i use the save as function 

this.SaveAs("file path/"+ myFileName +".pdf")  

 

When i try to make a statment that defines what myFileName depenedent if "Original Accession" fields exists or not i get the reference error if undefitend myFileName. 

 

Translate
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 ,
May 01, 2023 May 01, 2023

What your current code is trying to do is basically this:

- If the field "Original Accession" doesn't exist, use its value in the new file name.

That doesn't make sense.

Translate
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
New Here ,
May 01, 2023 May 01, 2023
LATEST

Thank you so much. That was exactly my problem plus I needed to define that the variable myFileName existed outside that statement. 

 

Thank you again for your help. my code now looks like this and it works as needed.

//Create Filename
var myFileName;
if (this.getField("Original Accession") != null){
var myFileName = this.getField("Original Accession").value + " " + getLoginName() +" " + myDateString();
}
else{
var myFileName = myIdString() + " " + getLoginName() +" " + myDateString();
}

Translate
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