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

Script for adding guides in the centerpoint of an object

Participant ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Hello, community.

 

I've run across this issue in my workflow, and I am pretty sure this can be solved via scripting. I am looking for a script that can add guides (both horizontal and vertical) to the centerpoint of a selected object.

 

By centerpoint, I understand the intersection of the middle points for both the object's width and height.

 

Looking forward to hearing back whether such a solution already exists (I've tried finding one, without any luck) or can be done. Thank you!

TOPICS
Scripting

Views

492

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

correct answers 1 Correct answer

Guide , Aug 24, 2022 Aug 24, 2022
// select item
var doc = app.activeDocument;
var ABs = doc.artboards;
var index = ABs.getActiveArtboardIndex();
var ABR = ABs[index].artboardRect;
var bounds = app.selection[0].geometricBounds;
var centreX = bounds[0] + (app.selection[0].width / 2);
var centreY = bounds[1] - (app.selection[0].height / 2);
var path1 = doc.pathItems.add();
var path2 = doc.pathItems.add();
path1.setEntirePath([[centreX, ABR[1]], [centreX, ABR[3]]]);
path2.setEntirePath([[ABR[0], centreY], [ABR[2], centreY]]);
doc.s
...

Votes

Translate

Translate
Adobe
Guide ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

This should be easily doable.  If you don't get an answer before then, I'll do it after work today.  

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 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Not a script, but an action.

 

Bounding Box Centre Guides

 

It's meant for single selected objects. Just select one and run the action.

 

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
Participant ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

Thank you, Kurt. This will be definitely most useful to someone. However, I'm more of a script guy, since I'm using a script manager plugin that allows me to create GUI items out of them. I appreciate it nonetheless!

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
Guide ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

// select item
var doc = app.activeDocument;
var ABs = doc.artboards;
var index = ABs.getActiveArtboardIndex();
var ABR = ABs[index].artboardRect;
var bounds = app.selection[0].geometricBounds;
var centreX = bounds[0] + (app.selection[0].width / 2);
var centreY = bounds[1] - (app.selection[0].height / 2);
var path1 = doc.pathItems.add();
var path2 = doc.pathItems.add();
path1.setEntirePath([[centreX, ABR[1]], [centreX, ABR[3]]]);
path2.setEntirePath([[ABR[0], centreY], [ABR[2], centreY]]);
doc.selection = null;
path1.selected = true;
path2.selected = true;
app.executeMenuCommand("makeguide");

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 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Nice, Femke. looks great. My only beef is the ever present issue in visible/geometricBounds and clip masks. clipped artwork could break this. I utilized @jduncan 's getVisibleBounds() function for my stab at this. But reading through your code gives me another idea of how to tackle the problem without the added dependencies that my script requires...

 

one could forget all about the geometric bounds of the artwork itself, since that property can't be fully trusted.. And instead, we could create a temporary artboard and use executeMenuCommand to fit the temp artboard to the current art.. then simply extract the centerpoint from the artboardRect, then scrap the temporary artboard. 

 

lastly, you needn't use executeMenuCommand for making guides. In fact, i recommend against it because it creates an unnecessary undo step which makes it trickier to revert changes made by the script. the pathItem object has a "guides" property which is a boolean that determines whether it's a guide or not. Simply set PathItem.guides = true, and you're good to go. So you can save yourself two selected items and one menuCommand by using this instead:

 

path1.guides = path2.guides = true;

 

edit** it also just occurred to me that your function here might produce undesired results if the selected artwork is not on the artboard. It will have long guidelines that extend way off the actual artwork until it reaches the edge of the artboard, since you're using the bounds of the active artboard to draw your pathpoints. Safer to use the true visible bounds of the artwork itself as opposed to tying the guides to an artboard that may or may not be relevant.

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
Guide ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

@Disposition_Dev  Thanks for the tips, particularly the one regarding the "guides" property (which I was not aware of).  I prefer shorter code, even if it's by three lines. 

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
Participant ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

LATEST

Thank you very much, @femkeblanco . This absolutely did the trick.

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 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

i've written a public community utility for aligning objects via script which you can find here: https://github.com/wdjsdev/public_illustrator_scripts/blob/master/alignment_functions.js

(the alignment functions also have a dependency called "object_bounds_data.js" which can be found in the same public_illustrator_scripts repo). 

 

Using that, you can easily create a crosshair, and then align it to your selected object(s). Just save this code below into a new .js(x) file (or clone it from the github repo), select the item(s) in your document for which you want to draw guides, then execute this script and it will create a 10x10pt crosshair in the center of each selected item. 

 

You can preset the crosshair size by updating the "GUIDE_SIZE" constant. And you can determine whether to make true illustrator "guides" (the kind that are visible in the UI even if stuff is stacked on top of them, but won't print or display on any exported artwork), or you can choose to just make simple paths of a given stroke color. To toggle this setting, update the "MAKE_CROSSHAIRS_INTO_GUIDES" constant. true means they'll be guides. false means they'll be stroked paths.

 

Here's the link to the github page for this script: https://github.com/wdjsdev/public_illustrator_scripts/blob/master/crosshair_at_center.jsx

 

Let me know how it goes.

 

//author: William Dowling
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//github: https://github.com/wdjsdev
//Adobe Discussion Forum Post about this: https://community.adobe.com/t5/illustrator-discussions/script-for-adding-guides-in-the-centerpoint-of-an-object/td-p/13155180
//github link for this project: https://github.com/wdjsdev/public_illustrator_scripts/blob/master/crosshair_at_center.jsx

//*******//

//Did you find this useful? Would you like to buy me a cup (or a pot?) of coffee to say thanks?
//paypal.me/illustratordev
//<3

//*******//


//
//Draw crosshairs at center of selected items
//
//given at least one selected item, this function will draw crosshairs at the center of each item.


//arguments
//none. just select the items first and run the script.

//preferences
// MAKE_CROSSHAIRS_INTO_GUIDES:  boolean to determine whether the crosshairs should be 
//drawn as guides or as simple stroked paths.
    //if true, crosshairs will be converted to true illustrator guides.
    //if false, crosshairs will be drawn as simple stroked paths.

//GUIDE_SIZE: the height/width of the crosshairs in points.

//dependencies:
//alignment_functions.js: https://github.com/wdjsdev/public_illustrator_scripts/blob/master/alignment_functions.js
#include "alignment_functions.js"
    //the alignment functions also has a sub dependency called "object_bounds_data.js"
    //which is located here: https://github.com/wdjsdev/public_illustrator_scripts/blob/master/object_bounds_data.js
function makeCrosshairsAtCenter()
{
    //boolean to determine whether the crosshairs drawn should be
    //converted into true illustrator "guides" or not.
    //If false, crosshairs will be drawn as simple lines with a
    //stroke color of "crosshairColor" which can be customized below.
        //currently set to CMYK with 100% values for each. But you can choose
        //rgb, lab, etc. and set any values you want.
    const MAKE_CROSSHAIRS_INTO_GUIDES = true;

    const GUIDE_SIZE = 10; //number in points representing the width/height of the crosshair

    var doc = app.activeDocument;
    var layers = doc.layers;
    var sel = doc.selection;
    if(!sel.length)
    {
        alert("Please select at least one page item and try again.");
        return;
    }

    var crosshairColor = new CMYKColor();
    crosshairColor.cyan = 100;
    crosshairColor.magenta = 100;
    crosshairColor.yellow = 100;
    crosshairColor.black = 100;


    //since i don't know exactly what you want, i'm going to create a new layer
    //to plop all the crosshairs on. this helps avoid issues with trying to draw
    //or manipulate artwork on locked/hidden layers. If the items you're drawing
    //guides for are groupItems, you could simply put the crosshairs inside that groupItem.
    //entirely your call. Just update the "dest" argument in the "drawCrosshair" function call
    //to 

    //identify existing center guides layer, or create one
    var guidesLayer = (function()
    {
        for(var l=0;l<layers.length;l++)
        {
            if(layers[l].name === "Center Guides")
                return layers[l];
        }
    })() || layers.add();
    guidesLayer.name = "Center Guides";
    

    //loop through selected items and draw crosshairs on each item
    var curItem,curGuideGroup,curLine;
    for(var x=0;x<sel.length;x++)
    {
        curItem = sel[x];
        
        //create a crosshair group on the guides layer
        //change the second argument to "curItem" if you want the crosshairs to be drawn on the item itself
        //currently, curItem would need to be a groupItem.. But if need be, we can add logic to create
        //the necessary group structure and place curItem inside along with the guide. Let me know.
        drawCrosshair(curItem,guidesLayer);
    }


    function drawCrosshair(itemToGuideify,guideDest)
    {
        //make a group to hold the crosshair lines
        //this makes it easier to move stuff around later.
        //potentially this group could be named to match whatever
        //item it's marking. But it depends on whether the items you're
        //marking centerpoints for are named and whether it would even be
        //helpful to have the crosshair groups named. Up to you.
        //let me know if you want to change this and need some help.
        var crosshairGroup = guideDest.groupItems.add();


        //draw the horizontal line
        var crosshairLine1 = crosshairGroup.pathItems.add()
        crosshairLine1.setEntirePath([[0,0],[GUIDE_SIZE,0]]);
        crosshairLine1.filled = false;
        crosshairLine1.stroked = false; 
        
        //draw the vertical line
        var crosshairLine2 = crosshairGroup.pathItems.add()
        crosshairLine2.setEntirePath([[GUIDE_SIZE/2,GUIDE_SIZE/2],[GUIDE_SIZE/2,-GUIDE_SIZE/2]]);
        crosshairLine2.filled = false;
        crosshairLine2.stroked = false;

        //align the crosshairGroup to the center of the desired object
        //hint: you could use this very same logic to add "trim marks" to the corners of the object.
            //simply change last argument from "center" to "topLeft", then duplicate the crosshairGroup
            //and use the "align" function to align the duplicate to the top right corner of the object.
            //repeat for bottom right and bottom left.
        align(itemToGuideify,[crosshairGroup],"center");
        
        //check whether to convert to guides or not
        MAKE_CROSSHAIRS_INTO_GUIDES ? crosshairLine1.guides = true : crosshairLine1.strokeColor = crosshairColor;
        MAKE_CROSSHAIRS_INTO_GUIDES ? crosshairLine2.guides = true : crosshairLine2.strokeColor = crosshairColor;

        
    }
    
}
makeCrosshairsAtCenter();

 

 

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
Participant ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

Hi, Dilliam.

 

Unfortunately, this didn't seem to work for me out of the bat, probably due to its dependencies on other libraries. That makes it a bit harder for me to manage, so I am looking for a much simpler solution like @femkeblanco 's above. I'm sure this will come in handy for someone else browsing the forum for a similar problem. Thank you!!

 

P.S. Will take a closer look at your utility when my time allows me to play around with it a bit. It sounds truly amazing. Thanks for all the great work you're putting out there!

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