Javascript Indesign CS6脚本-导出图像

Javascript Indesign CS6脚本-导出图像,javascript,export,adobe-indesign,Javascript,Export,Adobe Indesign,我在indesign cs6中编写js脚本以导出格式化图像时遇到问题。下面的代码(在本网站上找到并稍作修改)仅打开文档 理想情况下,脚本将循环遍历我文档中的所有格式化/剪切图像,并将它们导出到桌面上的新文件夹中,但使用原始文件名 任何帮助都将不胜感激: test(); function test(){ var myDoc = app.open('/Users/StudioA/Desktop/file.indd'); var myGroups = myDoc.groups; //for ea

我在indesign cs6中编写js脚本以导出格式化图像时遇到问题。下面的代码(在本网站上找到并稍作修改)仅打开文档

理想情况下,脚本将循环遍历我文档中的所有格式化/剪切图像,并将它们导出到桌面上的新文件夹中,但使用原始文件名

任何帮助都将不胜感激:

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

           }
       }

 }
test();
功能测试(){
var myDoc=app.open('/Users/StudioA/Desktop/file.indd');
var myGroups=myDoc.groups;
//对于每个组。。。
对于(var i=0;i
文件名不在矩形上,而是在与放置的图形相关的链接上。 如果有一个打开的文档,这应该满足您的要求:

test();



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

只需添加我的详细版本,它可以从InDesign中的当前选择工作,并提供控制台反馈。它使用前缀“crop_u2;”重命名图像并将其保存到~/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.");
}
exportSelectedImages();
函数exportSelectedImages(){
//配置导出设置
app.jpegExportPreferences.exportResolution=72;
app.jpegExportPreferences.jpegQuality=jpegoptionsquity.HIGH;
//收集选定对象
所选变量=app.activeDocument.selection;
$.writeln(“Got”+selected.length+“selected objects…”);
//处理选定对象
对于(变量i=0;i0){
var img=cursor.images[0];
$.writeln(“\t包含类型为“+img.imageTypeName”的图像);
var imageFileName=cursor.images[0].itemLink.name;
$.writeln(“\t文件名:“+imageFileName”);
}否则{
$.writeln(“\t不是图像”);
}
//将对象保存到以下指定路径中的jpeg
var myFile=新文件(“~/temp/”+“crop_389;”+imageFileName+'.jpg”);
cursor.exportFile(ExportFormat.JPG,myFile);
}
$.writeln(“完成”);
}

谢谢您,@Loic。我从这段代码中学到了一些东西。我没有经历过将变量设置为三个不同的项(API=…),也没有见过有人使用.pop()。这看起来是非常有用和精简的代码。