Skip to main content
May 1, 2018
Répondu

Open InDesign files from Subfolders using a variable

Hi all,

I have a script that opens any InDesign documents that have "0050" in the name ( Thanks to Kasyan Servetsky for the original ).

My problem is that the larger script it will be incorporated into has a variable called "pgNum" that controls which page number needs to be opened (not always page 50) so I need to replace the "0050" with the "pgNum" variable.

I've spent quite a while chopping and changing, then going back to the original but cannot find a way to do it, if anyone could help that would be fantastic .

Many Thanks, Bren

var pgNum = 0060

var files; 

var folder = Folder.selectDialog("Select a folder with InDesign documents"); 

if (folder != null) { 

    files = GetFiles(folder); 

    if (files.length > 0) { 

        // turn off warnings: missing fonts, links, etc. 

        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT; 

 

        for (i = 0; i < files.length; i++) { 

            app.open(files); 

        } 

        // turn on warnings 

        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL; 

    } 

    else { 

        alert("Found no match"); 

    } 

 

function GetFiles(theFolder) { 

    var files = [], 

    fileList = theFolder.getFiles(), 

    i, file; 

 

    for (i = 0; i < fileList.length; i++) { 

        file = fileList

        if (file instanceof Folder) { 

            files = files.concat(GetFiles(file)); 

        } 

        else if (file instanceof File && file.name.match(/0050.*\.indd$/i)) {  // ******** NEED TO CHANGE "0050" TO THE VARIABLE pgNum ********

            files.push(file); 

        } 

    } 

 

    return files; 

Ce sujet a été fermé aux réponses.
Meilleure réponse par Loic.Aigon

else if (file instanceof File && file.name.match(/0050.*\.indd$/i)) {  // ******** NEED TO CHANGE "0050" TO THE VARIABLE pgNum ********

            files.push(file);

        }

to

else if (file instanceof File && file.name.match(new RegExp(pageNum+".*\\.indd$", "i"))) { 

            files.push(file);

        }

or

else if (file instanceof File && file.name.indexOf ( pageNum ) > -1 )) {

            files.push(file);

        }

2 commentaires

May 2, 2018

Thank you so much,

I used your first option as I couldn't get the second option wouldn't work, I added an "open bracket" after ".indexOf" but when I ran the script it kept trying to open the ".DS_Store" file and error'ing.

I had one other problem with my script which was I had to change the Var pageNum (pgNum) to have quotation marks around the number to make it work correctly, I found some strange behaviour when I didn't, which I've listed below:

var pageNum = 0060 // Opens page 0048 _ No idea why?

var pageNum = 60 // Opens page 0060, 0160, 0260 etc. _ Obviously will open any file with 60 in the name... Doh!

var pageNum = "0060" // Opens page 0060 _ Works like a dream

This now works perfectly

Many Thanks, Bren

Loic.Aigon
Legend
May 2, 2018

else if (file instanceof File && file.name.match(new RegExp(String(pageNum)+".*\\.indd$", "i"))) {

            files.push(file);

        }

Loic.Aigon
Loic.AigonRéponse
Legend
May 1, 2018

else if (file instanceof File && file.name.match(/0050.*\.indd$/i)) {  // ******** NEED TO CHANGE "0050" TO THE VARIABLE pgNum ********

            files.push(file);

        }

to

else if (file instanceof File && file.name.match(new RegExp(pageNum+".*\\.indd$", "i"))) { 

            files.push(file);

        }

or

else if (file instanceof File && file.name.indexOf ( pageNum ) > -1 )) {

            files.push(file);

        }