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

Partial Filename Text Variable

Community Beginner ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

Hello.

I have read through all the forums and cannot find a current working script of what we are after.

I have an InDesign document that has an alphanumeric code in the slug. I name all our files with an alphanumeric code at the start.

Example: ABC1234 Presentation 1

I'd like to be able to create a text variable that pulls in only the ABC1234 part of the filename. (First 7 characters)

Any help would be greatly appreciated.

TOPICS
Scripting

Views

5.8K

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 1 Correct answer

Community Expert , Mar 06, 2018 Mar 06, 2018

Hi Michael,

this one by Loic.Aigon  will be a very good starting point for you:

Re: Custom text variable from javascript

Instead of using word or character count as contents of the variable you could use the name of the document that is truncated by a little regular expression.

Just before line 28 add this. The regular expression returns the first 7 characters of the name of the active document:

var truncatedName = doc.name.match(/^.{7}/)[0];

And change line 28 to that:

tv.variableOptions.contents =  t

...

Votes

Translate

Translate
Community Expert ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

So you have a INDD document name like:

PSCP72_MIRRORS_PRES_R2.indd

And you want it to shorten to:

PSCP72R2.indd

Right?

Regards,
Uwe

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
New Here ,
Jun 12, 2019 Jun 12, 2019

Copy link to clipboard

Copied

Yes, that is correct!

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 ,
Jun 13, 2019 Jun 13, 2019

Copy link to clipboard

Copied

Then you could do something like this:

var s = "PSCP72_MIRRORS_PRES_R2.indd";

var a = s.split("_");

var truncatedName = a[0] + a[a.length-1]; // returns PSCP72R2.indd

Here I assume the following rule:

The first chunk before the first occurance of "_" should always be added to the last chunk that begins after the last occurance of  "_" in the name.

To make that work for all names you need at least two occurences of "_".

Regards,
UWe

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
New Here ,
Jun 16, 2019 Jun 16, 2019

Copy link to clipboard

Copied

Thanks for your help Uwe, and please excuse my ignorance... where exactly does this rule go? Is it within the current code I have such as in the 'Function' rule on line 6? I'm very new to scripts and trying to wrap my head around it!

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 10, 2020 Jan 10, 2020

Copy link to clipboard

Copied

What if you wanted to extract certain characters from the filename. For example, if i wanted to get the 3rd to 5th characters of the filename?

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 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

Hi,

if you have a string like that:

var name = "MyName.indd";

 

and you want to get the 3rd, the 4th and the 5th character of the name, you can simply do a concatenation of single characters where numbering starts with 0 as the first item:

 

var name = "MyName.indd";
var thirdToFithChar = name[2] + name[3] + name[4] ;

 

Or do it with method slice().

 

Quoting documentation:

String.slice( startSlice , endSlice ) extracts a substring of the given string and returns it as a new string. The substring begins at startSlice, and includes all characters up to, but not including the character at the index endSlice.

 

var name = "MyName.indd";
var thirdToFithChar = name.slice(2,5) ;

 

But first check the value of length of the string. Could be your file name is not five characters long.

 

Regards,
Uwe Laubender

( ACP )

 

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 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

Thank you for the response, Uwe. I don't have much experience coding so i was wondering how i would add this to the code below. It was using this to find the text variable for the version number in the filenmame, but now i need to zero in on the Client Name "IRV" (eg. 19IRV1234_filename.indd).

 

UpdateClientName// The present code can be used as a startup script.

#targetengine 'UpdateClientName'

const UpdateClientName = function(/*Event*/ev, doc,m,t)
// -------------------------------------
// AFTER_OPEN event handler.
// Reset the `ClientName` text variable to the 9 first characters of the
// doc file name iff they are formed of uppercase letters and/or digits.
// ---
// Tested conditions:
// 1. The event type MUST be 'afterSave' (prevent wrong listener.)
// 2. The event MUST provide a valid `fullName` prop (File),
// since one can't rely on document.fullName yet!
// 3. The save-as File name MUST match /^[A-Z\d]{9}/.
// 4. The target MUST be a Document instance (who knows!)
// 5. It MUST have a `ClientName` TextVariable...
// 6. ...of the kind 'Custom text.'
// 7. No need to rewrite the variable if already set as expected.
{
( 'afterSave' == ev.eventType )
&& ( m=ev.properties.fullName )
&& ( m=m.name.match(/^[A-Z\d]{9}/) )
&& ( (doc=ev.target) instanceof Document )
&& ( (t=doc.textVariables.itemByName('ClientName')).isValid )
&& ( (t=t.variableOptions) instanceof CustomTextVariablePreference )
&& ( m[0] != t.contents )
&& ( t.contents=m[0] );
};

app.addEventListener('afterSave', UpdateClientName);

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
New Here ,
Oct 19, 2020 Oct 19, 2020

Copy link to clipboard

Copied

Hello. I am trying to use this script but keep getting the same job number, even when i resave the document to a new name. It also doesn't seem to be recognizing the if statement to stop if the name hasn't changed- as the document keeps showing that it is unsaved. Can you help?

 

 

Here is the script i am trying to use in the startup scripts folder.

 

#targetengine "com.kv.getkmjobnumber"

main();

 

//Will add a custom idleTask listener to count current document words

function main()

{

var myIdleTask = app.idleTasks.item("countWords");

 

if ( !myIdleTask.isValid ) {

myIdleTask = app.idleTasks.add({name:"countWords", sleep:100});

var onIdleEventListener = myIdleTask.addEventListener(IdleEvent.ON_IDLE, onIdleEventHandler, false);

}

}

 

//Idke task listener

function onIdleEventHandler(myIdleEvent)

{

 

var doc = app.properties.activeDocument,

tv;

 

if ( !doc ) return;

 

tv = doc.textVariables.item("jobCode");

 

!tv.isValid && tv = doc.textVariables.add({name:"KMJobNumber9", variableType:VariableTypes.CUSTOM_TEXT_TYPE});

var truncatedName = doc.name.match(/^.{9}/)[0];

if(truncatedName == tv.variableOptions.contents){return};

tv.variableOptions.contents = truncatedName;

 

}

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