2017-09-25 144 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();