2012-06-11 72 views
2

我無法寫在InDesign CS6 JS腳本導出我的格式的圖像。下面的代碼(在本網站上找到並稍加修改)只能打開文檔。排版CS6腳本 - 導出圖像

理想的腳本將通過我的所有文件中的格式/裁切影像,並將其導出到桌面上的一個新的文件夾,但與原來的文件名循環。

任何幫助,將不勝感激:

test(); 
function test(){ 

var myDoc = app.open('/Users/StudioA/Desktop/file.indd'); 
var myGroups = myDoc.groups; 

//for each group... 
for (var i = 0;i < myGroups.length; i++){ 
    // for each rectangle in the group... 
    for(var r = 0; r< myGroups[i].rectangles.length; r++){ 

     var myRect = myGroups[i].rectangles[r]; 
      app.jpegExportPreferences.exportResolution = 300; 
      app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM; 

      //give it a unique name 
      var myFile = new File('/Users/StudioA/Desktop/Export/' + myRect.name + '.jpg'); 

      myRect.exportFile(ExportFormat.JPG, myFile); 

      } 
     } 

} 

回答

4

文件名不位於矩形,但涉及到放置圖形的鏈接。 這應該做你想要給出一個打開的文檔的內容:

test(); 

功能測試(){

var myDoc = app.activeDocument, 
apis = myDoc.allPageItems, rect, fileName; 


while (rect = apis.pop()) 
{ 
    if (!(rect instanceof Rectangle) || !rect.graphics[0].isValid){ continue; } 

    fileName = File (rect.graphics[0].itemLink.filePath).name; 
    fileName = fileName.replace(/\.[a-z]{2,4}$/i, '.jpg'); 

    app.jpegExportPreferences.exportResolution = 300; 
    app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM; 

    //give it a unique name 
    var myFile = new File (Folder.desktop+"/"+ fileName); 

    rect.exportFile(ExportFormat.JPG, myFile); 
} 

}

+0

非常感謝你,我將測試這一點,並回到這裏發表評論! – Alex

+0

完美的作品!多謝 :) – Alex

0

只是增加我的這個詳細的版本,從當前選擇工作在InDesign中並提供控制檯反饋。它重命名圖像以前綴「crop_」並將它們保存到〜/ TEMP

exportSelectedImages(); 

function exportSelectedImages() { 
    // configure export settings 
    app.jpegExportPreferences.exportResolution = 72; 
    app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH; 

    // collect selected objects 
    var selected = app.activeDocument.selection; 
    $.writeln("Got " + selected.length + " selected objects..."); 

    // process selected objects 
    for (var i = 0; i < selected.length; i++) { 
     var cursor = selected[i]; 
     var img = cursor.images; 

     $.writeln("Processing #" + (i+1) + "/" + selected.length); 
     $.writeln("\t Type: " + cursor.constructor.name); 

     // verify if object contains an image or not 
     if (cursor.images.length > 0) {  
      var img = cursor.images[0]; 
      $.writeln("\t Contains image of type " + img.imageTypeName); 
      var imageFileName = cursor.images[0].itemLink.name; 
      $.writeln("\t File Name: " + imageFileName); 
     } else { 
      $.writeln("\t Not an image"); 
     } 

     // save the object to a jpeg in path specified below 
     var myFile = new File('~/temp/' + "crop_" + imageFileName + '.jpg'); 
     cursor.exportFile(ExportFormat.JPG, myFile); 

    } 

    $.writeln("Done."); 
}