10 first letters/digits Partial filename

Community Beginner ,
Jun 29, 2021 Jun 29, 2021

Copy link to clipboard

Copied

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.

 

 

TOPICS
Scripting

Views

186

Likes

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

correct answers 4 Correct Answers

Adobe Community Professional , Jun 29, 2021 Jun 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

Likes

Translate

Translate
Enthusiast , Jun 30, 2021 Jun 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

Likes

Translate

Translate
Adobe Community Professional , Jun 30, 2021 Jun 30, 2021
Did you use Mike's code or mine? If Mike's, I made some revisions to execute the change afterSave, rather afterOpen, and I made it check that the variable didn't already exist before trying to rename. See below.  #targetengine "session" var eventListener = app.addEventListener("afterSave", myCustomTextVariable, false); function myCustomTextVariable() { try { var doc = app.documents[0]; var fileName = doc.name.replace(/\.indd$/i, ""); if(fileName.length > 10){ var tenName = fi...

Likes

Translate

Translate
Enthusiast , Jun 30, 2021 Jun 30, 2021
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 ...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 29, 2021 Jun 29, 2021

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Jun 29, 2021 Jun 29, 2021

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Jun 29, 2021 Jun 29, 2021

Copy link to clipboard

Copied

Here's a convulouted non-script way 

 

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

 

EugeneTyson_0-1624976994251.png

 

 

Insert a Text Frame

And then insert a cross reference

This will pull the entire File Name.

EugeneTyson_1-1624977053389.png

 

EugeneTyson_2-1624977084063.png

 

 

 

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

EugeneTyson_4-1624977287798.png

 

 

Create a New Text Variable

EugeneTyson_3-1624977203889.png

 

 

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

 

 

Likes

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
Enthusiast ,
Jun 29, 2021 Jun 29, 2021

Copy link to clipboard

Copied

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

Likes

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 ,
Jun 30, 2021 Jun 30, 2021

Copy link to clipboard

Copied

Thank you all for your passion 😄

image.png

 I got the following syntax error

Likes

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
Enthusiast ,
Jun 30, 2021 Jun 30, 2021

Copy link to clipboard

Copied

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

Likes

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 ,
Jun 30, 2021 Jun 30, 2021

Copy link to clipboard

Copied

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

 

 

Likes

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
Adobe Community Professional ,
Jun 30, 2021 Jun 30, 2021

Copy link to clipboard

Copied

Did you use Mike's code or mine? If Mike's, I made some revisions to execute the change afterSave, rather afterOpen, and I made it check that the variable didn't already exist before trying to rename. See below. 

 

 

#targetengine "session"

var eventListener = app.addEventListener("afterSave", 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{
       tenName = fileName;
    }
   var tv = doc.textVariables.itemByName("Doc Name 10");
   if (!tv.isValid) {
      tv = doc.textVariables.add({ 
         variableType: VariableTypes.CUSTOM_TEXT_TYPE,
         name: "Doc Name 10"
      });
   }
   tv.variableOptions.contents = tenName;
  
   } catch(err) {}
}

 

 

Likes

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 ,
Jul 01, 2021 Jul 01, 2021

Copy link to clipboard

Copied

Thank you so much! its working great!
When i try to apply paragrapgh style it seems like the text is in one block. is there a workaround to add such a specific paragrap style?Skärmavbild 2021-07-01 kl. 12.18.33.png

Likes

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
Adobe Community Professional ,
Jul 01, 2021 Jul 01, 2021

Copy link to clipboard

Copied

Apply a character style to the hyphen automatically by setting a GREP style in the paragraph style settings. 

Create a Character Style with the Kern settings you want, then set the GREP style to trigger that Character Style on: \-

 

 

 

 

Likes

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 ,
Jul 01, 2021 Jul 01, 2021

Copy link to clipboard

Copied

Thanks Brian!
It's still not really applying the GREP in the Variable text, i put a video here. The script is working and is really useful 🙂

Likes

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
Adobe Community Professional ,
Jul 01, 2021 Jul 01, 2021

Copy link to clipboard

Copied

Right, forgot that you can't apply GREP style to Text Variables. Alternative would be to add a space between the hyphens or figure out some kind of manual spacers on a master page. But in this case, you might have to just forsake design for convenience. 

Likes

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
Adobe Community Professional ,
Jul 01, 2021 Jul 01, 2021

Copy link to clipboard

Copied

LATEST

Haven't tried it, but you might be able to convert the variable to text in the Text Variable options to get the GREP style to stick. Of course that might mess up the script and future docs, but once a doc was set in stone and not used to propagate new docs it could work. 

Likes

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
Enthusiast ,
Jun 30, 2021 Jun 30, 2021

Copy link to clipboard

Copied

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

 

Likes

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