Copy link to clipboard
Copied
Hello everyone, I want to use a script to align all colored layers to gray layers according to their shape outlines, and some layers are rotated 180°. Can this function be achieved through a script? I'm not very familiar with scripts. I used ChatGPT to write the scripts I need, but this time, I don't know if this function can be achieved through script logic.
Your files have large linear dimensions. The most time-consuming operations are the selection expand operation (used when dividing a gray layer into fragments) and layer transformation operations (used to determine the optimal position of a figure and at the final stage of alignment). It is impossible to get rid of them. Changing the remaining commands will have an insignificant effect on the overall performance.
I added the constant SCALE = 0.25 to the code - this is the scale value by which t
...Copy link to clipboard
Copied
There were 2 new problems 🤔
With such an arrangement of objects, the area calculation does not work correctly. As I understand it, in your case, you cannot execute the revealAll command (so that all objects fit into the document by expanding the canvas), so I made some minor corrections so that when checking the position of objects, they are shifted inside the working area.
I also found another limitation that I hadn't thought of before - the sample with gray layers must have a gap of EXPAND_BY = 20 pixels not only between individual samples, but also from the edges of the document. I didn't solve this problem, since I assume that the margins will be on each document.
Updated Script here: Aug 13, 2025
Copy link to clipboard
Copied
Yes, each file essentially consists of samples with a gray layer. There must be an EXPAND_BY = 20 pixel gap not only between samples but also between them and the document edge. However, your mention of objects extending beyond the artboard reminds me that I'm going to try increasing the artboard width and height before running the script (to ensure no groups extend beyond the artboard), then resetting it afterward. This should improve the error message caused by the target group extending beyond the artboard and improve compatibility with different shape files. More importantly, I've discovered a potential issue that can cause the script to fail: if the long strip group overlaps with another group because it needs to stay within the artboard, the script seems to fail to recognize it. I'm not sure if this will also happen with other shapes. Given the diversity and complexity of the other files, I think increasing the artboard width and height before running the script (to minimize overlap) is the safest approach. Do you think this is feasible? Will this have any impact, or will I need to modify some parameters in the code?ps:I moved this group out and there will be no problem running it in the future if it is not overlapped.
Copy link to clipboard
Copied
The problem is not in the overlapping of objects, but in my proportion estimation algorithm - the objects seem identical to us, but the 腰节 layer is literally a couple of pixels longer than the sample. Because the layer has a (very) large width, but a small height, the WxH coefficient changes greatly due to these two pixels and exceeds WXHTolerance = 0.15 when comparing objects.
Changing the constant to WXHTolerance = 0.25 should solve the problem with this document and not cause problems with others.
You can set a higher value and see if there are false comparisons of objects in other documents - this const literally means "if the area of objects is close or equal, then how much can the WxH proportion coefficient of one object differ from the WxH of another so that we consider them a pair", for example the area of these two objects is equal:
but the WxH of a straight line is 15.59, and the WxH of a square is 1.02, i.e. with the help of this coefficient the script "sees" that the layers, although they have the same area, cannot be considered a pair.
Copy link to clipboard
Copied
the objects seem identical to us, but the 腰节 layer is literally a couple of pixels longer than the sample.
When working with templates (as these seem to be) changing measurements arbitraily would seem sloppy and an obvious hindrance to automation.
Copy link to clipboard
Copied
Yes, this perfectly solved the previous problem. After testing other files, this script was proven to be correct and feasible. I can’t believe you did it. Thank you very much, NB!
Copy link to clipboard
Copied
Yes, this perfectly solved the previous problem. After testing other files, this script was proven to be correct and feasible.
Then please mark @jazz-y ’s post as the »Correct Answer«.
Copy link to clipboard
Copied
Thanks for the reminder, I will set it up now. I was not familiar with this before.
Copy link to clipboard
Copied
I started using this script at work, and after using it a few times, I discovered a problem. It seems to mirror the groups when aligning. I tried to modify the parameters. While keeping other parameters unchanged, I changed "SCALE = 0.15". Changing the value of "SCALE" will cause group mirroring errors depending on the size of the group. 0.15 seems to be the balance point for this value. Too large or too small will cause errors. When "SCALE = 0.15" is used, the script only mirrors the smallest group. After changing the value, the other groups will also be mirrored. Can this problem be fixed? Or is it feasible to add a code to correct the mirroring error after running the script? (As marked in the figure, when "SCALE = 0.15" is used, the smallest group in red will be mirrored (as well as the slightly larger group), but after changing the value, the other groups will be mirrored, and more and more groups will be mirrored.)
Copy link to clipboard
Copied
The more I tested it, the more I found that it would randomly mirror the group left and right when it worked, for every file, and it had nothing to do with the size of the group.
Copy link to clipboard
Copied
Please provide some of the affected files.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I started using this script at work, and after using it a few times I discovered a problem. It seemed that when aligning, it would randomly mirror the group to align with the gray layer. I tried changing the values of WXHTolerance = 0.25 and SCALE = 0.15. After changing the values, the problem of the group being randomly mirrored was not solved, and the size recognition would change after changing the values. This is fatal. These files can only accept a 180° rotation, and if they are mirrored, they will completely lose their usability. Can this problem be corrected logically? Or is it feasible to add a code that corrects the mirroring error after running it? The more I tested, the more I found that it would randomly mirror the group left and right when working. This happened to every file, and it had nothing to do with the size of the group (as marked in the picture, when "SCALE = 0.15", the smallest red group would be mirrored, but after changing the value, other groups would be mirrored, and more and more)
Copy link to clipboard
Copied
The problem occurs at the moment of the initial comparison of objects. The script considers that two objects can be aligned with each other if their areas and proportions are as close as possible (compared to all other layers). The situation in which, as a result of such alignment, objects can have a "correct" and "incorrect" orientation is not considered in principle - the main task for the script is to select a position at which the best overlap will be achieved. It is necessary to rework the logic of object comparison, at the moment I do not see a quick and easy solution.
Copy link to clipboard
Copied
I was thinking yesterday that the transformation parameter for 180° rotation is [-100 -100] or [100 100], and the transformation parameter for mirroring is [-100 100] or [100 -100]. Can this condition be used to correct the problem of group mirroring in the code? I tried to use ChatGPT to add a correction function about this parameter. It can indeed help me disable "mirror", but it will cause small-sized groups to be unable to be aligned. Maybe you can refer to whether this logic is feasible. This script was developed by you. If it is feasible, maybe you can give me better suggestions. If it is feasible, I will always use ChatGPT to try to correct it.
Copy link to clipboard
Copied
What is cruser?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now