将图层坐标导出为ex​​cel的脚本 [英] Script to export layer coordinates to excel

查看:169
本文介绍了将图层坐标导出为ex​​cel的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个脚本导出我的图层坐标从Photoshop CS5到XML
我希望有人可以帮助我编辑该脚本记录坐标到xls文件?
如果可以让每个坐标在单独的行将是伟大的。
以下是我要修改的脚本,我需要做。

  // 
// This脚本导出将layer.bounds信息扩展为[psd_file_name] .xml
//由pattesdours
//

函数docCheck(){
//确保有至少有一个文档打开
if(!documents.length){
alert('没有文档打开。
返回; //退出
}
}

docCheck();

var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

var docRef = activeDocument;

var docWidth = docRef.width.value;
var docHeight = docRef.height.value;
var mySourceFilePath = activeDocument.fullName.path +/;

//获取图层索引/描述符的代码
//
cTID = function(s){return app.charIDToTypeID(s); };
sTID = function(s){return app.stringIDToTypeID(s); };
函数getLayerDescriptor(doc,layer){
var ref = new ActionReference();
ref.putEnumerated(cTID(Lyr),cTID(Ordn),cTID(Trgt));
return executeActionGet(ref)
};

函数getLayerID(doc,layer){
var d = getLayerDescriptor(doc,layer);
return d.getInteger(cTID('LyrI'));
};

var stackorder = 0;

//从Xbytor遍历所有图层的函数
traverseLayers = function(doc,ftn,reverse){
function _traverse(doc,layers,ftn,reverse){
var ok = true;
for(var i = 1; i< = layers.length&& ok!= false; i ++){
var index =(reverse == true)? layers.length-i:i - 1;
var layer = layers [index];

if(layer.typename ==LayerSet){
ok = _traverse(doc,layer.layers,ftn,reverse);

} else {
stackorder = stackorder + 1;
ok = ftn(doc,layer,stackorder);
}
}
return ok;
};

return _traverse(doc,doc.layers,ftn,reverse);
};

//创建一个字符串来保存数据
var str =;

//使用构造函数的类
函数cLayer(doc,layer){

//this.layerID = Stdlib.getLayerID(doc,layer);
this.layerID = getLayerID(doc,layer);
// alert(layer ID:+ this.layerID);
this.layerWidth = layer.bounds [2] .value - layer.bounds [0] .value;
this.layerHeight = layer.bounds [3] .value - layer.bounds [1] .value;

//相对于canvas的这些返回对象坐标
this.upperLeftX = layer.bounds [0] .value;
this.upperLeftY = layer.bounds [1] .value;
this.upperCenterX = this.layerWidth / 2 + layer.bounds [0] .value;
this.upperCenterY = layer.bounds [1] .value;
this.upperRightX = layer.bounds [2] .value;
this.upperRightY = layer.bounds [1] .value;
this.middleLeftX = layer.bounds [0] .value;
this.middleLeftY = this.layerHeight / 2 + layer.bounds [1] .value;
this.middleCenterX = this.layerWidth / 2 + layer.bounds [0] .value;
this.middleCenterY = this.layerHeight / 2 + layer.bounds [1] .value;
this.middleRightX = layer.bounds [2] .value;
this.middleRightY = this.layerHeight / 2 + layer.bounds [1] .value;
this.lowerLeftX = layer.bounds [0] .value;
this.lowerLeftY = layer.bounds [3] .value;
this.lowerCenterX = this.layerWidth / 2 + layer.bounds [0] .value;
this.lowerCenterY = layer.bounds [3] .value;
this.lowerRightX = layer.bounds [2] .value;
this.lowerRightY = layer.bounds [3] .value;

//我添加这些更容易编辑flash符号转换点(输出'x,y'格式)
//因为我喜欢分配使用数字的快捷键垫键盘,如:
// 7 8 9
// 4 5 6
// 1 2 3
//
this.leftBottom = this.lowerLeftX +,+ this.lowerLeftY;
this.bottomCenter = this.lowerCenterX +,+ this.lowerCenterY;
this.rightBottom = this.lowerRightX +,+ this.lowerRightY;

this.leftCenter = this.middleLeftX +,+ this.middleLeftY;
this.center = this.middleCenterX +,+ this.middleCenterY;
this.rightCenter = this.middleRightX +,+ this.middleRightY;

this.leftTop = this.upperLeftX +,+ this.upperLeftY;
this.topCenter = this.upperCenterX +,+ this.upperCenterY;
this.rightTop = this.upperRightX +,+ this.upperRightY;

//相对于层边界的这些返回对象坐标
this.relUpperLeftX = layer.bounds [1] .value - layer.bounds [1] .value;
this.relUpperLeftY = layer.bounds [0] .value - layer.bounds [0] .value;
this.relUpperCenterX = this.layerWidth / 2;
this.relUpperCenterY = layer.bounds [0] .value - layer.bounds [0] .value;
this.relUpperRightX = this.layerWidth;
this.relUpperRightY = layer.bounds [0] .value - layer.bounds [0] .value;
this.relMiddleLeftX = layer.bounds [1] .value - layer.bounds [1] .value;
this.relMiddleLeftY = this.layerHeight / 2;
this.relMiddleCenterX = this.layerWidth / 2;
this.relMiddleCenterY = this.layerHeight / 2;
this.relMiddleRightX = this.layerWidth;
this.relMiddleRightY = this.layerHeight / 2;
this.relLowerLeftX = layer.bounds [1] .value - layer.bounds [1] .value;
this.relLowerLeftY = this.layerHeight;
this.relLowerCenterX = this.layerWidth / 2;
this.relLowerCenterY = this.layerHeight / 2;
this.relLowerRightY = this.layerHeight;
this.relLowerRightX = this.layerWidth;
this.relLowerRightY = this.layerHeight;

返回此;
}

//添加标题行
// str =< psd filename = \+ docRef.name +\path = \ + mySourceFilePath +\width = \+ docWidth +\height = \+ docHeight +\> \\\
;

//现在收集一个函数
函数exportBounds(doc,layer,i){
var isVisible = layer.visible;
var layerData = cLayer(doc,layer);

// if(isVisible){
//相对于其活动像素的图层对象主坐标
var str2 = leftTop //这是
// + \layerwidth = \+ layerData.layerWidth
// +\layerheight = \+ layerData.layerHeight
// +\transformpoint = \ +center+\>// hard-coding'center'作为默认转换点
+\\+ layer.name +.png+ < / layer> \\\
//我必须在这里放一些内容,否则标签被忽略
str + = str2.toString();
};
//} ;


//调用X的函数使用上面的
traverseLayers(app.activeDocument,exportBounds,true);

//使用这个将XML文件导出到PSD文件所在的同一个目录
var mySourceFilePath = activeDocument.fullName.path +/;
//创建对文件进行输出的引用
var csvFile = new文件(mySourceFilePath.toString()。匹配(/([ ^ \。] +)/)[1] + app.activeDocument.name.match(/([^ \。] +)/)[1] +.xls);
//打开文件,写入数据,然后关闭文件
csvFile.open('w');
csvFile.writeln(str +< / psd>);
csvFile.close();
preferences.rulerUnits = originalRulerUnits;
//确认操作已完成
alert(操作完成!+\\\
+图层坐标成功导出为:+\\\
+\\\
+ mySourceFilePath.toString()。match(/([^ \。] +)/)[1] + app.activeDocument.name.match(/([^ \。] +)/)[1] + .XML);


解决方案

更改

  var str2 = leftTop //这是
// +\layerwidth = \+ layerData.layerWidth
// +\layerheight = \+ layerData.layerHeight
// +\transformpoint = \+center+\>// hard-coding中心'作为默认转换点
+\\+ layer.name +.png+< / layer> \\\
//我必须在这里放一些内容有时标签被忽略
str + = str2.toString();

  var str2 = leftTop +,+ layer.name +\\\

str + = str2.toString );

  var csvFile = new File(mySourceFilePath.toString()。match(/([^ \。] +)/)[1] + app.activeDocument.name.match(/([^ \ 。] +)/)[1] +.xls); 

