Search code examples
javascriptadobe-illustratorextendscript

Illustrator - batch insert filename in textpath script crashes Illustrator


First off: I am not a programmer. Just playing around with code and trying to get it to work for a specific task:

Here is a script I made for the purpose of inserting a text with the file name in over 600 pdf files. This is suppose to work on all files in a selected folder.

The problem: Illustrator crashes.

First test code, after a few edited files Illustrator crashed, so I tried to introduce a delay after each save in order to slow down the batch process.

$.setTimeout(function () {sourceDoc.close(SaveOptions.SAVECHANGES)}, 1000);

No idea what to do next. The code works if I delete this line: sourceDoc.close(SaveOptions.SAVECHANGES);

Here is the complete script:

    var destFolder, sourceFolder, files, fileType, sourceDoc, layers, writeText, finLabel;  
      
    // Select the source folder.  
    sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PNG', '~' );  
      
    // If a valid folder is selected  
    if ( sourceFolder != null )  
    {  
    files = new Array();  
    fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', '*.pdf' );  
      
    // Get all files matching the pattern  
    files = sourceFolder.getFiles( fileType );  
      
    if ( files.length > 0 )  
    for ( i = 0; i < files.length; i++ )  
    {  
        sourceDoc = app.open(files[i]); // returns the document object  
        layers = unlock();  
        writeText = getFilename();  
        finLabel = remText();  
        sourceDoc.close(SaveOptions.SAVECHANGES);   //if save command line is deleted the code works   WTF???  
        $.setTimeout(function () {sourceDoc.close(SaveOptions.SAVECHANGES)}, 1000); // still crashes using delay ...  
    }  
    //alert( 'Files are saved as PNG in ' + destFolder );  
      
    else  
    {  
    alert( 'No matching files found' );  
    }  
    }  
      
    function unlock()  
    {  
       //get the total number of layers in the active document  
    doc = app.activeDocument;  
    var totalLayers = doc.layers.length;  
      
    //looping on layers to create one artboard per layer  
    for ( var i = 0 ; i < totalLayers ; i++){  
          
        var currentLayer = doc.layers[i];  
          
        //We don't want to deal with hidden layers  
        if(currentLayer.visible == false) continue;  
          
        //Unlock the layer if needed  
        currentLayer.locked = false;  
      
    }  
        }  
      
    function getFilename()  
    {  
    // Write text  
    var pointTextRef = app.activeDocument.textFrames.add();  
    pointTextRef.contents = app.activeDocument.name + "\n" + "YBS";  
    pointTextRef.top = 0;  
    pointTextRef.left = 0;  
    app.activeDocument.textFrames[0].textRange.characterAttributes.textFont=app.textFonts[31];  
          
        }  
      
    function remText()  
    {  
    // This works for search and replace :))))))  
      
        var active_doc = app.activeDocument;    
            
        var search_string = /_Template.pdf/gi; // g for global search, remove i to make a case sensitive search    
        var replace_string = '';    
            
        var text_frames = active_doc.textFrames;    
            
        if (text_frames.length > 0)    
        {    
            for (var i = 0 ; i < text_frames.length; i++)    
              {    
                  var this_text_frame = text_frames[i];    
                   var new_string = this_text_frame.contents.replace(search_string, replace_string);    
                       
                   if (new_string != this_text_frame.contents)    
                       {    
                            this_text_frame.contents = new_string;    
                       }    
              }    
        }        
     }  

Any ideas about what makes Illustrator crash?

Note: The application crashes after opening the first file.

Thanks for helping out!


Solution

  • Some things you should change and try:

    1. There is no $.setTimeout in extendscript
    2. Check if your file filter really works (getFiles(FileType))
    3. Your functions unlock(), getFilename() and remText() don't have return value so you don't need to pass their result into a variable
    4. Try a subset of your pdf files, not all 600
    5. add the var to your for loop for(var i = 0; i < ...)
    6. Try to do your for loop from the end of the list of files for (var i = files.length; i >= 0; i-- ){}