• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
8

Library for aligning objects via script.

Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

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.

**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

TOPICS
Scripting , SDK , Tools

Views

800

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

@Disposition_Dev 

Nice work and thanks for sharing with all of us.

Thank you. 😊 

One question: Do we need to make an object as a key object manually?

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

You're not really setting any object as a true "key object" like you do in the UI. It's just that whatever item you pass to the functions as the first argument, all other items in the second argument array will be aligned relative to that object.

 

You needn't do anything in the UI at all. You could create some items via script, then pass them as arguments to be aligned. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

Got it. 

Thanks for the public library.

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

Hi @Disposition_Dev 

nice work.

😉

 

(Just a side note: bottom right doesn't work properly in your video.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

Haha. Doh!

 

Fortunately that function is just a combination of the align right and align bottom functions and we know those both work from the other examples.

 

Just rushing for some reason, I guess. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

fixed. 😃

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

awesome! thanks for sharing William!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

You guys have all given me so much over the last 5 years... It's only right to try and return the favor. And if I can get some experience writing code that will be used by others instead of just for myself, I'll be better suited to working on a team in the future.

 

Getting public comment and criticism is also invaluable as well, but almost all the code I've ever written has been seen only by me or a client that doesn't know how to code.

 

To that end, I'll be writing more and more stuff for free use and posting it around here. Hopefully that'll end up growing into a large library of useful stuff that will make scripting illustrator more accessible to everyone. And as mentioned, it'll give ample opportunity for the bright minds around here to tear my code apart and ask me, "why on earth would you do it THAT way?" or "did you ever consider [edge case(s)]??" ultimately resulting in a community driven project designed to fill some holes in the API. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

I have done some experiments this year as a basis for a blog that goes over some item-moving principles, I'll have to dig those up and do some screenshots and make the blog to post on here. Hope to get to it before a few weeks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Sweet! Thanks for sharing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2022 Aug 18, 2022

Copy link to clipboard

Copied

LATEST

bumping to the top for visibility of recent update.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines