【问题标题】:Illustrator - batch insert filename in textpath script crashes IllustratorIllustrator - 在文本路径脚本中批量插入文件名会导致 Illustrator 崩溃
【发布时间】:2014-09-28 21:30:00
【问题描述】:

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

这是我为在 600 多个 pdf 文件中插入带有文件名的文本而编写的脚本。这假设适用于选定文件夹中的所有文件。

问题:Illustrator 崩溃。

第一次测试代码,在一些编辑过的文件后 Illustrator 崩溃了,所以我尝试在每次保存后引入延迟以减慢批处理速度。

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

不知道下一步该做什么。如果我删除此行,则代码有效: sourceDoc.close(SaveOptions.SAVECHANGES);

这是完整的脚本:

    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 崩溃的任何想法?

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

感谢您的帮助!

【问题讨论】:

    标签: javascript adobe-illustrator extendscript


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      删除此行后:

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

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

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

      @fabiantheblind 感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 2018-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-30
        • 1970-01-01
        • 2020-12-17
        • 2023-01-14
        • 2020-02-19
        相关资源
        最近更新 更多