• 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 to relink

Participant ,
Dec 08, 2017 Dec 08, 2017

Copy link to clipboard

Copied

Hello all,

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 all 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])

    }

}

Thanks.

Dane

TOPICS
Scripting

Views

438

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
Guru ,
Dec 10, 2017 Dec 10, 2017

Copy link to clipboard

Copied

Check out this script.

— Kas

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

Copy link to clipboard

Copied

mornin' Kas

I just went through this application. Very interesting. I think I learned a dozen new things.

I did actually manage to pound out a refined search using parts of the old path name.

One of the main goals of the script though, was to have no dialogs. The script would just figure out where to go(with in a defined area)

Below is the code I ended up with.

Thanks a lot for your input and share this great bit of scripting.

  1. var idDoc = app.activeDocument 
  2. var broke = [] 
  3. var foundFiles = [] 
  4. var folds = [] 
  5. var started = false 
  6. for (var i=0; i < idDoc.links.length; i++){ 
  7.     var a = idDoc.links 
  8.     if (a.status == LinkStatus.LINK_MISSING ){ 
  9.          
  10.         //here i parsed the broken path and used parts of it im my searchFolder path. 
  11.         //this made my directory was much smaller and more targeted and therefore does not have a stack overflow 
  12.         var parse = a.filePath.split("\\"
  13.         var gender = parse[2]+ "\\" 
  14.         var prodGroup = parse[3
  15.         broke.push(a.name) 
  16.     } 
  17. if (broke.length >= 0){ 
  18.     var searchFolder =Folder("Q:\Kollektion_Files\\" + gender + prodGroup) 
  19.     getFilesFromFolder(searchFolder) 
  20.     fix(broke, foundFiles) 
  21.     } 
  22.  
  23.  
  24. function getFilesFromFolder(curfolder){ 
  25.      
  26.     if (started == true){ 
  27.         folds.splice(0,1
  28.         } 
  29.     var files = curfolder.getFiles();  
  30.     for (l = 0; l < files.length; l++){ 
  31.         if (files instanceof Folder){ 
  32.             var entry = files 
  33.             folds.push(entry) 
  34.             } 
  35.         } 
  36.     var curFile;   
  37.     for(var x=0; x<files.length; x++){   
  38.         curFile = files;   
  39.         if(curFile instanceof File && validFile(curFile)){ 
  40.             foundFiles.push(curFile);  
  41.              
  42.         } 
  43.     } 
  44.     started = true 
  45.     var len = folds.length 
  46.     if (folds.length >> 0){ 
  47.     getFilesFromFolder(folds[0]) 
  48.     } 
  49. }  
  50.  
  51.  
  52. function fix(linkList, fileList){ 
  53. for (var t = 0; t < linkList.length; t++){ 
  54.     var fixLink = linkList.substring(0, 9
  55.     for(var x=0;x<fileList.length; x++)   {   
  56.         if(fileList.displayName.search(fixLink) >= 0){ 
  57.             var theLink = idDoc.links.itemByName(linkList
  58.             theLink.relink(fileList
  59.             break
  60.             } 
  61.       } 
  62.     }   
  63. }   
  64.  
  65.  
  66. function validFile(file){   
  67. return (/TF.*\.ai/i.test(file.name));   
  68. }  
  1. var idDoc = app.activeDocument 
  2. var broke = [] 
  3. var foundFiles = [] 
  4. var folds = [] 
  5. var started = false 
  6. for (var i=0; i < idDoc.links.length; i++){ 
  7.     var a = idDoc.links 
  8.     if (a.status == LinkStatus.LINK_MISSING ){ 
  9.          
  10.         //here i parsed the broken path and used parts of it im my searchFolder path. 
  11.         //my directory was much smaller and more targeted and therefore does not have a stack overflow 
  12.         var parse = a.filePath.split("\\"
  13.         var gender = parse[2]+ "\\" 
  14.         var prodGroup = parse[3
  15.         broke.push(a.name) 
  16.     } 
  17. if (broke.length >= 0){ 
  18.     var searchFolder =Folder("Q:\Kollektion_Files\\" + gender + prodGroup) 
  19.     getFilesFromFolder(searchFolder) 
  20.     fix(broke, foundFiles) 
  21.     } 
  22.  
  23.  
  24. function getFilesFromFolder(curfolder){ 
  25.      
  26.     if (started == true){ 
  27.         folds.splice(0,1
  28.         } 
  29.     var files = curfolder.getFiles();  
  30.     for (l = 0; l < files.length; l++){ 
  31.         if (files instanceof Folder){ 
  32.             var entry = files 
  33.             folds.push(entry) 
  34.             } 
  35.         } 
  36.     var curFile;   
  37.     for(var x=0; x<files.length; x++){   
  38.         curFile = files;   
  39.         if(curFile instanceof File && validFile(curFile)){ 
  40.             foundFiles.push(curFile);  
  41.              
  42.         } 
  43.     } 
  44.     started = true 
  45.     var len = folds.length 
  46.     if (folds.length >> 0){ 
  47.     getFilesFromFolder(folds[0]) 
  48.     } 
  49. }  
  50.  
  51.  
  52. function fix(linkList, fileList){ 
  53. for (var t = 0; t < linkList.length; t++){ 
  54.     var fixLink = linkList.substring(0, 9
  55.     for(var x=0;x<fileList.length; x++)   {   
  56.         if(fileList.displayName.search(fixLink) >= 0){ 
  57.             var theLink = idDoc.links.itemByName(linkList
  58.             theLink.relink(fileList
  59.             break
  60.             } 
  61.       } 
  62.     }   
  63. }   
  64.  
  65.  
  66. function validFile(file){   
  67. return (/TF.*\.ai/i.test(file.name));   
  68. }  

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
Guru ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

One of the main goals of the script though, was to have no dialogs.

In this case you may be interested in my Batch processor script so instead of opening, processing the current document, closing it, you may process dozens/hundreds/thousands files in one go. I am working on a new -- much improved -- version now. Last week I tested it together with my new 'package' script at work to make archive of old issues and it worked well for me: packaged almost 300 files for 16 minutes.

— Kas

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

Copy link to clipboard

Copied

LATEST

Excellent.

Get stuff.

I'd sure like to be spending more time writing scripts and less time using them. Far more interesting.

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