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.
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
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
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
...
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 "se
...
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
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
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
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
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
Copy link to clipboard
Copied
Thank you all for your passion 😄
I got the following syntax error
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
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
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) {}
}
Copy link to clipboard
Copied
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: \-
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
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