Illustrator-在文本路径脚本中批量插入文件名会导致Illustrator崩溃

Illustrator - batch insert filename in textpath script crashes Illustrator

本文关键字:文件名 崩溃 Illustrator 插入 文本 路径 脚本 Illustrator-      更新时间:2023-09-26

首先:我不是程序员。只是摆弄代码,并试图让它为特定任务工作:

这是我制作的一个脚本,目的是在600多个pdf文件中插入一个带有文件名的文本。假设这适用于选定文件夹中的所有文件。

问题是:Illustrator崩溃了。

第一个测试代码,在几个编辑过的文件Illustrator崩溃后,所以我试图在每次保存后引入延迟,以减缓批处理过程。

$.setTimeout(函数(){sourceDoc.close(SaveOptions.SAVECHANGE)},1000);

不知道下一步该做什么。如果我删除以下行,代码就会工作:sourceDoc.close(SaveOptions.SAVECHANGE);

以下是完整的脚本:

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

关于Illustrator崩溃的原因有什么想法吗?

注意:打开第一个文件后,应用程序崩溃。

谢谢你的帮助!

删除此行后:

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

代码在一批中处理了499个文件:)

原来Illustrator在试图保存损坏的pdf时崩溃了。

@fabiantheblind谢谢你的帮助!

有些事情你应该改变并尝试:

  1. 扩展脚本中没有$.setTimeout
  2. 检查文件过滤器是否真的有效(getFiles(FileType)
  3. 函数unlock()getFilename()remText()没有返回值,因此不需要将它们的结果传递到变量中
  4. 试试你的pdf文件的一个子集,而不是全部600个
  5. var添加到for循环for(var i = 0; i < ...)
  6. 尝试从文件列表for (var i = files.length; i >= 0; i-- ){}的末尾执行for循环