2014-09-28 66 views
2

首先:我不是程序員。只是玩弄代碼,並試圖讓它適用於特定的任務: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

有些事情你應該改變和嘗試:

  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--){}
1

的列表的末尾做你for循環刪除這一行後:

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

的代碼工作的499個文件在一個批次:)

原因當嘗試保存損壞的pdf時,Illustrator崩潰。

@ fabiantheblind 謝謝你的幫助!

+0

不客氣 – fabianmoronzirfas 2014-09-30 06:00:28