Reputation: 23
Goal:
Design a script that I can run inside of photoshop, that will provide me the x and y coordinates of every layer in the PSD file, and then save that to a text file that I can use to extract said data.
Progress:
I have already found a script that will accomplish this, and made my own additions to it.
Issue:
My issue is that I am not a very advanced coder, I am trying to get the x and y coordinates of the center registration point of the layer, not top left. I have done hours of research (maybe not asking google the right questions) to try to figure this out. I realize my level of understanding is not on par with actual software developers, and while I respect that, there is no developer that works here and I am kind of left alone to figure this out. I have studied a little javascript, so I understood enough to make small additions to the following code.
The Code:
// Bring application forward
app.bringToFront();
// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);
// 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[0].value;
var y = 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[0].value;
y = layerRef.bounds[1].value;
coords += layerRef.name + ": " + x + "x" + "," + y + "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 " + layerNum + " layer's coordinates to " + FPath + "/" +
docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}
Upvotes: 1
Views: 1110
Reputation: 18888
The bounds
property of a layer gives you the top left corner. The middle point is x + half the layer width and y + half the layer height. All you need are the layer dimensions and a little math.
Upvotes: 1