脚本导出艺术板中每个层的所有Android相关分辨率



上下文

对于Android开发,我试图为Illustrator创建一个脚本,将我的矢量作品从每一层导出到.png-格式,其中每个导出都必须以Android需要的所有密度(ppi(格式提供。我知道我可以导出这样的图像:

png24_settings = function ( ) {
var options = new ExportOptionsPNG24();
options.antiAliasing = true;
options... = ...
...
return options;
}
var file_destination = "Some string with the destination path and name"
var doc = app.activeDocument; //Retrieve active document
doc.exportFile(file_destination, ExportType.PNG24, png24_settings() ); // Exports visible canvas

每个层都可以通过在层上循环并将所有其他层设置为不可见来导出。然而,我不能用给定的函数创建一个循环来帮助我解决密度问题。

问题

exportFile()-func不采用任何分辨率参数,因此无法用于创建Android所需的不同密度文件(hdpi、xhdpi…(。我必须使用哪个选项以所有必要的ppi格式导出每个图层?

通过一些文档咨询,我能够解决我的问题。存在一个名为imageCapture()的函数,它包含所需ppi(指向文档的链接(的resolution参数。请注意,此功能将导出艺术板上的所有可见层。因此,导出的迭代需要注意可见性。

有了这些信息,我可以迭代我的艺术板中的每一层并进行导出。脚本看起来像下面提供的脚本。我知道它有点详尽无遗,但我希望它有助于获得清晰的理解。

提供设置信息

// Provide density-information necessary for android
android_densities = [
120 // ldpi
, 160 // mdpi
, 240 // hdpi
, 320 // xhdpi
, 480 // xxhdpi
, 640 // xxxhdpi
]
// Provide settings for image export which takes the respective density as argument
setOptions = function(density) {
var options = new ImageCaptureOptions();

options.artBoardClipping = true;
options.resolution       = density // Your desired ppi
options...                         // Provide other options you'd also like to implement.

return options;
}

// Initialize current document as document to work with
var doc = app.activeDocument; //Gets the active document
// Retrieve current directory of illustrator-document as default path to save to
// If you want to save anywhere else, just provide it here
var filePath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\/g, '/');
// Select current artboard according to current document
// This is the artboard you want to export your layers from
var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];

注意:文档中提供了Android密度。

迭代导出

// Export, for example, all visible layers.
// Thus, all layers are first iterated, checked for visibility and set invisible.
// Because of the last step all elements have to be cached in an array (= export_layers)
export_layers= []; // Store layers to export
for(var i=0; i<doc.layers.length; i++){
var layer = doc.layers[i];
if(layer.visible){
export_layers.push(i);
doc.layers[i].visible=false;
};
}
// Do exports
for(var exportElement=0; exportElement<export_layers.length; exportElement++) {

// Get layer to export
var layer = doc.layers[export_layers[exportElement]];

// Set this layer visible (it is the only layer visible now, because imageCapture will export all visible layers)
doc.layers[export_layers[exportElement]].visible=true;

// Iterate over all densities
for(var densityElement=0; densityElement<android_densities.length; densityElement++) {

// Provide path and name for output image
var destination_of_file = new File(filePath + "/" + layer.name + "_" + String(android_densities[densityElement]) + ".png");

// Save file with resolution
doc.imageCapture(destination_of_file, activeAB.artboardRect, setOptions(android_densities[densityElement]));
}
// Set visibility to false again for next layer to be exported correctly
doc.layers[export_layers[exportElement]].visible=false;
}

// Finally, reset the visibility-status prior to the export
for(var resetElement=0; i<export_layers.length; resetElement++) {
doc.layers[export_layers[resetElement]].visible=true;
}

之后,您应该在指定的文件夹中拥有所有密度的所有层。当然,这可以通过为每个分辨率提供一个文件夹等方式变得更加优雅——我只是试着把它保留到正确的答案:(

最新更新