to

  va r csvFile = new File(mySourceFilePath.toString()。match(/([^ \。] +)/)[1] + app.activeDocument.name.match(/([^ \。] +)/) [1] +.csv); 

这对我来说非常棒!


I have found a script that export my layers coordinates form photoshop CS5 to XML I hope somebody here can help me to edit that script to record coordinates to xls file? Also if is possible to have each coordinates on separate row will be great. Below is script I want to modify to do what I need.

//
// This script exports extended layer.bounds information to [psd_file_name].xml
// by pattesdours
//

function docCheck() {
    // ensure that there is at least one document open
    if (!documents.length) {
        alert('There are no documents open.');
        return; // quit
    }
}

docCheck();

var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

var docRef = activeDocument;

var docWidth = docRef.width.value;
var docHeight = docRef.height.value;
var mySourceFilePath = activeDocument.fullName.path + "/";

//  Code to get layer index / descriptor
//
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function getLayerDescriptor (doc, layer) {
    var ref = new ActionReference();
    ref.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
    return executeActionGet(ref)
};

function getLayerID(doc, layer) {
  var d = getLayerDescriptor(doc, layer);
  return d.getInteger(cTID('LyrI'));
};

var stackorder = 0;

// function from Xbytor to traverse all layers
traverseLayers = function(doc, ftn, reverse) {
  function _traverse(doc, layers, ftn, reverse) {
    var ok = true;
    for (var i = 1; i <= layers.length && ok != false; i++) {
      var index = (reverse == true) ? layers.length-i : i - 1;
      var layer = layers[index];

      if (layer.typename == "LayerSet") {
        ok = _traverse(doc, layer.layers, ftn, reverse);

      } else {
  stackorder = stackorder + 1;
        ok = ftn(doc, layer, stackorder);
      }
    }
    return ok;
  };

  return _traverse(doc, doc.layers, ftn, reverse);
};

