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:
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[
...
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
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 ?
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:
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:
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.
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?
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
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?
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]);
}
}
}
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]);
}
}
}
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:
Copy link to clipboard
Copied
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
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
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:
Error from script:
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]
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
Copy link to clipboard
Copied
Thank you Manan, this now works as intended!
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.
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?
Copy link to clipboard
Copied
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.