How to write text to a text file by Photoshop JavaScript?

前端 未结 5 698
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 17:50

I took a look at Photoshop CS5 Scripting Guide and Photoshop CS5 JavaScript Reference, but I couldn\'t find out a method to write text to a plain text file. Is there any way to

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 18:19

    Here's what you need: It's pretty basic. It'll loop over the layers (no layersets!!) and save out the layer name and the layer bounds for each layer.

    app.preferences.rulerUnits = Units.PIXELS;
    var srcDoc = app.activeDocument;
    var numOfLayers = srcDoc.layers.length;
    var results = "";
    var fileName = srcDoc.name;
    var docName = fileName.substring(0,fileName.length -4)
    var theFile = srcDoc.path + "/" + docName + ".txt";
    
    for (var i = 0; i < numOfLayers  ; i++)
    {
      var theLayer = srcDoc.layers[i];
      var lb = getLayerBounds(theLayer).toString();
      results += theLayer.name + ": " + lb + "\n";
    }
    
    writeTextFile(theFile, results)
    alert(results);
    
    function getLayerBounds(alayer)
    {
      var x1 = parseFloat(alayer.bounds[0])
      var y1 = parseFloat(alayer.bounds[1])
      var x2 = parseFloat(alayer.bounds[2])
      var y2 = parseFloat(alayer.bounds[3])
      return [x1,y1,x2,y2]
    }
    
    function writeTextFile(afilename, output)
    {
      var txtFile = new File(afilename);
      txtFile.open("w"); //
      txtFile.writeln(output);
      txtFile.close();
    }
    

提交回复
热议问题