Find layer in the same position in another document
/*
FUNCTION DESCRIPTION:
Looks for the exact same layer in the current document in another document
INPUT:
layerTarget (Object) = The layer that will be searched for in the target document
docTarget (Object) = The target document
OUTPUT:
If the layer is found in the exact same location in the target document, that layer will be returned
Returns false if the layer didn't match anything in the target document
NOTE:
The function will look in the same exact location, not only by name.
If the layer exist but with another placement in the hierarchy than in the original document, the function will return false.
*/
function findMatchingLayer(layerTarget,docTarget) {
// Build the tree array.... funky....
// 0 = the target layer's name, 1 = the target layer's type, (parent's info and so on...)
var tree = [layerTarget.name,layerTarget.typename,layerTarget.parent.name,layerTarget.parent.typename];
var parentRef = layerTarget.parent;
var timeout = 12; // Maximum number of groups in a document is 10 but setting the timeout to 12 for good measure
while (parentRef != activeDocument && timeout > 0) {
parentRef = parentRef.parent;
// Push the name and type into the tree
tree.push(parentRef.name);
tree.push(parentRef.typename);
timeout--;
}
tree.length = tree.length-2; // Removes the document name and type out of the tree
tree.reverse(); // Puts the top group first in the array for easier reading
var layerPosition = docTarget;
for (var i = 0; i < tree.length; i+=2) { // Skip the type entry in the tree by incrementing by 2
try {
if (tree[i] == "ArtLayer") {
layerPosition = layerPosition.artLayers.getByName(tree[i+1]);
} else if (tree[i] == "LayerSet") {
layerPosition = layerPosition.layerSets.getByName(tree[i+1]);
} else { // If the array messes up..
alert("The layer type of \""+tree[i+1]+"\" returned as \""+tree[i]+"\"\nScript aborted.");
return;
}
} catch (e) {
// The layer is not present at the same location in the target document
return false;
}
}
return layerPosition;
}