// create a string to hold the data
var str ="";

// class using a contructor
function cLayer(doc, layer) {

//this.layerID = Stdlib.getLayerID(doc, layer);
    this.layerID = getLayerID(doc, layer);
  //alert("layer ID: " + this.layerID);
    this.layerWidth = layer.bounds[2].value - layer.bounds[0].value;
      this.layerHeight = layer.bounds[3].value - layer.bounds[1].value;

    // these return object coordinates relative to canvas
      this.upperLeftX = layer.bounds[0].value;
      this.upperLeftY = layer.bounds[1].value;
      this.upperCenterX = this.layerWidth / 2 + layer.bounds[0].value;
      this.upperCenterY = layer.bounds[1].value;
      this.upperRightX = layer.bounds[2].value;
      this.upperRightY = layer.bounds[1].value;
      this.middleLeftX = layer.bounds[0].value;
      this.middleLeftY = this.layerHeight / 2 + layer.bounds[1].value;
      this.middleCenterX = this.layerWidth / 2 + layer.bounds[0].value;
      this.middleCenterY = this.layerHeight / 2 + layer.bounds[1].value;
      this.middleRightX = layer.bounds[2].value;
      this.middleRightY = this.layerHeight / 2 + layer.bounds[1].value;
      this.lowerLeftX = layer.bounds[0].value;
      this.lowerLeftY = layer.bounds[3].value;
      this.lowerCenterX = this.layerWidth / 2 + layer.bounds[0].value;
      this.lowerCenterY = layer.bounds[3].value;
      this.lowerRightX = layer.bounds[2].value;
      this.lowerRightY = layer.bounds[3].value;

      // I'm adding these for easier editing of flash symbol transformation point (outputs a 'x, y' format)
      // because I like to assign shortcut keys that use the numeric pad keyboard, like such:
      //        7   8   9
      //        4   5   6
      //        1   2   3
      //
      this.leftBottom = this.lowerLeftX + ", " + this.lowerLeftY;
      this.bottomCenter = this.lowerCenterX + ", " + this.lowerCenterY;
      this.rightBottom = this.lowerRightX + ", " + this.lowerRightY;

      this.leftCenter = this.middleLeftX + ", " + this.middleLeftY;
      this.center = this.middleCenterX + ", " + this.middleCenterY;
      this.rightCenter = this.middleRightX + ", " + this.middleRightY;

      this.leftTop = this.upperLeftX + ", " + this.upperLeftY;
      this.topCenter = this.upperCenterX + ", " + this.upperCenterY;
      this.rightTop = this.upperRightX + ", " + this.upperRightY;

    // these return object coordinates relative to layer bounds
      this.relUpperLeftX = layer.bounds[1].value - layer.bounds[1].value;
      this.relUpperLeftY =  layer.bounds[0].value - layer.bounds[0].value;
      this.relUpperCenterX = this.layerWidth / 2;
      this.relUpperCenterY = layer.bounds[0].value - layer.bounds[0].value;
      this.relUpperRightX = this.layerWidth;
      this.relUpperRightY = layer.bounds[0].value - layer.bounds[0].value;
      this.relMiddleLeftX = layer.bounds[1].value - layer.bounds[1].value;
      this.relMiddleLeftY = this.layerHeight / 2;
      this.relMiddleCenterX = this.layerWidth / 2;
      this.relMiddleCenterY = this.layerHeight / 2;
      this.relMiddleRightX = this.layerWidth;
    this.relMiddleRightY = this.layerHeight / 2;
      this.relLowerLeftX = layer.bounds[1].value - layer.bounds[1].value;
      this.relLowerLeftY = this.layerHeight;
      this.relLowerCenterX = this.layerWidth / 2;
    this.relLowerCenterY = this.layerHeight / 2;
      this.relLowerRightY = this.layerHeight;
      this.relLowerRightX = this.layerWidth;
      this.relLowerRightY = this.layerHeight;

  return this;
}

