Illustrator - 文本路径脚本中的批插入文件名崩溃Illustrator

Illustrator - 文本路径脚本中的批插入文件名崩溃Illustrator

问题描述:

首先:我不是程序员。只是玩弄代码,并试图让它适用于特定的任务:Illustrator - 文本路径脚本中的批插入文件名崩溃Illustrator

这是一个脚本,我为了在600多个pdf文件中插入一个带有文件名的文本。这是假设处理选定文件夹中的所有文件。

问题:Illustrator崩溃。

第一次测试代码,经过几次编辑后的文件,Illustrator崩溃了,所以我试图在每次保存后引入一个延迟,以减慢批处理过程。 (函数(){sourceDoc.close(SaveOptions.SAVECHANGES)},1000); $ .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;  
 
         }  
 
       }  
 
     }   
 
    }

是什么让插画崩溃任何想法?

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

感谢您的帮助!

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

  1. 有一个在extendscript
  2. 检查没有$.setTimeout如果您的文件过滤确实有效(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循环删除这一行后:

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

的代码工作的499个文件在一个批次:)

原因当尝试保存损坏的pdf时,Illustrator崩溃。

@ f*theblind 谢谢你的帮助!

+0

不客气 – f*moronzirfas 2014-09-30 06:00:28