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

script drawing crosses

Explorer ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Hello.
I believe in the power of this forum, once a friend helped me write a script, for which I thank you again!
Is anyone able to write me a skeleton script that will insert/draw crosses like in the screen for each selected grouped element? Such a cross should be 7mm in size and be at least 7mm away from the object.
Both crosses should be in one line. Importantly, next to the right cross on the right side there should be the inserted text from the variable that I should enter, while at the bottom the variable counting +1 to each object (more or less like on the screen)
The crosshairs should be the same color as the selected objects (usually black or a secondary swatch).

Please help 
Zrzut ekranu 2022-12-14 o 07.51.10.pngZrzut ekranu 2022-12-14 o 07.50.01.png

TOPICS
Scripting , Tools

Views

1.0K

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 2 Correct answers

Guide , Dec 14, 2022 Dec 14, 2022
// select targets
var input = prompt("");
var w = 7 * 2.835;
var gap = w;
var weight = 0.25 * 2.835;
var layer1 = app.activeDocument.layers.add();
layer1.name = "+";
var color = new CMYKColor();
color.black = 100;
var counter = 1;
for (var i = 0; i < app.selection.length; i++) {
    if (app.selection[i].typename == "GroupItem") {
        var group = layer1.groupItems.add();
        var b = app.selection[i].geometricBounds;
        var y = b[1] + ((b[3] - b[1]) / 2);
        var x1 = b[0] - gap -
...

Votes

Translate

Translate
Guide , Dec 19, 2022 Dec 19, 2022

For all five crosses:

 

var w = 7 * 2.835;
var gap = w;
var doc = app.activeDocument;
var color = new CMYKColor();
color.black = 100;
for (var i = 0; i < app.selection.length; i++) {
    if (app.selection[i].typename == "GroupItem") {
        var b = app.selection[i].geometricBounds;
        var y = b[1] + ((b[3] - b[1]) / 2);
        var x1 = b[0] - gap - w / 2, x2 = b[2] + gap + w / 2;
        var x = b[0] + ((b[2] - b[0]) / 2);
        var y1 = b[1] + gap + w / 2, y2 = b[3] - gap - w / 2;
     
...

Votes

Translate

Translate
Adobe
Guide ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

Regarding the text, (1) is the input variable (the top line) to be hardwired into the script, (2) is the number (the bottom line) just a count of the items, and (3) what size and font? 

 

This should do the crosses (without the text).  Also, group items don't have colors, so color is black.  

 

// select targets
var w = 7 * 2.835;
var gap = w;
var doc = app.activeDocument;
for (var i = 0; i < app.selection.length; i++) {
    if (app.selection[i].typename == "GroupItem") {
        var bounds = app.selection[i].geometricBounds;
        var y = bounds[1] + ((bounds[3] - bounds[1]) / 2);
        var x1 = bounds[0] - gap - w / 2;
        var x2 = bounds[2] + gap + w / 2;
        function draw(top, left, w, p) {
            var square = doc.pathItems.rectangle(
                top, left, w, w);
            var line1 = doc.pathItems.add();
            line1.setEntirePath([[p - w, y], [p + w, y]]);
            var line2 = doc.pathItems.add();
            line2.setEntirePath([[p, y - w], [p, y + w]]);
            square.strokeWidth = line1.strokeWidth = line2.strokeWidth = 1;
            var color = new RGBColor();
            color.red = color.green = color.blue = 0;
            square.strokeColor = line1.strokeColor = line2.strokeColor = color;
            square.filled = false;
        }
        draw(y + w / 4, x1 - w / 4, w / 2, x1);
        draw(y + w / 4, x2 - w / 4, w / 2, x2);
    }
}

 

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
Explorer ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

Thank you again for your help!
you are great


1. yes, there should be one and the same number in the entire document, but each time the script is run, it wants to enter this number (because each project has a different number).

2. yes, it's supposed to be the number of objects. Each object should be numbered from 1
3. Myriad Pro. Regular, 12pt


What you wrote works, but only on one object (despite selecting several)
I forgot to add that the thickness of the crosses should be 0.25mm and that it would create a new layer called "+"

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
Explorer ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

As for the color, if they can't be the same color as the group, it's like they can be 100% black in the CMYK palette

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
Explorer ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

I've already converted to CMYK
var color = new CMYKColor();
color.black = 100;
And the code works on multiple objects though (I checked it wrong, sorry)

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 ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

// select targets
var input = prompt("");
var w = 7 * 2.835;
var gap = w;
var weight = 0.25 * 2.835;
var layer1 = app.activeDocument.layers.add();
layer1.name = "+";
var color = new CMYKColor();
color.black = 100;
var counter = 1;
for (var i = 0; i < app.selection.length; i++) {
    if (app.selection[i].typename == "GroupItem") {
        var group = layer1.groupItems.add();
        var b = app.selection[i].geometricBounds;
        var y = b[1] + ((b[3] - b[1]) / 2);
        var x1 = b[0] - gap - w / 2;
        var x2 = b[2] + gap + w / 2;
        function draw(top, left, w, p) {
            var square = layer1.pathItems.rectangle(top, left, w, w);
            var l1 = layer1.pathItems.add();
            l1.setEntirePath([[p - w, y], [p + w, y]]);
            var l2 = layer1.pathItems.add();
            l2.setEntirePath([[p, y - w], [p, y + w]]);
            l2.strokeWidth = l1.strokeWidth = square.strokeWidth = weight;
            l2.strokeColor = l1.strokeColor = square.strokeColor = color;
            square.filled = false;
            square.moveToEnd(group);
            l1.moveToEnd(group);
            l2.moveToEnd(group);
        }
        draw(y + w / 4, x1 - w / 4, w / 2, x1);
        draw(y + w / 4, x2 - w / 4, w / 2, x2);
        function comp(p, content) {
            var t = layer1.textFrames.pointText([x2 + w / 2, y + p]);
            t.contents = content;
            t.textRange.textFont = app.textFonts["MyriadPro-Regular"];
            t.textRange.strokeWeight = weight;
            t.textRange.fillColor = t.textRange.strokeColor = color;
            t.moveToEnd(group);
        }
        comp(w / 4, input);
        comp(- w * 3 /4, counter);
        counter++;
    }
}

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
Explorer ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

It works!
Two help is invaluable!
One more question, it will be possible to add something to support the colors of the additional sample, i.e. if I enter the name of the additional sample in the variable, it will insert it all with this color sample?

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 ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

Sorry, I don't understand the last question. 

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
Explorer ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

Sorry, my English is very basic

Zrzut ekranu 2022-12-14 o 22.27.25.png
I have added custom colors, additional samples.
Is there a possibility that, for example, after entering the name of this additional color, the script will work in this selected color?

I forgot the most important
They must have the fill attribute and the outline of the overprint turned on.

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 ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

If I understand correctly, you want to input the name of a swatch to use its color. If this is the case, change

var color = new CMYKColor();
color.black = 100;

to

var color = app.activeDocument.swatches[prompt("")].color;

 

Or, if the swatch is always going to be called "red", to

var color = app.activeDocument.swatches["red"].color;

 

Sorry, again, I don't understand the comment about the fill. There is no fill. Do you want a white fill? 

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
Explorer ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

I mean that the vertical and horizontal line have a white fill, basically it doesn't bother me so let's leave this topic.

It works with color change, but...
I'm trying to do if - if I enter "1" in the name of the color, it should draw in black, if I enter the name of the color, it should do with this color

var kolor = prompt("kolor");
if (kolor.text == "1")
{
var color = new CMYKColor();
color.black = 100;
}
else {
var color = app.activeDocument.swatches[prompt("")].color;
}

 And what do I need to add to enable the attribute of filling the print with the outline of the print?

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
Explorer ,
Dec 15, 2022 Dec 15, 2022

Copy link to clipboard

Copied

I noticed that when there are more colors in a group, the script can draw crosses without an outline, this is not always the case

Zrzut ekranu 2022-12-15 o 10.15.54.pngZrzut ekranu 2022-12-15 o 10.15.49.png

 

 

 

 

 

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
Explorer ,
Dec 15, 2022 Dec 15, 2022

Copy link to clipboard

Copied

I apologize in advance for so many posts. I'll try to include everything here.
So...
I extended the script with a pop-up window etc. Below it is attached.
The problem is that with groups with more elements, the crosses are drawn without any outline (I attached the screen in the above post)
I noticed that sometimes, despite the set thickness of 0.25mm, it will give an outline with a thickness of 1pt, or it will not make it at all.
I would also like them to have the overprint fill attribute and the overprint outline turned on

 

 

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 ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

I can't replicate the problem with the stroke and weight. They should be unrelated to anything outside the script. To make sure the paths are stroked, you could add

square.stroked = l1.stroked = l2.stroked = true;

To add overprint

square.fillOverprint = square.strokeOverprint = true;
l1.fillOverprint = l1.strokeOverprint = true;
l2.fillOverprint = l2.strokeOverprint = true;

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
Explorer ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

I have already dealt with the overprint and wrote exactly as you 🙂

As for the outline, adding what you wrote seems to have solved the problem. Thank you for help.

I added a bit more to the code, added the input from which number the elements should be counted, and if I want them in the color of the additional sample, I give the name (if not, they should be black) - I'm just wondering if it can be better solved

Would you be able to give me a skeleton code for drawing crosses to add them also centrally in the middle (vertical and horizontal), and at the top and bottom at 7mm distance from the object?

More specifically, it's about adding a cross
- a cross in the center of X and Y in line with those on the sides (as an if option, but I can handle it)
- two crosses in the X axis in the center of the object, and Y at the top at a distance of 7mm from the object, and at the bottom

Zrzut ekranu 2022-12-19 o 14.53.47.png

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 ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

For all five crosses:

 

var w = 7 * 2.835;
var gap = w;
var doc = app.activeDocument;
var color = new CMYKColor();
color.black = 100;
for (var i = 0; i < app.selection.length; i++) {
    if (app.selection[i].typename == "GroupItem") {
        var b = app.selection[i].geometricBounds;
        var y = b[1] + ((b[3] - b[1]) / 2);
        var x1 = b[0] - gap - w / 2, x2 = b[2] + gap + w / 2;
        var x = b[0] + ((b[2] - b[0]) / 2);
        var y1 = b[1] + gap + w / 2, y2 = b[3] - gap - w / 2;
        function draw(top, left, w, p1, p2) {
            var square = doc.pathItems.rectangle(top, left, w, w);
            var line1 = doc.pathItems.add();
            line1.setEntirePath([[p1 - w, p2], [p1 + w, p2]]);
            var line2 = doc.pathItems.add();
            line2.setEntirePath([[p1, p2 - w], [p1, p2 + w]]);
            square.strokeWidth = line1.strokeWidth = 
                line2.strokeWidth = 1;
            square.strokeColor = line1.strokeColor = 
                line2.strokeColor = color;
            square.filled = false;
        }
        draw(y + w / 4, x1 - w / 4, w / 2, x1, y);  // R
        draw(y + w / 4, x2 - w / 4, w / 2, x2, y);  // L
        draw(y1 + w / 4, x - w / 4, w / 2, x, y1);  // top
        draw(y2 + w / 4, x - w / 4, w / 2, x, y2);  // bottom
        draw(y + w / 4, x - w / 4, w / 2, x, y);  // centre
    }
}

 

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
Explorer ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

Thank you for your help.
I want to add what you wrote to the previous code I have, but something is changed and I can't figure out which variables are which.
Middle and left and right works, while top and bottom only draw a square

 

 

 

    for (var i = 0; i < app.selection.length; i++) {
        if (app.selection[i].typename == "GroupItem") {
            var group = layer1.groupItems.add();
            var b = app.selection[i].geometricBounds;
            var y = b[1] + ((b[3] - b[1]) / 2);
           var x1 = b[0] - gap - w / 2, x2 = b[2] + gap + w / 2;
            var x = b[0] + ((b[2] - b[0]) / 2);
            var y1 = b[1] + gap + w / 2, y2 = b[3] - gap - w / 2;
            function draw(top, left, w, p, p1, p2) {
                var square = layer1.pathItems.rectangle(top, left, w, w);
                var l1 = layer1.pathItems.add();
                l1.setEntirePath([[p - w, y], [p + w, y]]);
                var l2 = layer1.pathItems.add();
                l2.setEntirePath([[p, y - w], [p, y + w]]);
                square.stroked = l1.stroked = l2.stroked = true;
                l2.strokeWidth = l1.strokeWidth = square.strokeWidth = weight;
                l2.strokeColor = l1.strokeColor = square.strokeColor = color;
                l1.strokeOverprint = l2.strokeOverprint = square.strokeOverprint = l2.fillOverprint = l1.fillOverprint = true;
                square.filled = l2.filled = l1.filled = false;
                square.moveToEnd(group);
                l1.moveToEnd(group);
                l2.moveToEnd(group);
            }
            draw(y + w / 4, x1 - w / 4, w / 2, x1); // L
            draw(y + w / 4, x2 - w / 4, w / 2, x2); // R

         
           if (srodek.value == true){
             draw(y + w / 4, x - w / 4, w / 2, x, y); // centre
             draw(y1 + w / 4, x - w / 4, w / 2, x, y1);  // top
             draw(y2 + w / 4, x - w / 4, w / 2, x, y2);  // bottom
            }

            //TEXT
            function comp(p, content) {
                var t = layer1.textFrames.pointText([x2 + w / 2, y + p]);
                t.contents = content;
                t.textRange.textFont = app.textFonts["MyriadPro-Regular"];
                t.textRange.fillColor = color;
                t.textRange.overprintFill = true;
                t.moveToEnd(group);
            }
            comp(w / 4, input.text);
            
            //number
            if (ifnumber.value == false){
               comp(- w * 3 /4, counter.text);
               counter.text++;            
           }

        }
    }

 

 

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 ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

The draw function changed.  There's a an added variable to pass to it and a change in the body.  This is the part that changed

function draw(top, left, w, p1, p2) {
    var square = doc.pathItems.rectangle(top, left, w, w);
    var line1 = doc.pathItems.add();
    line1.setEntirePath([[p1 - w, p2], [p1 + w, p2]]);
    var line2 = doc.pathItems.add();
    line2.setEntirePath([[p1, p2 - w], [p1, p2 + w]]);
    // ...
}

 

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
Explorer ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

I renamed all the variables and it worked! Thank you

Now I'm wondering if it is possible (maybe if) for the script to check the size of elements, and if it is smaller than, for example, 400mm, it would not insert crosses at the top and bottom, and if it is larger, it would insert it.

I think it should be something like this


if (formula to check the height of objects){
draw(y1 + w / 4, x - w / 4, w / 2, x, y1); // top
draw(y2 + w / 4, x - w / 4, w / 2, x, y2); // bottom
}


I guess I'm asking too much :x 

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 ,
Dec 20, 2022 Dec 20, 2022

Copy link to clipboard

Copied

Not a problem.

if (app.selection[i].height > 400 * 2.835) {
    draw(y1 + w / 4, x - w / 4, w / 2, x, y1);  // top
    draw(y2 + w / 4, x - w / 4, w / 2, x, y2);  // bottom
}

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
Explorer ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

Thank you for help. I don't think I can squeeze any more, we did everything I wanted 🙂

A little question from a slightly different topic.

 

And is it possible to set the color of the created new layer. I noticed that it often creates a yellow color, it is hardly visible and I have to manually change it to any other color.
I found some ready command, but it won't cooperate


What do I have to enter to make the checkbox checked by default after enabling the script? The "active onclick" function does not work and any other code combination below

 

 

 

            name.onClick = function () {
                restoreDefaultsButton.enabled = true;
                name.active = false;
                name.active = true;
            } 

 

 

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 ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

To set a layer's "color", e.g. green:

var colorx = new RGBColor();
colorx.red = colorx.blue = 0;
colorx.green = 255;
var layerx = app.activeDocument.layers.add()
layerx.color = colorx;

 

 To have a checkbox checked, assign it's "value" true. 

var w = new Window("dialog");
var check1 = w.add("checkbox");
check1.value = true;
var button1 = w.add("button", undefined, "OK");
button1.onClick = function () {
    if (check1.value) alert(true)
    else alert(false);
}
w.show ();

 

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
Explorer ,
Jan 02, 2023 Jan 02, 2023

Copy link to clipboard

Copied

LATEST

Hi @femkeblanco !
I have another question.
Is it possible to display all the color swatches in the document in a script?
For example, after expanding the list, a window with all samples from the active open document/file will be displayed?

Such a list can be with names only if it is to be simpler

en_03.png

 

And one more thing. At the moment the code works so that each item must be in a group for it to insert crosses. Is it possible that if it also selects an element that is not in any group, it will also consider it?

EDIT: 
I managed to write it with color -
I hooked it up to the script so that it doesn't give the color name, just choose from a list.
Below is the code that displays all color swatches and displays an alert when a specific color is selected

var swatchList = app.activeDocument.swatches;
var w = new Window("dialog", "Lista próbek");
var swatchListGroup = w.add("group");
swatchListGroup.orientation = "column";
var swatchDropdownList = swatchListGroup.add("dropdownlist");
for(var i = 0; i < swatchList.length; i++) {
  swatchDropdownList.add("item", swatchList[i].name);
}
var selectButton = swatchListGroup.add("button", undefined, "GO!");
selectButton.onClick = function() {
  var cusColor = swatchDropdownList.selection.text;
  alert("Color: " + cusColor);
}
w.show();

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