// add header line
//str = "<psd filename=\"" + docRef.name + "\" path=\"" + mySourceFilePath + "\" width=\"" + docWidth + "\" height=\"" + docHeight + "\">\n";

// now a function to collect the data
function exportBounds(doc, layer, i) {
    var isVisible = layer.visible;
    var layerData = cLayer(doc, layer);

 // if(isVisible){
// Layer object main coordinates relative to its active pixels
    var str2 = leftTop // this is the 
//  + "\" layerwidth=\"" + layerData.layerWidth 
//  + "\" layerheight=\"" + layerData.layerHeight 
//  + "\" transformpoint=\"" + "center" + "\">" // hard-coding 'center' as the default transformation point 
    +"\"  \""  + layer.name + ".png" + "</layer>\n" // I have to put some content here otherwise sometimes tags are ignored
str += str2.toString();
   };
//};


// call X's function using the one above
traverseLayers(app.activeDocument, exportBounds, true);

// Use this to export XML file to same directory where PSD file is located
    var mySourceFilePath = activeDocument.fullName.path + "/";
// create a reference to a file for output
    var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xls");
// open the file, write the data, then close the file
csvFile.open('w');
csvFile.writeln(str + "</psd>");
csvFile.close();
preferences.rulerUnits = originalRulerUnits;
// Confirm that operation has completed
alert("Operation Complete!" + "\n" + "Layer coordinates were successfully exported to:" + "\n" + "\n" + mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xml");

解决方案

Change

var str2 = leftTop // this is the 
//  + "\" layerwidth=\"" + layerData.layerWidth 
//  + "\" layerheight=\"" + layerData.layerHeight 
//  + "\" transformpoint=\"" + "center" + "\">" // hard-coding 'center' as the default transformation point 
    +"\"  \""  + layer.name + ".png" + "</layer>\n" // I have to put some content here otherwise sometimes tags are ignored
str += str2.toString();

to

var str2 = leftTop + ","+ layer.name + "\n" 
   str += str2.toString();

and

var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xls");

to

var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".csv");

This works great for me!

这篇关于将图层坐标导出为ex​​cel的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