脚本运行速度极慢,需要更高效

时间:2017-09-25 20:41:02

标签: javascript performance photoshop software-design

目标:

我的脚本获取photoshop中的每个文件夹和图层,获取中心点坐标,并将它们保存到txt文件。

问题:

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

以下是输出数据的示例:

1 -MUD ROOM / GARAGE:483.5x,559y

130A:307.5x,681y

Lighting_icon_square_4x copy 19:382x,749y

Lighting_icon_square_4x copy 19:382x,681y

Lighting_icon_square_4x copy 19:382x,613y

Lighting_icon_square_4x copy 18:233x,749y

Lighting_icon_square_4x copy 17:233x,681y

Lighting_icon_square_4x copy 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 ");
}

1 个答案:

答案 0 :(得分:0)

为了面对性能错误的其他读者,我已经缩短了你的代码。这在我的机器上运行得很快。

public class YourFragment extends BaseFragmnet
{

    ...
    .
    .

    @Override
    public boolean onBackPressed()
    {
        // do something...

        return true; // you should return true;
    }
}