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

searching directories with javascript

Participant ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

Hello all,

I posted this in the indesign forum because it is a script for that program but since the actual problem is javascript conundrum, and requires no knowledge of indesign, I thought i would post it here too. Lot a smart folks.

I am trying to write a script to automate relinking broken links.

The idea is to make a list of all broken links, then search for those in a particular directory. There is no easy way of searching a directory with javascript that i know of.

I tried making an array populated with all ai files in a particular tree structure but i hit stack overflow rather quickly.

Any thoughts about how one could search in directories?

below is what i have:

var idDoc = app.activeDocument

var broke = []

var foundFiles = []

var folds = []

var started = false

for (var i=0; i < idDoc.links.length; i++){

    var a = idDoc.links

    if (a.status == LinkStatus.LINK_MISSING ){

        broke.push(a.name)

    }

}

if (broke.length >= 0){

    var searchFolder =Folder("Q:\Kollektion_Files")

    var folderCount = []

    getFilesFromFolder(searchFolder)

    fix(broke, foundFile)

    }

function getFilesFromFolder(curfolder){

   

    if (started == true){

        folds.splice(0,1)

        }

    var files = curfolder.getFiles();

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

        if (files instanceof Folder){

            var entry = files

            folds.push(entry)

            }

        }

    var curFile; 

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

        curFile = files

        if(curFile instanceof File && validFile(curFile)){

            foundFiles.push(curFile);

        }

    }

    started = true

    var len = folds.length

    if (folds.length >=0){

    getFilesFromFolder(folds[0])

    }

}

Also i notice that when other people post scripts in this forum the scripts look just like they do in a compiler. How does one do that?

Thanks.

Dane

TOPICS
Scripting

Views

929

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
Adobe
Community Expert ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

For the second part:

Re: Changing the text in a scriptUI window

(But I prefer javascript highlightning instaed of Java)

The first part should be moved to the Forum InDesign Scripting

There is a different handling between Illustrator and InDesign

Have fun

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
Participant ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

Hello again.

I did post this quiry in the indesign forum however actually the indesign coding of the script is very basic stuff. I am not having problems with it.

It is using javascript to locate files in directories on my hard drive that is problematic. I adapted a recursive script to try to build and object with all relevent file paths to be checked through but obvously it does not take long to reach stack overflow.

Since the question is "übergreifend" so to speak, I posted it here too.

Thanks for the link. perhaps it is only asthetic but my copy paste scripts bothered me.

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 ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

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
Participant ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

Thanks for the help but they all rely on dialogs, user imput. I want a to say go and watch as broken links are magically repaired.

I think i might be on to something. I am parsing the broken links to help direct the populating of directory array.

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
Valorous Hero ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

If there's a stack overflow exception with recursion, my thoughts are that a recursive algorithm is doing something unexpected. One way to help with dealing with recursion for files, provided there's not zillions of files, is to separate the recursive gathering from regular gathering. In this instance, it may be helpful to first recursively gather folders into a linear array, and then analyze files in the folders as a second step.

Maybe the folds.splice() isn't working for you as it should.

So, my advice is to get all folders in an array first and then go through the files in folders in that array to isolate the ones you need to process (maybe even do the fixBroke right there too).

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
Participant ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

Hello Silly-V

That is what i was trying to do with:

    var files = curfolder.getFiles();

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

        if (files instanceof Folder){

            var entry = files

            folds.push(entry)

            }

        }

Am i wrong in thinking this makes an array of all possible folders?

I dont have a zillion files of folders but there is over a thousand folders and 5000 files. This may be the main problem.

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
Valorous Hero ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

Well, maybe it is also having to do with the scope of the search.

#target illustrator

function test(){

  //var allFolders = [];

  var counter = 0;

  function getAllFolders(curfolder){

    var files = curfolder.getFiles();

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

        if (files instanceof Folder){

            var entry = files;

            //allFolders.push(decodeURI(entry));

            counter++;

            getAllFolders(entry);

        }

      }

  };

  var curFolder = Folder.selectDialog("Choose Folder");

  if(!curFolder){

    return;

  }

  getAllFolders(curFolder);

  //$.write(allFolders.toSource());

  alert(counter);

};

test();

I ran this code on my Desktop and got an alert of 4866 (number of folders), and it took like a good minute.

A problem I encountered was that after a certain amount, the ESTK $.write() ceases to work, - or .push(folderObject) does - so I changed to a counter and was able to at least determine script ran to completion, that way.

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
Participant ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

LATEST

Yes It think it must have been the scope.

I got the script to work.

I narrowed the search down to much smaller field by peicing together bits of the broken links.

I have pasted the script below. near the top are comments where I narrowed the search.

Thanks for all your input and to pixxxel schubser​.

var idDoc = app.activeDocument

var broke = []

var foundFiles = []

var folds = []

var started = false

for (var i=0; i < idDoc.links.length; i++){

    var a = idDoc.links

    if (a.status == LinkStatus.LINK_MISSING ){

       

        //here i parsed the broken path and used parts of it im my searchFolder path.

        //my directory was much smaller and more targeted and therefore does not have a stack overflow

        var parse = a.filePath.split("\\")

        var gender = parse[2]+ "\\"

        var prodGroup = parse[3]

        broke.push(a.name)

    }

}

if (broke.length >= 0){

    var searchFolder =Folder("Q:\Kollektion_Files\\" + gender + prodGroup)

    getFilesFromFolder(searchFolder)

    fix(broke, foundFiles)

    }

function getFilesFromFolder(curfolder){

   

    if (started == true){

        folds.splice(0,1)

        }

    var files = curfolder.getFiles();

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

        if (files instanceof Folder){

            var entry = files

            folds.push(entry)

            }

        }

    var curFile; 

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

        curFile = files

        if(curFile instanceof File && validFile(curFile)){

            foundFiles.push(curFile);

           

        }

    }

    started = true

    var len = folds.length

    if (folds.length >> 0){

    getFilesFromFolder(folds[0])

    }

}

function fix(linkList, fileList){

for (var t = 0; t < linkList.length; t++){

    var fixLink = linkList.substring(0, 9)

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

        if(fileList.displayName.search(fixLink) >= 0){

            var theLink = idDoc.links.itemByName(linkList)

            theLink.relink(fileList)

            break;

            }

      }

    } 

function validFile(file){ 

return (/TF.*\.ai/i.test(file.name)); 

}

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