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

Script to Batch Rename as Title Case

Community Expert ,
Dec 26, 2023 Dec 26, 2023

This comes up from time to time due to the shortcoming of Bridge Batch Renaming having only UPPERCASE or lowercase options. It is possible to create a Regular Expression based Batch Rename to create Title Case , however, it isn't very flexible and can be complex to setup.

 

The following script simplifies the process of renaming selected files to use a simple Title Case naming convention.

 

For example, the following original filenames:

 

the quick brown fox jumps over the lazy dog.psd

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.psd

 

Would be transformed to:

 

The Quick Brown Fox Jumps Over The Lazy Dog.psd

 

/*
Rename to Title Case.jsx
v1.0 - 26th December 2023, Stephen Marsh
https://community.adobe.com/t5/bridge-discussions/script-to-batch-rename-as-title-case/td-p/14319096
*/

#target bridge

if (BridgeTalk.appName == "bridge") {
   var titleCase = new MenuElement("command", "Rename to Title Case v1.0", "at the end of Tools");
}

titleCase.onSelect = function () {
   var theFiles = app.document.selections;
   for (var a = 0; a < theFiles.length; a++) {

      // Add the PreservedFileName metadata
      var selectedFile = theFiles[a].spec;
      var theMD = new Thumbnail(selectedFile).synchronousMetadata;
      theMD.namespace = "http://ns.adobe.com/xap/1.0/mm/";
      var theName = decodeURI(selectedFile.name).replace(/\.[^\.]+$/, '');
      theMD.PreservedFileName = theName;

      // Rename the file to Title Case
      var selsName = decodeURI(app.document.selections[a].spec.name.replace(/\.[^\.]+$/, ''));
      var selsExt = decodeURI(app.document.selections[a].spec.name.replace(/(^.+)(\.[^\.]+$)/, '$2'));
      app.document.selections[a].name = toTitleCase(selsName) + selsExt;
   }

   function toTitleCase(theText) {
      var words = theText.toLowerCase().split(' ');
      for (var i = 0; i < words.length; i++) {
         words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
      }
      return words.join(' ');
   }
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

TOPICS
Scripting
464
Translate
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 ,
Apr 25, 2025 Apr 25, 2025

Here's an updated 1.1 version, to specifically handle prefixes such as "Mc" or "Mac".

/*
Rename to Title Case 1-1.jsx
v1.0 - 26th December 2023, Stephen Marsh
v1.1 - 25th April 2025, Added support for "Mc" and "Mac" prefixes
https://community.adobe.com/t5/bridge-discussions/script-to-batch-rename-as-title-case/td-p/14319096
*/

#target bridge

if (BridgeTalk.appName == "bridge") {
    var titleCase = new MenuElement("command", "Rename to Title Case v1.1", "at the end of Tools");
}

titleCase.onSelect = function () {
    var theFiles = app.document.selectionsLength;
    for (var a = 0; a < theFiles; a++) {
        var selectedFile = app.document.selections[a].spec;
        var selsName = decodeURI(selectedFile.name.replace(/\.[^\.]+$/, ''));
        var selsExt = decodeURI(selectedFile.name.replace(/(^.+)(\.[^\.]+$)/, '$2'));

        // Rename the file
        app.document.selections[a].name = toTitleCase(selsName) + selsExt;

        // Add the PreservedFileName metadata
        var theMD = new Thumbnail(selectedFile).synchronousMetadata;
        theMD.namespace = "http://ns.adobe.com/xap/1.0/mm/";
        var theName = decodeURI(selectedFile.name).replace(/\.[^\.]+$/, '');
        theMD.PreservedFileName = theName;
    }
};

function toTitleCase(theText) {
    var words = theText.toLowerCase().split(' ');
    for (var i = 0; i < words.length; i++) {
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);

        // Handle special prefixes
        if (words[i].match(/^Mc[a-z]/i) && words[i].length > 2) {
            words[i] = 'Mc' + words[i].charAt(2).toUpperCase() + words[i].slice(3);
        } else if (words[i].match(/^Mac[a-z]/i) && words[i].length > 3) {
            words[i] = 'Mac' + words[i].charAt(3).toUpperCase() + words[i].slice(4);
        }
    }
    return words.join(' ');
}

 

Translate
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 ,
Apr 25, 2025 Apr 25, 2025

This 1.2 version attempts to expand on the original simple Title Case renaming with conditional processing for multinational names. Best to work on duplicates of the original files, just in case there are unexpected results:

 

/*
Rename to Multinational Title Case 1-2.jsx
v1.0 - 26th December 2023, Stephen Marsh
v1.1 - 25th April 2025, Added support for "Mc" and "Mac" prefixes
v1.2 - 25th April 2025, Added additional support for other common multinational name prefixes
*/

#target bridge

