Copy link to clipboard
Copied
Hi can anyone please provide a Javascipt snippet that will link a text frame to the built- in Text Variable "File Name"?
Thanks for any help on this 🙂
Hi @TestriteVisual, here's a starting point. I've added the option to create your own text variable to show you how to set the options, such as include extension or include path. The script will check if there's an existing text var where name == varName and use that, or otherwise will create a new text variable with options. So if you change varName from "My FileName" to "File Name" is will pick up the pre-built text variable. Hope that makes sense.
- Mark
/**
* Select a text frame or put cu...
You insert the text variable as an object. Instead, create the frame, then insert a variable instance:
var fileNameTextFrame = graphicTemplate.textFrames.add ({
geometricBounds: [...],
contents: "File Name: ",
fillColor: "None"
});
fileNameTextFrame.parentStory.textVariableInstances.add (
LocationOptions.AFTER,
fileNameTextFrame.parentStory.insertionPoints[-1],
{associatedTextVariable: fileNameText}
);
P.
Copy link to clipboard
Copied
Hi @TestriteVisual, here's a starting point. I've added the option to create your own text variable to show you how to set the options, such as include extension or include path. The script will check if there's an existing text var where name == varName and use that, or otherwise will create a new text variable with options. So if you change varName from "My FileName" to "File Name" is will pick up the pre-built text variable. Hope that makes sense.
- Mark
/**
* Select a text frame or put cursor in some text and run script.
* Note: will clear text in container and add a FileName text variable.
* @discussion https://community.adobe.com/t5/indesign-discussions/javascipt-snippet-that-will-link-a-text-frame-to-the-built-in-text-variable-quot-file-name-quot/m-p/13708455
*/
function main() {
var doc = app.activeDocument,
textFrame = doc.selection[0];
if (
textFrame == undefined
|| !textFrame.isValid
|| !textFrame.hasOwnProperty('texts')
) {
alert('Please select a text frame and try again.');
return;
}
var varName = 'My FileName',
filenameTextVariable = app.activeDocument.textVariables.itemByName(varName);
if (!filenameTextVariable.isValid) {
// text variable not found, so make it
filenameTextVariable = doc.textVariables.add({
name: varName,
variableType: VariableTypes.FILE_NAME_TYPE,
});
filenameTextVariable.variableOptions.properties = {
includeExtension: false,
includePath: false,
textAfter: '',
textBefore: '',
};
}
// clear the text frame's contents
textFrame.contents = '';
// add the text variable instance
var tv = textFrame.texts[0].textVariableInstances.add();
// and link it to the file name text variable
tv.associatedTextVariable = filenameTextVariable;
} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Insert FileName Variable');
Copy link to clipboard
Copied
Thanks for your reply @m1b
I was able to take your snippet and incorporate it into my full script, and set it to make those mods on the default "File Name" Text Variable. It correctly modifies the text before/after, extension, path, etc, but I cannot get the text frame at the bottom of the page to actually place a linked field to the Text Variable "File Name", like when you right click "Insert Variable" and pick "File Name" in an open document. It just places unlinked text that says "[object TextVariable]", like in the screen shot below. Could you provide any additional insight you could provide to update the code to link to the "File Name" Text Variable.. Thanks again.
Here is the current script which creates a new document and adds the file name to the bottom left corner.
// Set units
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
// Create a dialog box to prompt user for input
var myDialog = app.dialogs.add({name:"Product Information"});
with(myDialog.dialogColumns.add()){
with(borderPanels.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Finished Width (inches):"});
}
with(dialogColumns.add()){
var finishedWidthField = textEditboxes.add({editContents:"20"});
}
}
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Finished Height (inches):"});
}
with(dialogColumns.add()){
var finishedHeightField = textEditboxes.add({editContents:"30"});
}
}
}
}
var myResult = myDialog.show();
// Get the values entered in the dialog box
if(myResult == true){
var finishedWidthIN = finishedWidthField.editContents;
var finishedHeightIN = finishedHeightField.editContents;
}
// Convert the entered values to points (1 inch = 72 points)
//coerce numbers
finishedWidth = Number(finishedWidthIN)*72;
finishedHeight = Number(finishedHeightIN)*72;
// Create a new document with the calculated dimensions
var graphicTemplate = app.documents.add({
documentPreferences: {
pageWidth: finishedWidth,
pageHeight: finishedHeight
}
});
graphicTemplate.marginPreferences.properties = {
top : 0,
left: 0,
right: 0,
bottom:0
};
// Add file name text frame
var fileNameSize = Math.max(Math.min(Number(finishedHeightIN), Number(finishedWidthIN)) * 2, 11);
var fileNameWidth = finishedWidth * 0.85;
var fileNameXdim = 72;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//file name variable
var fileNameText = app.activeDocument.textVariables.itemByName("File Name");
fileNameText.variableOptions.properties = {
includeExtension: true,
includePath: true,
textBefore: 'File Name: ',
textAfter: ' After text goes here',
};
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var fileNameTextFrame = graphicTemplate.textFrames.add({
geometricBounds: [graphicTemplate.documentPreferences.pageHeight - fileNameSize, 0, graphicTemplate.documentPreferences.pageHeight, fileNameWidth],
contents: "File Name: " + fileNameText,
fillColor: "None"
});
fileNameTextFrame.move([fileNameXdim, graphicTemplate.documentPreferences.pageHeight - 2 * fileNameSize]);
fileNameTextFrame.parentStory.appliedFont = "Arial";
fileNameTextFrame.parentStory.pointSize = fileNameSize;
Copy link to clipboard
Copied
You insert the text variable as an object. Instead, create the frame, then insert a variable instance:
var fileNameTextFrame = graphicTemplate.textFrames.add ({
geometricBounds: [...],
contents: "File Name: ",
fillColor: "None"
});
fileNameTextFrame.parentStory.textVariableInstances.add (
LocationOptions.AFTER,
fileNameTextFrame.parentStory.insertionPoints[-1],
{associatedTextVariable: fileNameText}
);
P.
Copy link to clipboard
Copied
Ah this worked perfectly, thanks @Peter Kahrel. Would you be able to point me to some InDesign documentation I could read up on to understnad the "variable instance" syntax, like parentStory, LocationOptions.AFTER, .insertionPoints[-1], etc? I would like to be able to learn how to make sense of and write similar code. Thanks again for your help.
Copy link to clipboard
Copied
This is a basic text with fairly simple examples:
https://creativepro.com/product/javascript-for-indesign-2nd-edition/
The next one is more technical. It has CS6 in its title but virtually everything is still relevanr:
https://applescriptlibrary.files.wordpress.com/2013/11/indesign-cs6-server-scripting-guide.pdf
On this page (at the bottom) you'll find some more links to InDesign scripting textx:
https://www.typefi.com/resources/do-more-with-scripting/scripting-resources/
None of these have much reflection on what those objects really are, they show examples and you'll have to work out what they really mean..
Marc Autret's site (https://www.indiscripts.com/) has many articles on InDesign's object model. Maybe a bit technical for the beginner, but interesting nevertheless.
The best way to learn how to script is to try and do it, and come to this forum (or similar forums that specialise in InDesign scripting) if you have a problem.
P.
Copy link to clipboard
Copied
Oh great, perfect!! Thanks again @Peter Kahrel!!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more