Reputation: 11
Example Photoshop file:
[Layer 1]
[Group 1]
[Layer 2]
[Layer 3]
[Layer 4 (Hidden)]
[Layer 5]
Desired Output (colon separated text file):
Layer 1:x:y:w:h
Group 1:Layer 2:x:y:w:h
Group 1:Layer 3:x:y:w:h
Layer 5:x:y:w:h
As you can see, I am trying to write the names of each layer with its co-ordinates and dimensions to a colon separated text file. Ignoring any hidden layers and appending the group name to any layers within groups. I can get the co-ordinates and dimensions for each layer with this script:
// Enables double-click launching from the Mac Finder or Windows Explorer
#target photoshop
// 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 w = layerRef.bounds[2].value;
var h = layerRef.bounds[3].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];
if (!layerRef.visibility) {
continue;
}
x = layerRef.bounds[0].value;
y = layerRef.bounds[1].value;
w = layerRef.bounds[2].value - x;
h = layerRef.bounds[3].value - y;
coords += layerRef.name + ":" + x + ":" + y + ":" + w + ":" + h + "\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!");
}
Above script originally provided on this site by Chris DeLuca with a small modification by me to provide the dimensions. However, that lists all layers whether or not they are hidden and doesn't care about groups (Layer Sets - right?)
In an attempt to ignore hidden layers I changed the recursive function to this:
// Loop to iterate through all layers
function recurseLayers(currLayers) {
for ( var i = 0; i < currLayers.layers.length; i++ ) {
layerRef = currLayers.layers[i];
if (!layerRef.visibility) {
continue;
}
x = layerRef.bounds[0].value;
y = layerRef.bounds[1].value;
w = layerRef.bounds[2].value - x;
h = layerRef.bounds[3].value - y;
coords += layerRef.name + "," + x + "," + y + "," + w + "," + h + "\n";
//test if it's a layer set
if ( isLayerSet(currLayers.layers[i]) ) {
recurseLayers(currLayers.layers[i]);
}
}
}
Thinking that it would ignore any hidden layers. Unfortunately that produces a zero byte file.
So, ignoring that issue for now and trying the grouping problem. I don't want a group to be listed but I do want the groups name to be appended to the beginning of any layers that are within the group. I tried this:
// 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;
w = layerRef.bounds[2].value - x;
h = layerRef.bounds[3].value - y;
coords += group + ":" + layerRef.name + ":" + x + :," + y + ":" + w + ":" + h + "\n";
group = "";
//test if it's a layer set
if ( isLayerSet(currLayers.layers[i]) ) {
group = currLayers.layers[i]
recurseLayers(currLayers.layers[i]);
}
}
}
But that only appends the group name to the first layer within it. Understandable really but I can't figure out how to get each layers parent group.
Any help with one or both of those issues would be greatly appreciated. Thanks
Upvotes: 0
Views: 309
Reputation: 11
I've been tinkering for a couple of hours and managed to answer my own question. It's a bit hacky and I'm sure there is a more elegant solution but it works!
Here it is if anyone else needs it:
#target photoshop
// 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 w = layerRef.bounds[2].value;
var h = layerRef.bounds[3].value;
var coords = "";
var group = "";
var layersInGroup = 0;
var thisGroupsLayer = 0;
var inGroup = false;
// Loop to iterate through all layers
function recurseLayers(currLayers) {
for ( var i = 0; i < currLayers.layers.length; i++ ) {
layerRef = currLayers.layers[i];
if (!layerRef.visible) {
continue;
}
if (isLayerSet(currLayers.layers[i]) == 0){
x = layerRef.bounds[0].value;
y = layerRef.bounds[1].value;
w = layerRef.bounds[2].value - x;
h = layerRef.bounds[3].value - y;
coords += group + layerRef.name + ":" + x + ":" + y + ":" + w + ":" + h + "\n";
}
if (inGroup && thisGroupsLayer == layersInGroup){
inGroup = false;
group = "";
thisGroupsLayer = 0;
}
if (inGroup && thisGroupsLayer != layersInGroup){
thisGroupsLayer++;
}
if (!inGroup){
layersInGroup = isLayerSet(currLayers.layers[i]);
if ( layersInGroup ) {
var groupname = currLayers.layers[i].toString();
group = groupname.substring(0, groupname.length-1);
group = group.slice(10, group.length)+":";
inGroup = true;
thisGroupsLayer++;
recurseLayers(currLayers.layers[i]);
}
}
}
}
//a test for a layer set
function isLayerSet(layer) {
try {
if ( layer.layers.length > 0 ) {
return layer.layers.length;
}
}
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
var filename = docName.substring(0, docName.length - 4) + " subimages.txt";
function writeFile(info) {
try {
var f = new File(FPath + "/" + filename);
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 + "/" + filename + " using " + fileLineFeed + " line feeds.", "Success!");
}
Upvotes: 1