if (BridgeTalk.appName == "bridge") {
    var titleCase = new MenuElement("command", "Rename to Multinational Title Case v1.2", "at the end of Tools");

    titleCase.onSelect = function () {
        var theFiles = app.document.selections;
        if (app.document.selectionsLength > 0) {
            for (var i = 0; i < app.document.selectionsLength; i++) {

                // Add the PreservedFileName metadata
                var selectedFile = theFiles[i].spec;
                var theMD = new Thumbnail(selectedFile).synchronousMetadata;
                theMD.namespace = "http://ns.adobe.com/xap/1.0/mm/";
                var theName = decodeURI(selectedFile.name).replace(/\.[^\.]+$/, '');
                theMD.PreservedFileName = theName;

                var currentFile = app.document.selections[i];
                var fullName = decodeURI(currentFile.spec.name);
                var baseName = fullName.replace(/\.[^\.]+$/, '');
                var extension = fullName.substring(fullName.lastIndexOf('.'));

                // Apply multicultural title case to the basename
                var newName = applyMulticulturalTitleCase(baseName) + extension;

                // Rename the file
                currentFile.name = newName;
            }
            alert("Renamed " + app.document.selectionsLength + " files");
        } else {
            alert("Please select at least one file to rename");
        }
    };
}

// Main function to apply multicultural title case rules
function applyMulticulturalTitleCase(text) {
    // Convert to lowercase and split by spaces
    var words = text.toLowerCase().split(' ');

    // Process each word
    for (var i = 0; i < words.length; i++) {
        var word = words[i];

        // First check if the word contains a hyphen
        if (word.indexOf("-") !== -1) {
            var hyphenParts = word.split("-");

            // Process each part of the hyphenated word individually
            for (var j = 0; j < hyphenParts.length; j++) {
                var part = hyphenParts[j];

                // Skip empty parts
                if (part.length === 0) continue;

                // Basic capitalization
                part = part.charAt(0).toUpperCase() + part.slice(1);

                // Apply special rules for name prefixes
                if (part.indexOf("Mc") === 0 && part.length > 2) {
                    part = "Mc" + part.charAt(2).toUpperCase() + part.slice(3);
                }
                else if (part.indexOf("Mac") === 0 && part.length > 3) {
                    part = "Mac" + part.charAt(3).toUpperCase() + part.slice(4);
                }
                else if (part.indexOf("O'") === 0 && part.length > 2) {
                    part = "O'" + part.charAt(2).toUpperCase() + part.slice(3);
                }

                // Handle apostrophes within a hyphenated part
                if (part.indexOf("'") !== -1 && part.indexOf("'") > 0) {
                    var apostParts = part.split("'");
                    if (apostParts[0].length === 1) { // Single letter prefix
                        apostParts[0] = apostParts[0].toUpperCase();
                        if (apostParts[1].length > 0) {
                            apostParts[1] = apostParts[1].charAt(0).toUpperCase() + apostParts[1].slice(1);
                        }
                        part = apostParts.join("'");
                    }
                }

                hyphenParts[j] = part;
            }

            word = hyphenParts.join("-");
        }
        else {
            // Basic title case - capitalize first letter
            word = word.charAt(0).toUpperCase() + word.slice(1);

            // Apply special rules for various name formats

            // "Mc" prefix (i.e., "McGregor")
            if (word.indexOf("Mc") === 0 && word.length > 2) {
                word = "Mc" + word.charAt(2).toUpperCase() + word.slice(3);
            }

            // "Mac" prefix (i.e., "MacDonald")
            else if (word.indexOf("Mac") === 0 && word.length > 3) {
                word = "Mac" + word.charAt(3).toUpperCase() + word.slice(4);
            }

            // "O'" prefix (i.e., "O'Neill")
            else if (word.indexOf("O'") === 0 && word.length > 2) {
                word = "O'" + word.charAt(2).toUpperCase() + word.slice(3);
            }

            // Handle apostrophes (like D'Angelo)
            if (word.indexOf("'") !== -1 && word.indexOf("'") > 0) {
                var parts = word.split("'");
                if (parts[0].length === 1) { // Single letter prefix
                    parts[0] = parts[0].toUpperCase();
                    if (parts[1].length > 0) {
                        parts[1] = parts[1].charAt(0).toUpperCase() + parts[1].slice(1);
                    }
                    word = parts.join("'");
                }
            }
        }

        // Handle Spanish/Portuguese/Italian/etc. prefixes
        if (i > 0) { // Only if not the first word
            if (word === "De" || word === "Del" || word === "La" ||
                word === "Van" || word === "Von" || word === "Der" ||
                word === "Y" || word === "E" || word === "Da" ||
                word === "Dos" || word === "Du" || word === "Des") {
                word = word.toLowerCase();
            }
        }

        words[i] = word;
    }

    return words.join(' ');
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below, scroll down to the section for Adobe Bridge)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Translate
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
Enthusiast ,
Apr 25, 2025 Apr 25, 2025

