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

Custom Text Variable for file name

Community Beginner ,
Jul 04, 2024 Jul 04, 2024

Copy link to clipboard

Copied

Hi all,

Currently now working on two systems at work each which have different job-numbering requrements.

We currently have an Indesign Startup script that gets the job number of the active document and places it as a text variable within a custom slug.

 

Example of current file name "000000-0_XX_PublicationName_250x150.indd" The job number is anything before the first underscore.

Here is the current code we are using...

function getJobNumber() {
    try {
        if (app.documents.length>0) {
            var xdoc = app.activeDocument;
            if (xdoc.isValid) {
                var xdocname = app.activeDocument.name;
                var xjobnumber = xdocname.split("_")[0];
                if (xjobnumber === xdocname) { 
                    xjobnumber = "no job number";
                } else {
                    var xnumbercheck = Number(xjobnumber.split("-")[0]);
                    if (isNaN(xnumbercheck)) { xjobnumber = "no job number"};
                }
            }
        } else {
            var xjobnumber = "no job number";
        }
    } catch(err) {
        var xjobnumber = "no job number";
    }
    return xjobnumber;
}

 

Tthe new system however requires us to have this naming convention "UK-000000-0_XX_PublicationName_250x150.indd" but the current script will not work with the "UK-" at the beginning.

It is important that this script works both ways and was wondering if there is a simple solution to this?

TOPICS
Scripting

Views

158

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 ,
Jul 04, 2024 Jul 04, 2024

Copy link to clipboard

Copied

Can't you ignore first 3x characters?

 

But if there is "UK-" at the beginning - shouldn't that be still included in your workflow?

I'm pretty sure you can't just remove "UK-" as then you'll have duplicated files - and probably more than two - other "countries"?

 

If you just need numeric part of the jobnumber - you could split it again - by "-" - and take 2nd result.

 

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 ,
Jul 04, 2024 Jul 04, 2024

Copy link to clipboard

Copied

Here's an update to the script. Note my commented line where I made the change. 

function getJobNumber() {
    
    try {
        if (app.documents.length>0) {
            var xdoc = app.activeDocument;
            if (xdoc.isValid) {
                var xdocname = app.activeDocument.name;
                //you could add other country codes to this regex using | char
                if (/^UK/g.test(xdocname)) {
                    xdocname = xdocname.slice(3);
                }
                var xjobnumber = xdocname.split("_")[0];
                if (xjobnumber === xdocname) { 
                    xjobnumber = "no job number";
                } else {
                    var xnumbercheck = Number(xjobnumber.split("-")[0]);
                    if (isNaN(xnumbercheck)) { xjobnumber = "no job number"};
                }
            }
        } else {
            var xjobnumber = "no job number";
        }
    } catch(err) {
        var xjobnumber = "no job number";
    }
    return xjobnumber;
}

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 ,
Jul 05, 2024 Jul 05, 2024

Copy link to clipboard

Copied

Hi Brian,

Thank you for the reply, this is exaclty what i'm looking for, although is it possible to have the "UK-" displayed when the variable is returned?

Sorry if that wasnt too clear last time. but in some cases I need to display the job number as 123456-7 for example and now also need to display it as UK-123456-7. This is all dependant on which system we pick a job up from.

Sorry if this is a simple fix. I had a guy teaching me some basic javascript for a little while but he has now left and I am a little stuck with this one.

It is possible that the country code may need to change down the line, but for now im only aware that is needs to be UK-

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 ,
Jul 05, 2024 Jul 05, 2024

Copy link to clipboard

Copied

LATEST

Hi @brianp311 ,

Thank you for the reply, this is exaclty what i'm looking for, although is it possible to have the "UK-" displayed when the variable is returned?

Sorry if that wasnt too clear last time. but in some cases I need to display the job number as 123456-7 for example and now also need to display it as UK-123456-7. This is all dependant on which system we pick a job up from.

Sorry if this is a simple fix. I had a guy teaching me some basic javascript for a little while but he has now left and I am a little stuck with this one.

It is possible that the country code may need to change down the line, but for now im only aware that is needs to be UK-

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 ,
Jul 04, 2024 Jul 04, 2024

Copy link to clipboard

Copied

Hi @Scott1992, if I understand you right, then a good option might be to use a RegExp (grep) to match the job number because it handles the digits check automatically. Also, if you know RegExp, it is much more readable. So I would write the function like this:

 

function getJobNumber(doc) {

    const matchJobNumber = /(^(UK)?[-\d]+)_/;

    var match = doc.name.match(matchJobNumber);

    if (match && match.length > 1)
        return match[1];

};

 

 

And you could incorporate this function into your script; something like this, for example:

 

function main() {

    if (0 === app.documents.length)
        return alert('No documents open.');

    var doc = app.activeDocument;
    var jobNumber = getJobNumber(doc);

    if (jobNumber)
        alert('Job number is "' + jobNumber + '".');
    else
        alert('Document has no job number.');

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Show job number');

 

 

You already have a good answer from @brianp311, but this in another approach, just for learning if nothing else. 🙂

 - Mark

 

Edit: simplified regexp a little.

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 ,
Jul 05, 2024 Jul 05, 2024

Copy link to clipboard

Copied

Again, just for additional learning, here is a RegExp approach that captures the different parts of the job number:

function main() {

    if (0 === app.documents.length)
        return alert('No documents open.');

    var doc = app.activeDocument;
    var jobNumber = getJobNumber(doc);


    if (jobNumber)
        alert(
            'Job number is "' + jobNumber.fullCode + '".'
            + '\nCountry Code is "' + jobNumber.countryCode + '".'
            + '\nNumber Code is "' + jobNumber.numberCode + '".'
        );
    else
        alert('Document has no job number.');

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Show job number');


function getJobNumber(doc) {

    const matchJobNumber = /(^(?:([A-Z]{2})-?)?([-\d]+))_/;

    var match = doc.name.match(matchJobNumber);

    if (match && match.length > 1)
        return {
            fullCode: match[1],
            countryCode: match[2],
            numberCode: match[3],
        };

};

 Try running this script with your document(s) active and see if it gets the job number right.. This will work with any 2-letter country code, or none.

- Mark

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 ,
Jul 05, 2024 Jul 05, 2024

Copy link to clipboard

Copied

Hi @m1b 

Thank you for this, i really appreciate any help 🙂

I will give this a go shortly

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