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

Set document title from entry in spreadsheet

Explorer ,
Dec 04, 2023 Dec 04, 2023

I've got a task to enter Document Titles of thousands of InDesign files based on entries in a spreadsheet. The spreadsheet will show the current filename and the corresponding Document title.

 

Is it possible to script this? Can a script, like, search within an external document, find it's own filename in the first column and then set the Document Title based on the entry in the second column?

 

Failing that, I think I'd do it in two steps which I know are possible:

1) Outside of InDesign, batch-rename all the files to the document title.

2) Within InDesign, change the Document Title to the filename.

3) Outside of InDesign, batch-rename all the files back to original filename. 

 

It just seems like there might be a more efficient way to do it

 

Thoughts?

TOPICS
Scripting
269
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

correct answers 1 Correct answer

Community Expert , Dec 04, 2023 Dec 04, 2023

Hi @dona56558947, yes that can be done. I've written a script that attempts it. The script tries to load a csv file (currently looks in the same folder as the script file, but you can change this of course) called "Document Titles.csv" (you can change this, too). Document Titles.csv looks something like this:

Screenshot 2023-12-05 at 07.13.15.png

You can make this in a spreadsheet or text editor. If there are commas in the Document Titles, you can use a tab character as the delimiter rather than the comma I used. The script doesn't

...
Translate
Community Expert ,
Dec 04, 2023 Dec 04, 2023

Hi @dona56558947, yes that can be done. I've written a script that attempts it. The script tries to load a csv file (currently looks in the same folder as the script file, but you can change this of course) called "Document Titles.csv" (you can change this, too). Document Titles.csv looks something like this:

Screenshot 2023-12-05 at 07.13.15.png

You can make this in a spreadsheet or text editor. If there are commas in the Document Titles, you can use a tab character as the delimiter rather than the comma I used. The script doesn't properly parse CSV, it just splits it up using a delimiter, which is currently every tab or comma but you can change this.

 

The script loads this CSV, then it looks to see if any of those documents are open in Indesign and, if they are, will set their document titles and save/close them. It will not close any documents that aren't matched in the CSV file.

 

Let me know if this is going to work for you.

- Mark

 

/**
 * Reads a CSV file where each line has two fields:
 * (1) document name, including extension, and
 * (2) document title.
 * If the document named (1) is open, then set its
 * XMP document title to (2) and save/close it.
 * It will look for the csvFile in the same folder
 * as this script.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/set-document-title-from-entry-in-spreadsheet/m-p/14276685
 */
(function () {

    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

    // the csv file name
    var csvFileName = 'Document Titles.csv',

        // the same folder as this script
        csvFolderPath = File($.fileName).parent + '/',

        // fields delimiter (comma or tab)
        csvDelimiter = /[,\t]/g,

        // read the file
        csv = readTextFile(csvFolderPath + csvFileName);

    if (!csv)
        return alert('Could not read file "' + csvFileName + '".');

    // divide into rows
    csv = csv.split('\n');

    var counter = 0,
        fields,
        docName,
        doc;

    // loop over each row:
    csvRowLoop:
    for (var i = 0, len = csv.length; i < len; i++) {

        fields = csv[i].split(csvDelimiter)
        docName = fields[0];


        docTitle = fields[1];
        if (!(docName && docTitle))
            continue csvRowLoop;

        doc = app.documents.itemByName(docName);

        if (!doc.isValid)
            continue csvRowLoop;

        // if document found in csv file is open,
        // set its title and close/save it
        doc.metadataPreferences.documentTitle = docTitle;
        doc.close(SaveOptions.YES)

        counter++

    }

    alert('Set ' + counter + ' document titles.');

})();

/**
 * Returns contents of text file.
 * @param {File|String} f - the file or path.
 * @returns {String}
 */
function readTextFile(f) {

    if (f.constructor.name == 'String')
        f = File(f);

    if (!f || !f.exists)
        return;

    f.open('r');
    var data = f.read();
    f.close();

    return data;

};
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
Explorer ,
Dec 04, 2023 Dec 04, 2023
LATEST

Yes! Yes! This looks like it will work!

 

I had overcomplicated it thinking I would need to some kind of search, but certainly a script could run through thousands of lines and compare for a match in the blink of an eye.

 

Thanks a million!

 

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