2017-09-25 143 views
0

目标:脚本运行极其缓慢,需要更有效的

我的脚本需要每个文件夹和层在Photoshop中,得到的中心点坐标,并将它们保存到一个txt文件。

问题:

脚本工作得很好,给了我所需要的确切数据。但是,当我有很多photoshop图层时,脚本运行非常缓慢。例如,我在PSD上运行脚本,可以说有200个小图层。这需要大约20分钟来获取我需要的输出文本文件。我的问题,并不考虑我不是程序员,是如何提高这个代码的效率,并让它运行得更快。

这里是输出数据的样本:

1 -MUD ROOM /车库:483.5x,559y

130A:307.5x,681y

Lighting_icon_square_4x复制19:382x, 749y

Lighting_icon_square_4x复制19:382x,681y

Lighting_icon_square_4x复制19:38 2X,613Y

Lighting_icon_square_4x复制18:233x,749y

Lighting_icon_square_4x复制17:233x,681y

Lighting_icon_square_4x复制13:233x,613Y

的代码:

// Bring application forward 
 
app.bringToFront(); 
 

 
// Set active Document variable and decode name for output 
 
var docRef = app.activeDocument; 
 
var docName = decodeURI(activeDocument.name).slice(0, -4); 
 

 
// Define pixels as unit of measurement 
 
var defaultRulerUnits = preferences.rulerUnits; 
 
preferences.rulerUnits = Units.PIXELS; 
 

 
// Define variable for the number of layers in the active document 
 
var layerNum = app.activeDocument.artLayers.length; 
 

 
// Define variable for the active layer in the active document 
 
var layerRef = app.activeDocument.activeLayer; 
 

 
// Define varibles for x and y of layers 
 
var x = (layerRef.bounds[2].value) - (layerRef.bounds[0].value); 
 
var y = (layerRef.bounds[3].value) - (layerRef.bounds[1].value); 
 
var coords = ""; 
 

 
// Loop to iterate through all layers 
 
function recurseLayers(currLayers) { 
 
    for (var i = 0; i < currLayers.layers.length; i++) { 
 
    layerRef = currLayers.layers[i]; 
 
    x = (layerRef.bounds[2].value) - (layerRef.bounds[0].value); 
 
    y = (layerRef.bounds[3].value) - (layerRef.bounds[1].value); 
 
    coords += layerRef.name + ": " + (layerRef.bounds[0].value + x/2) + "x" + "," + (layerRef.bounds[1].value + y/2) + "y" + "\n"; 
 

 
    //test if it's a layer set 
 
    if (isLayerSet(currLayers.layers[i])) { 
 
     recurseLayers(currLayers.layers[i]); 
 
    } 
 
    } 
 
} 
 

 
//a test for a layer set 
 
function isLayerSet(layer) { 
 
    try { 
 
    if (layer.layers.length > 0) { 
 
     return true; 
 
    } 
 
    } 
 

 
    catch(err) { 
 
    return false; 
 
    } 
 
} 
 

 
// Ask the user for the folder to export to 
 
var FPath = Folder.selectDialog("Save exported coordinates to"); 
 

 
// Detect line feed type 
 
if ($.os.search(/windows/i) !== -1) { 
 
    fileLineFeed = "Windows"; 
 
} 
 
else { 
 
    fileLineFeed = "Macintosh"; 
 
} 
 

 
// Export to txt file 
 
function writeFile(info) { 
 
    try { 
 
    var f = new File(FPath + "/" + docName + ".txt"); 
 
    f.remove(); 
 
    f.open('a'); 
 
    f.lineFeed = fileLineFeed; 
 
    f.write(info); 
 
    f.close(); 
 
    } 
 
    catch(e){} 
 
} 
 

 
// Run the functions 
 
recurseLayers(docRef); 
 
preferences.rulerUnits = defaultRulerUnits; // Set preferences back to user 's defaults 
 
writeFile(coords); 
 

 
// Show results 
 
if (FPath == null) { 
 
    alert("Export aborted", "Canceled"); 
 
} 
 
else { 
 
    alert("Exported " + docName + " x/y coordinates to " + FPath + "/" + docName + ".txt "); 
 
}

+0

我应该在输出示例中添加我发布的内容如下:根文件夹,子文件夹,图层。这个PSD文件中有许多根和子文件夹,至少有20个根文件夹和各种子文件夹,并且共有200个独立层遍布在外。 – Mike

回答

0

为了其他面临性能问题的读者,我缩短了一些代码。这在我的机器上运行得很快。

function main() { 
    var file = File.saveDialog("Save exported coordinates"); 
    if (file == null) return; 

    if (!file.open('w')) { 
     alert("Aborted", "Could not write file."); 
     return; 
    } 

    write(activeDocument.layers, file); 
    alert("Exported x/y coordinates to " + file.path); 
} 

function write(layers, file) { 
    for (var i = 0; i < layers.length; i++) { 
    var layer = layers[i]; 
    var width = layer.bounds[2] - layer.bounds[0]; 
    var height = layer.bounds[3] - layer.bounds[1]; 
    file.write(layer.name + ": " + (layer.bounds[0] + width/2).value + 
     "x, " + (layer.bounds[1] + height/2).value + "y\n"); 

    if (layer instanceof LayerSet) { 
     write(layer.layers, file); 
    } 
    } 
} 

main();