You can also put all of those special cases into an array and loop through it for testing. That's much easier to maintain and read.

I might just use an awk or sed script called with system() since those are pretty fast and compact.

Translate
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 ,
Apr 25, 2025 Apr 25, 2025

@ExUSA 

 

Thank you for the tip! I keep breaking the script though...

Translate
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
Enthusiast ,
Apr 25, 2025 Apr 25, 2025

I'll work on some sample code when I have time.

Translate
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
Enthusiast ,
Apr 25, 2025 Apr 25, 2025

This still needs work (and testing!) but the bones are there. It should streamline things a bit.


/*
Rename to Multicultural Title Case 1-2.jsx
v1.0 - 26th December 2023, Stephen Marsh
v1.1 - 25th April 2025, Added support for 'Mc' and 'Mac' prefixes
v1.2 - 25th April 2025, Added additional support for other common multi-cultural name prefixes
*/

#target bridge

if (BridgeTalk.appName == 'bridge') {
    var titleCase = new MenuElement('command', 'Rename to Multicultural Title Case v1.2', 'at the end of Tools');

    titleCase.onSelect = function () {
        var theFiles = app.document.selections;
        if (app.document.selectionsLength > 0) {
            for (var i = 0; i < app.document.selectionsLength; i++) {

                // Add the PreservedFileName metadata
                var selectedFile = theFiles[i].spec;
                var theMD = new Thumbnail(selectedFile).synchronousMetadata;
                theMD.namespace = 'http://ns.adobe.com/xap/1.0/mm/';
                var theName = decodeURI(selectedFile.name).replace(/\.[^\.]+$/, '');
                theMD.PreservedFileName = theName;

                var currentFile = app.document.selections[i];
                var fullName = decodeURI(currentFile.spec.name);
                var baseName = fullName.replace(/\.[^\.]+$/, '');
                var extension = fullName.substring(fullName.lastIndexOf('.'));

                // Apply multicultural title case to the basename
                var newName = applyMulticulturalTitleCase(baseName) + extension;

                // Rename the file
                currentFile.name = newName;
            }
            alert('Renamed ' + app.document.selectionsLength + ' files');
        } else {
            alert('Please select at least one file to rename');
        }
    }
}

// Main function to apply multicultural title case rules
function applyMulticulturalTitleCase(text) {
    var terms = ['Mc', 'Mac', 'O\'', 'De', 'Del', 'La', 'Van', 'Von', 'Der', 'Y', 'E', 'Da', 'Dos', 'Du', 'Des'];
    var currTerm = '';
    var newWord = '';
    // Convert to lowercase and split by spaces
    var words = text.toLowerCase().split(' ');

    // Process each word
    for (var i = 0; i < words.length; i++) {
        var word = words[i];

        // Basic title case - capitalize first letter
        word = word.charAt(0).toUpperCase() + word.slice(1);
        newWord = newWord + ' '  + word;

        // Apply special rules for various name formats
        for(var j = 0; j < 3; j++){
            currTerm = terms[j];
            if(word.search(currTerm) != -1){
                word = terms[j] + word.charAt(terms[j].length).toUpperCase() + word.slice(terms[j].length + 1);
                }
            }
        }
        // Handle Spanish/Portuguese/Italian/etc. prefixes
        for(var j = 3; j < terms.length; j++){
            currTerm = terms[j];
            if(word.search(currTerm) != -1){
                word = word.toLowerCase();
                }
            }
        // Handle hyphenated names
        if (word.indexOf('-') !== -1) {
            var parts = word.split('-');
            for (var j = 0; j < parts.length; j++) {
                if (parts[j].length > 0) {
                    parts[j] = parts[j].charAt(0).toUpperCase() + parts[j].slice(1);
                }
            }
            word = parts.join('-');
        }

        // Handle apostrophes (like D'Angelo)
        if (word.indexOf(''') !== -1 && word.indexOf(''') > 0) {
            var parts = word.split(''');
            if (parts[0].length === 1) { // Single letter prefix
                parts[0] = parts[0].toUpperCase();
                if (parts[1].length > 0) {
                    parts[1] = parts[1].charAt(0).toUpperCase() + parts[1].slice(1);
                }
                word = parts.join(''');
            }
        words[i] = word;
    return newWord;
}

 

Translate
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 ,
Apr 25, 2025 Apr 25, 2025
LATEST
quote

This still needs work (and testing!) but the bones are there. It should streamline things a bit.

 


By ExUSA

 

Thank you, I'll look over your example! I just updated my previous post, as hyphenated-names were not processed as expected.

Translate
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