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

Script to Grab Existing Object and Resize to Current Selection

Explorer ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

Hello,

I am a production artist looking for a script to prevent me from doing a monotomous tasks at work.

Currently when creating proofs we have to resize our heading and grommets and scale them proportionately to fit the height on each side of the flag (front and back).

 

I am looking for a script that grabs the group containing the heading & grommets and scales it to fit the height of the current selection.

Screen Shot 2021-04-22 at 2.05.58 PM.png

Any help would be appreciated thank you!

 

 

 

TOPICS
Scripting

Views

1.3K

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 , Apr 27, 2021 Apr 27, 2021

The message says the error is in line 1. This targets the first and second paths in a selected container (here a group) and deciding, based on their x positions, which is right and which is left. The error imples that the two items in the selected group are not simple paths. So try the following, which targets pageItems (all art items):

 

if (app.selection[0].pageItems[0].position[0] > app.selection[0].pageItems[1].position[0]) {
    var right = selection[0].pageItems[0], left = selection[0].pageI
...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

There you may find some (old) scripts that may still work and do what you are looking for:

 

http://illustrator.hilfdirselbst.ch/dokuwiki/en/skripte/javascript/uebersicht

 

Search for Wolfgang's entries and the russian width and height things.

 

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 ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

An example .ai file is necessary here.

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 ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

Here is an example illustrator file of what I'm looking for.

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 ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

I am unable to properly open your file, probably because I'm using CS6, so I cannot see your paths, groups, etc. This snippet assumes that the header and grommets are the only group in the doc.

 

if (app.selection[0].position[0] > app.selection[1].position[0]) {
    var right = selection[0], left = selection[1];
} else {
    var left = selection[0], right = selection[1];
}
var HG = app.activeDocument.groupItems[0];
var percent = (left.height/HG.height)*100;
HG.resize(percent, percent);
var copy1 = HG.duplicate();
copy1.position = left.position;
var copy2 = HG.duplicate();
copy2.position = [right.position[0]+right.width-copy2.width, right.position[1]];
HG.remove();

 

Untitled1.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 ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Thanks for the help this is looking very promising!  Sorry for saving that file in the newest version maybe try the one below.

The HG is currently a grouped sub-layer named "grab-layer" there will be other groups involved when I am running it.

 

Sorry to sound like a newb, but I am getting this error when I try to run the script above.

Screen Shot 2021-04-23 at 3.43.07 PM.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 ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Still can't open the file properly.  Nevertheless, the error is because the selection was a group (which you mentioned, but I didn't notice).  Assuming the item to be grabbed is named "grab-layer", try this. 

 

if (app.selection[0].pathItems[0].position[0] > app.selection[0].pathItems[1].position[0]) {
    var right = selection[0].pathItems[0], left = selection[0].pathItems[1];
} else {
    var left = selection[0].pathItems[0], right = selection[0].pathItems[1];
}
var HG = app.activeDocument.groupItems["grab-layer"];
var percent = (left.height/HG.height)*100;
HG.resize(percent, percent);
var copy1 = HG.duplicate();
copy1.position = left.position;
var copy2 = HG.duplicate();
copy2.position = [right.position[0]+right.width-copy2.width, right.position[1]];
HG.remove();

 

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 ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Ok this is working! Unfortunately our heading & grommets are in addition to the flag size. so is it possible to get the heading and grommets on the outside of the right and left of selection?

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 ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

if (app.selection[0].pathItems[0].position[0] > app.selection[0].pathItems[1].position[0]) {
    var right = selection[0].pathItems[0], left = selection[0].pathItems[1];
} else {
    var left = selection[0].pathItems[0], right = selection[0].pathItems[1];
}
var HG = app.activeDocument.groupItems["grab-layer"];
var percent = (left.height/HG.height)*100;
HG.resize(percent, percent);
var copy1 = HG.duplicate();
copy1.position = [left.position[0]-copy1.width, left.position[1]];
var copy2 = HG.duplicate();
copy2.position = [right.position[0]+right.width, right.position[1]];
HG.remove();

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 ,
Apr 27, 2021 Apr 27, 2021

