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

Filename scripting InDesign

Community Beginner ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Hello everyone,

 

After reading every single post on the coummnity and everywhere in the internet, I can't find what I need to achieve my goal.


My problem is that we save files with very specific naming, which it gets used on the slug afterwards for production purposes.

 

I am trying to simplify this process and avoid errors as when typing manually.

 

Our format at present is as follows:

Client_Size_ClientNumber_ClientCategory

 

I would like to use the file data to fill information in the production data sheet but I am not certain on how to extract that data and utilise it a number of times accross the sheet.

 

My mind thinks to extract it as below:
Info1_Info2_Info3_Info4

 

And then in InDesign allocate this as a text variable so the info can be extracted as many times as required.

Hopefully I make sense on what I am talking about 🙂

 

Info boxes that I am referring to:

 

Ignacio314015688kvg_0-1698407011835.png

 

 

TOPICS
Scripting

Views

704

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 2 Correct answers

Community Expert , Oct 29, 2023 Oct 29, 2023

For Variable Link approach the following code should work. You should create textvariables with type custom text and insert them at all the places where you want the information to be replaced. The name of the variables should the same that are used inside the itemByName method in the code below. Then you can execute the following code

 

 

var doc = app.activeDocument;
var infos = doc.name.replace(".indd", "").split("_");
doc.textVariables.itemByName("showName").variableOptions.contents = infos[
...

Votes

Translate

Translate
Community Expert , Oct 30, 2023 Oct 30, 2023

My apologies @IC2023, I picked some lines from the previous script but did not copy it fully. I have edited the code in my original post, please give it a try now.

-Manan

Votes

Translate

Translate
Community Expert ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

So what exectly do you want to achieve ? Automatically split name of the INDD file into parts and put somewhere in your INDD Document ?

 

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
Participant ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Instead of Info 1, Info 2 etc, I would put the wildcards in square brackets inside the template like this:

before.PNG

 

With the following code you achieve the described intention.

var infos = app.activeDocument.name.replace(".indd", "").split("_");

var varsToReplace = ["[client]", "[size]", "[clientNumber]", "[clientCategory]"];

var tfs = app.activeDocument.textFrames;

for(i=0; i<tfs.length; i++) {
	for(j=0; j<varsToReplace.length; j++) {
		if(tfs[i].contents.indexOf(varsToReplace[j]) > -1){
			tfs[i].contents = tfs[i].contents.replace(varsToReplace[j], infos[j]);
		}
	}
}

 Running the script inside the above file with the name ClientXy_123_321_Publishing.indd, the result looks like this:

after.PNG

 

____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5

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 ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Almost... But you can't do it that way - as you'll destroy formatting... 

 

Your solution will work - only if there are no CharStyles applied or local formatting. 

 

You would have to use InDesign's built in Find&Change functionality.

 

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 ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Thank you GNDGN, much appreciated.

 

I think the above should do the trick, but I have tried to copy and save the code but I am getting the below message:
The file is not executable by any supported script language.

Perhaps I'm saving this wrongly, could you advice?

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
Participant ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

The script needs to be a .jsx file loaded into the Scripts panel in ID.

You can get more info about loading scripts in ID here: https://helpx.adobe.com/indesign/using/scripting.html 

____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5

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 ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Thank you, it works now.

The only issue I am having is that I need the wildcards used multiple times accross the document and at present, is only being updated once.

Could this be added to the code?

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
Participant ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

This can be solved via RegEx. Here is the updated script:

 

var infos = app.activeDocument.name.replace(".indd", "").split("_");

var varsToReplace = ["[client]", "[size]", "[clientNumber]", "[clientCategory]"];

var tfs = app.activeDocument.textFrames;

for(i=0; i<tfs.length; i++) {
	for(j=0; j<varsToReplace.length; j++) {
		if(tfs[i].contents.indexOf(varsToReplace[j]) > -1){
			var r = new RegExp(varsToReplace[j].replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"), "g");
			tfs[i].contents = tfs[i].contents.replace(r, infos[j]);
		}
	}
}
____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5

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 ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Still having the same issue, it won't update all instances.

I am attaching the Indesign file and script for reference.

Code I am using:

var infos = app.activeDocument.name.replace(".indd", "").split("_");

var varsToReplace = ["[showName]", "[standSize]", "[stdNumber]", "[exhName]"];

var tfs = app.activeDocument.textFrames;

for(i=0; i<tfs.length; i++) {
for(j=0; j<varsToReplace.length; j++) {
if(tfs[i].contents.indexOf(varsToReplace[j]) > -1){
var r = new RegExp(varsToReplace[j].replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"), "g");
tfs[i].contents = tfs[i].contents.replace(r, infos[j]);
}
}
}



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
Participant ,
Oct 28, 2023 Oct 28, 2023

Copy link to clipboard

Copied

This approach does the job with your given artwork:

var doc = app.activeDocument;

var infos = doc.name.replace(".indd", "").split("_");
// infos[0]: showName
// infos[1]: standSize
// infos[2]: stdNumber
// infos[3]: exhName

var tfShowName = doc.textFrames.itemByName("tfShowName");
tfShowName.contents = infos[0];

var tfMulti = doc.textFrames.itemByName("tfMulti").tables[0].cells;
tfMulti[4].contents = infos[1];
tfMulti[5].contents = infos[2];
tfMulti[6].contents = infos[3];

Before running the script, you have to rename the two relevant text frames to tfShowName and tfMulti in the Layers panel (see attachement) to target them directly with the script:

tfRenamed.PNG

____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5

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 ,
Oct 29, 2023 Oct 29, 2023

Copy link to clipboard

Copied

Hi GNDGN, than you for this!

 

I think it's pretty much there, attaching a full copy of my life for reference.

Page 2 is updating but not the instances accross the document.

 

Is there anything I am doing wrong?

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 ,
Oct 29, 2023 Oct 29, 2023

Copy link to clipboard

Copied

Hi @IC2023,

The script given is not going to be generic and won't work for all kinds of files. What if you move the content out of the table?  What if you have multiple frames with the same information to be replaced, the itemByName method won't find all and hence won't replace all.

So either go with what @Robert at ID-Tasker suggested or if the replacement text has no charstyle applied add one to each with the name of the style being the name of the info to use as the replacement. Then we can run a find change query and replace all of it. We can also check the use of variables.

-Manan

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 ,
Oct 29, 2023 Oct 29, 2023

Copy link to clipboard

Copied

For Variable Link approach the following code should work. You should create textvariables with type custom text and insert them at all the places where you want the information to be replaced. The name of the variables should the same that are used inside the itemByName method in the code below. Then you can execute the following code

 

 

var doc = app.activeDocument;
var infos = doc.name.replace(".indd", "").split("_");
doc.textVariables.itemByName("showName").variableOptions.contents = infos[0]
doc.textVariables.itemByName("standSize").variableOptions.contents = infos[1]
doc.textVariables.itemByName("stdNumber").variableOptions.contents = infos[2]
doc.textVariables.itemByName("exhName").variableOptions.contents = infos[3]

 

Edit:- Fixed the missing variable

-Manan

 

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 ,
Oct 30, 2023 Oct 30, 2023

Copy link to clipboard

Copied

Thank you Manan, I think this could the best approach.

 

However, I have created Custom text as Variables for each (Show name, std number...) but I am getting an error on Line 4, see below:

 

Variables created as Custom text and applied accross the document:

IC2023_0-1698656901016.png

 

Error from script:

IC2023_1-1698656917909.png

 

Script used:

 

var infos = doc.name.replace(".indd", "").split("_");
app.documents[0].textVariables.itemByName("showName").variableOptions.contents = infos[0]
app.documents[0].textVariables.itemByName("standSize").variableOptions.contents = infos[1]
app.documents[0].textVariables.itemByName("stdNumber").variableOptions.contents = infos[2]
app.documents[0].textVariables.itemByName("exhName").variableOptions.contents = infos[3]

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 ,
Oct 30, 2023 Oct 30, 2023

Copy link to clipboard

Copied

My apologies @IC2023, I picked some lines from the previous script but did not copy it fully. I have edited the code in my original post, please give it a try now.

-Manan

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 ,
Oct 30, 2023 Oct 30, 2023

Copy link to clipboard

Copied

LATEST

Thank you Manan, this now works as intended!

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 ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

After you run script - your original "tags" / placeholders are no longer there. 

 

If you want to update them multiple times - in the same file - you have to use XML tagging - or mark each place with a CharStyle - but then it would require completely different script. 

 

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 ,
Oct 28, 2023 Oct 28, 2023

Copy link to clipboard

Copied

Thank you Robert.

 

Your above statement could be correct, although, not all the Wildcards seem to be working - not even once.

 

Any advice?

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 ,
Oct 28, 2023 Oct 28, 2023

Copy link to clipboard

Copied

quote

Any advice?


By @IC2023

 

Yes - do it the "right" way. 

 

Script that you are testing is flawed:

A) it destroys formatting, 

B) you can't use it multiple times. 

 

I've already provided info how it should work. 

 

I'm not JS guy so can't help you with working code if you are on a Mac. 

I could only help you if you are working on a PC - I'm VBS guy. 

 

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