Skip to main content
charlieg19576563
Inspiring
June 29, 2021
Answered

10 first letters/digits Partial filename

  • June 29, 2021
  • 3 replies
  • 6121 views

I was looking around here I could not find the information i was looking for, anyone that could help me with the following?

 

I want to get first 10 letters or digits from my filename as a text variable in my document.

ex:

110538-001 Design sample design

only 110538-001 should be grabbed from the filename into my document.

 

 

This topic has been closed for replies.
Correct answer Mike Bro

I got this! amazing! 🙂
When i change the indesign document name, would it be possible to get the variable to match and change as well?
it looks like it's always priting the first variable when i change the document name.
110538-001 Design sample design.ind
110538-002 Design sample design.ind

 

 


Hello @charlieg19576563

 

When you say: When i change the indesign document name, would it be possible to get the variable to match and change as well?
it looks like it's always priting the first variable when i change the document name.
110538-001 Design sample design.ind
110538-002 Design sample design.ind

 

I assume you're changing the file name by doing a Save As, if so give the below code a try as it will update the text variable "Doc Name 10" contents with the new document name.

#targetengine "session"

var eventListener = app.addEventListener("afterOpen", myCustomTextVariable, false); 

function myCustomTextVariable() {
 try {var doc = app.documents[0];
  var fileName = doc.name.replace(/.indd$/i, ""); 
   if(fileName.length >= 10){
    var tenName = fileName.slice(0,10);
    }else{
    exit();
   }
var tv = doc.textVariables.add({ variableType: VariableTypes.CUSTOM_TEXT_TYPE,
name: "Doc Name 10"
});
tv.variableOptions.contents = tenName;
  
 } catch(err) {}
}

var eventListener = app.addEventListener('afterSaveAs', myCustomTextVariableUpdate, false);
function myCustomTextVariableUpdate() {
   try {var doc = app.documents[0];
    var fileName = doc.name.replace(/.indd$/i, ""); 
     if(fileName.length >= 10){
      var tenName = fileName.slice(0,10);
      }else{
      exit();
     }
    app.activeDocument.textVariables.item('Doc Name 10').variableOptions.contents = tenName;
    
   } catch(err) {}
  }

 

Regards,

Mike

 

3 replies

Legend
June 30, 2021

Hello @charlieg19576563

 

Here's a play off of Brian's code as a startup script, this script will run when a Indesign document in opened and only if the filename is equal or greater than 10 characters it will create the text variable "Doc Name 10" of the first 10 characters.

Save and place the script here: Applications⁩ ▸ ⁨Adobe InDesign 2021⁩ ▸ ⁨Scripts⁩ ▸ ⁨startup scripts⁩

 

#targetengine "session"

var eventListener = app.addEventListener("afterOpen", myCustomTextVariable, false); 

function myCustomTextVariable() {
 try {var doc = app.documents[0];
  var fileName = doc.name.replace(/.indd$/i, ""); 
   if(fileName.length >= 10){
    var tenName = fileName.slice(0,10);
    }else{
    exit();
   }
var tv = doc.textVariables.add({ variableType: VariableTypes.CUSTOM_TEXT_TYPE,
name: "Doc Name 10"
});
tv.variableOptions.contents = tenName;
  
 } catch(err) {}
}

 

 

Regards,

Mike

charlieg19576563
Inspiring
June 30, 2021

Thank you all for your passion 😄

 I got the following syntax error

Legend
June 30, 2021

Hello @charlieg19576563

 

When copying the code into TextEdit you need to change the it to "Make Plain Text" under Format menu before saving it as a .jsx file.

 

Regards,

Mike

Community Expert
June 29, 2021

Here's a convulouted non-script way 

 

Set your Slug area in File>Document Setup to be a larger size (larger than your bleed

 

 

 

Insert a Text Frame

And then insert a cross reference

This will pull the entire File Name.

 

 

 

 

Create a new character style and call it Filename (or something) and you don't have to do anything with this - just leave it plain and blank.

 

Create a New Paragraph Style for the text in the slug

in the white are is just a space

 

 

Create a New Text Variable

 

 

It will draw on first set of characters up to a space in your slug area - drawn from the cross reference - and inserted as a variable in the text above.

 

The only draw back is that I have to update the Cross Reference when resaving to a different file name.

 

Attached is a sample IDML file which should work.

https://www.dropbox.com/s/q8d6ueksuhd5a44/54321_123%20Design%20sample%20design.idml?dl=0

 

 

Grant H
Community Expert
Community Expert
June 29, 2021

you cant refine the variables to that degree, but you cant fake it: you can assign a paragraph style to it with a grep style to "hide" any text after the first ten digits for eg by changing the text colour to none.

 

/G

brian_p_dts
Community Expert
Community Expert
June 29, 2021

Just make your own custom text variable? 

 

If you need a script to do it, try this as JSX:

 

var doc = app.documents[0];

var tenName = doc.name.slice(0,10);

var tv = doc.textVariables.add({  variableType: VariableTypes.CUSTOM_TEXT_TYPE,

name: "Doc Name 10"

});

tv.variableOptions.contents = tenName;

 

 

 

Also see this thread if you need to automate it more: 

https://community.adobe.com/t5/indesign/partial-filename-text-variable/td-p/9733786