Copy link to clipboard

Copied

This script is working for the example file! but unfortunately this script is still not working for me when I go to implement it in my daily routine.  I'm trying to understand why, but I can't wrapped my head around what this script is doing.

Here is an example video of the process I'm doing daily.

 

 

Thanks!

 

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 ,
Apr 27, 2021 Apr 27, 2021

Copy link to clipboard

Copied

The message says the error is in line 1. This targets the first and second paths in a selected container (here a group) and deciding, based on their x positions, which is right and which is left. The error imples that the two items in the selected group are not simple paths. So try the following, which targets pageItems (all art items):

 

if (app.selection[0].pageItems[0].position[0] > app.selection[0].pageItems[1].position[0]) {
    var right = selection[0].pageItems[0], left = selection[0].pageItems[1];
} else {
    var left = selection[0].pageItems[0], right = selection[0].pageItems[1];
}
var HG = app.activeDocument.groupItems["grab-layer"];
var percent = (left.height/HG.height)*100;
HG.resize(percent, percent);
var copy1 = HG.duplicate();
copy1.position = [left.position[0]-copy1.width, left.position[1]];
var copy2 = HG.duplicate();
copy2.position = [right.position[0]+right.width, right.position[1]];
HG.remove();

 

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 ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

Thank you for your help and breaking it down for me!  Is it possible to reform this to make it work on the visible bounds of the whole selection and then do the hg.resize from that information?  Sometimes there are clipping masks involved and it pulls the overall height/width from that.

 

 

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 ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

It may be possible, but there are few unknowns, least of all:  Are the visible bounds of the "grab-layer" to be used too?  If so, the the header will have to be targeted for its stroke width, so either it has to be named or its order within the "grab-layer" group has to be known.  All in all, it would be more straightforward if this was not the case. 

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 ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

Yeah nothing in the "grab-layer" group would have a stroke around it.  Nor would anything in the selected group because I outline the strokes when scaling the art for the proofs.

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 ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

I wonder if we are at cross purposes. To me visible bounds are an item's bounds + stroke width. But you say the selected group doesn't have a stroke. Also you mention clipping masks; I wonder if you mean the bounds of the clipping path. I think it's best if you could show what you mean with screenshots (preferably with the layers expanded to see the contents, order, etc.). 

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 ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

Hopefully this video is helpful in explaining.  You can see the bounds I'm talking about at the end of the video.  It is the height and width of everything that is visible excluding the "grab-layer" of course.

Screen Shot 2021-04-29 at 4.55.37 PM.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 ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

LATEST

There isn't an automatic way to decide what's visible. In this case, the clipping paths completely cover the masked artwork, so they can be used. They are targeted based on the fact that they are the frontmost paths in clipping sets. So there are many variables that may cause errors, but hopefully this will work for the time being.

 

if (app.selection[0].pageItems[0].position[0] > app.selection[0].pageItems[1].position[0]) {
    var right = selection[0].pageItems[0], left = selection[0].pageItems[1];
} else {
    var left = selection[0].pageItems[0], right = selection[0].pageItems[1];
}

if (right.typename == "GroupItem" && right.clipped == true) {
    var right = right.pathItems[0];
}
if (left.typename == "GroupItem" && left.clipped == true) {
    var left = left.pathItems[0];
}

var HG = app.activeDocument.groupItems["grab-layer"];
var percent = (left.height/HG.height)*100;
HG.resize(percent, percent);
var copy1 = HG.duplicate();
copy1.position = [left.position[0]-copy1.width, left.position[1]];
var copy2 = HG.duplicate();
copy2.position = [right.position[0]+right.width, right.position[1]];
HG.remove();

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