Highlighted

Open InDesign files from Subfolders using a variable

Contributor ,
May 01, 2018

Copy link to clipboard

Copied

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; 

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);

        }

TOPICS
Scripting

Views

371

Likes

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

Open InDesign files from Subfolders using a variable

Contributor ,
May 01, 2018

Copy link to clipboard

Copied

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; 

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);

        }

TOPICS
Scripting

Views

372

Likes

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
May 01, 2018 0
Valorous Hero ,
May 01, 2018

Copy link to clipboard

Copied

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);

        }

Likes

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
Reply
Loading...
May 01, 2018 2
Contributor ,
May 02, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
May 02, 2018 0
Valorous Hero ,
May 02, 2018

Copy link to clipboard

Copied

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

            files.push(file);

        }

Likes

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
Reply
Loading...
May 02, 2018 2
Eric_R1 LATEST
Community Beginner ,
Jul 07, 2020

Copy link to clipboard

Copied

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

 

Because the leading 0 tells javascript, "the following number is in base-8 instead of base-10", and 60 in octal == 48 in decimal. (This is a feature / pitfall common to a number of programming languages.)

 

As you've discovered, if you're planning to compare a variable to other text - like a filename - it's generally better to define the variable as a string than as a number so you're dealing with characters-typed-on-a-keyboard rather than mathematics.

 

(Sorry to necro an already-answered thread, but I saw the leading-0 question and figured it might be useful to have an answer on-page for any other folks who might run across this.)

Likes

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
Reply
Loading...
Jul 07, 2020 2