Library for aligning objects via script.
Edit*** Resurrecting this post because i just made some big improvements to this "library". Updates are as follows:
- simplify logic and condense into one function called align() that accepts 3 required arguments
- key: any pageItem or artboard
- otherObjects: array of pageItems
- alignType: string representing the alignment option
- alignType options are: "center", "vcenter", "vtop", "vbottom", "hcenter", "hleft", "hright", "botleft", "botright", "topleft", "topright", "leftcenter", "rightcenter", "topcenter", "botcenter"
- Improve error handling for edge cases
- Included logic from a dependency called "Object Bounds Data", which when given a pageItem, it returns the actual true visible bounds of the artwork, without including any invisible clipped artwork. This ensures that the alignment functions behave just like the UI align tools, even though the script sees object bounds differently.
- this dependency logic can be found here: https://github.com/wdjsdev/public_illustrator_scripts/blob/master/object_bounds_data.js
**End Edit
Do you want to align objects via script, but just don't know how to go about it? Or if you do know how, do you find yourself constantly drawing little pictures to try and remember the correct equation to align 2 objects in the correct way (do i divide this one by 2? do i add these values or subtract them?)? Do you find that since you don't write the logic every day, you always yourself getting it wrong all the time and being frustrated while you keep trying different configurations of the equation until it works? Do you wish there was a super easy to use library that gave you easy access to named alignment functions that mimic the behavior of the UI's alignment tools (including key object alignment)?
Wish no more! Here are those alignment functions, free for you to use or adapt or whatever you want. Simply feed any of these functions a "key object" (any pageItem), and an array of pageItems you want to align to the key object.
https://github.com/wdjsdev/public_illustrator_scripts/blob/master/alignment_functions.js
And here's a demo video:
https://photos.app.goo.gl/JunYSy2368DbRhHL8
Edit: here's a fixed demo. In the first demo, i had forgotten a function call in the test function that caused improper "bottom right" alignment.
https://photos.app.goo.gl/jYHqHXMdLRDHSsNXA
functions include:
- alignObjectsToCenter effectively does a veritcal align center and horizontal align center.
- vAlignTop aligns the top edge of all objects to the top edge of the key object
- vAlignCenter aligns the vertical center of all objects to the vertical center of key object
- vAlignBottom aligns all bottom edges to key object
- hAlignLeft aligns all left edges to left edge of key object
- hAlignCenter aligns all horizontal center points to horizontal center point of key object
- hAlignRight aligns all right edges to right edge of key object
future plans...? distribute functions. 😃
If you find these useful.. Please consider buying me a cup of coffee at the link in the js file. 😃
If you'd like to extend this functionality in some way that better fits your workflow, contact me at illustrator.dev.pro@gmail.com
If you've wanted this kind of library for some other functionality, but aren't sure how to go about it... Let me know.
All the best, and happy coding.
William
