1

I'd like to get InDesign to insert the first few digits of a filename automatically in a document created from a template.

Our filenames are in this format:

XXXXX_Name1_Name2_Name3.indd

The Xs before the underscore represent the job number. It can be wither 4 or 5 digits. I know InDesign can automatically place the entire filename. I'd just like it to insert the job number portion.Is there a way to do this either with GREP styles or (preferably) some sort of scripting? Ideally, it would require little to no input from the user.

Is this even possible?

2
  • Do you have one filename to be inserted for one new document or do you need to insert more than one filenames to one (new) document? I guess that this question should be on a side with scripting expertise ... Nevertheless, Welcome to GD.SE!
    – Mensch
    Nov 17, 2014 at 15:50
  • Thanks, Kurt - It's just one filename. It's a template, so it will open as 'Untitled-1.' Then it will be saved into the format I listed above. Does that help? (And do you know of any scripting resources where I could find someone to create this script?) Nov 17, 2014 at 15:52

1 Answer 1

2

Using Text Variables, you cannot get the built-in type "File Name" to show only part of the actual file name; it's all or nothing. You can create a "Custom Text" variable and assign it anything you want, but you have to remember to update it with the job number part of the filename. Only the latter can be automated with a Javascript, and so you still need to run the script every time you save the document under a new name. InDesign keeps on surprising me, it wasn't half as hard as I initially believed...

Save the below script as UpdateJobNumber.jsx in the Startup Scripts folder in your Scripts Panel folder; you will need to restart InDesign for it to take effect. In your template, create a new variable called JobNumber and place it anywhere you like. Henceforth, after saving a document under a new name that starts with one or more digits, the text variable JobNumber will automagically be updated!

...GREP styles have absolutely nothing to do with this. I know: GREP may seem to be able to do magic, at times. But it cannot do anything. This, for example, is way, way out of its scope.

//DESCRIPTION:Update Job Number Variable
// Another Jongware Script 18-Nov-2014
#targetengine "updateJobNumber"

app.addEventListener("afterSaveAs", updateJobNumber, false); 

function updateJobNumber ()
{
    if (app.activeDocument.saved)
    {
        varRef = app.activeDocument.textVariables.item("JobNumber");
        if (varRef.isValid)
        {
            fileId = app.activeDocument.fullName.name.match(/^\d+/);
            if (fileId)
            {
                if (varRef.variableOptions.contents != fileId[0])
                {
                    varRef.variableOptions.contents = fileId[0];
                    app.activeDocument.save();
                }
            }
        }
    }
}

(Here is my older script, which could still be useful to update manually. Ordinarily you would not need it.)

//DESCRIPTION:Fill in Job Number Variable
// A Jongware Script 18-Nov-2014
if (app.activeDocument.saved)
{
    varRef = app.activeDocument.textVariables.item("JobNumber");
    if (!varRef.isValid)
            varRef = app.activeDocument.textVariables.add({name:"JobNumber"});
    fileId = app.activeDocument.fullName.name.match(/^\d+/);
    if (fileId)
        varRef.variableOptions.contents = fileId[0];
    else
        varRef.variableOptions.contents = "<INVALID>";
}

(After writing the above) I just thought of a nice alternative trick using the native "File Name" Text Variable. But it will only work (nicely) if (1) the job number always appears at the same place, (2) in a font where digits are all the same width, and (3) you always would have the same number of digits (which you don't, as I understand it).

Insert a File Name text variable in a text frame large enough to contain the entire name. Draw a rectangle on top of the part you want to see--the digits. Cut the text frame and Paste Into the rectangle.

The advantage is this will automagically get updated by InDesign itself after each save. A major disadvantage, however, is that it's unaware of the number of digits in the file name; so if these happen to be more than you expected, the last ones will be lost, and if less, you will see a part of the rest of the file name.

Not the answer you're looking for? Browse other questions tagged or ask your own question.