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

Wanting to move an object with javascript to specific coordinates

Participant ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

I can't seem to get the move method to work, it lists move( relativeObject, insertionLocation ) or would I use the transform method here instead?

 

I can't seem to find examples of move or transform being used unfortunately.

TOPICS
Scripting

Views

2.1K

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 , Jul 26, 2020 Jul 26, 2020

move(), as I understand it, corresponds to the "Arrange" command in the Object menu.  So, it "moves" a path above or below another path (in the example below, named path1 and path2).

 

var paths = app.activeDocument.pathItems;
paths["path1"].move(paths["path2"], ElementPlacement.PLACEAFTER);

 

It sounds like what you want is translate(dx, dy), which moves the path by dx and dy distances from the present position.  So

 

paths["path1"].translate(100, 100);

 

moves the path 100 points in the positive x dir

...

Votes

Translate

Translate
Adobe
Guide ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

move(), as I understand it, corresponds to the "Arrange" command in the Object menu.  So, it "moves" a path above or below another path (in the example below, named path1 and path2).

 

var paths = app.activeDocument.pathItems;
paths["path1"].move(paths["path2"], ElementPlacement.PLACEAFTER);

 

It sounds like what you want is translate(dx, dy), which moves the path by dx and dy distances from the present position.  So

 

paths["path1"].translate(100, 100);

 

moves the path 100 points in the positive x direction and 100 points in the positive y direction.

 

Alternatively, you can assign new values for the position property:

 

paths["path1"].position = [x, y];

 

I've not figured out how transform and transformation matrices work. 

 

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

Nice!  .position is what I was looking for. 

 

Without having to complicate things and introduce me having to do math with the visible bounds, is there a way to get the .position to apply to the mask of a clipped group?  Unfortunately the .position factors in the whole content and not the mask.

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

Most items have a position property.  So in the clipping mask from the preceding thread (Trying to assign a variable name to a masked object using a menu command in jsx), you can reassign the position of the whole "clipping set" group,

clippingSet.position = [x, y];

just the "clipping path"

clippingSet.pathItems["clippingPath"].position = [x, y];

or just the "masked artwork".

clippingSet.pathItems["maskedArtwork"].position = [x, y];

 

The key is getting the hang of Illustrator's object model to be able to reference the items you want. 

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

Yeah I'm pretty sure I'm setting the position of the entire set and it's not working,.. it's moving the entire set, which is what I want, ... but it's using the masked artwork as what determines the position.

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

Yeah if I run this code in an existing document, the final .position line of code doesn't put the circle at [0,0], but instead the square.

var myDoc = app.activeDocument;
var square = myDoc.pathItems.rectangle(-100,100,100,100);
var circlemask = myDoc.pathItems.ellipse(-120,120,60,60);
var finalGroup = myDoc.groupItems.add();
    square.moveToBeginning(finalGroup);
    circlemask.moveToBeginning(finalGroup);
finalGroup.clipped = true;
finalGroup.position = [0,0];

 

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

Yes, because the position of the finalGroup is the same as the position of the square (this is the case before and after you move it).  The outer part of the square still exists, it's just hidden and not directly selectable from the artboard, but if you switch to Outline mode (Ctrl+Y) or select it from the layers panel, you'll see it still exists. 

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

So there's no way easy way to set the position using the circle in a script?

 

I can use math I guess, but I was hoping for an easier way.

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 ,
Jul 26, 2020 Jul 26, 2020

Copy link to clipboard

Copied

Yes, math it is, but it shouldn't be that hard.  All you need is to calculate the distances between the circle and the finalGroup and subtract these distances from the final position you want. 

 

var myDoc = app.activeDocument;
var square = myDoc.pathItems.rectangle(-100,100,100,100);
var circlemask = myDoc.pathItems.ellipse(-120,120,60,60);
var finalGroup = myDoc.groupItems.add();
    square.moveToBeginning(finalGroup);
    circlemask.moveToBeginning(finalGroup);
finalGroup.clipped = true;
var xInterval = circlemask.position[0] - finalGroup.position[0];
var yInterval = circlemask.position[1] - finalGroup.position[1];
finalGroup.position = [0 - xInterval, 0 - yInterval];

 

Caveats:  (1) "position" does not include stroke weight, so you will have to calculate for that, and (2) the x and y elements cannot be assigned individually, i.e. "position" must be assigned an array [x, y]. 

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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Is there an easy way to expand all strokes inside a grouped object?

 

Also if jsx feature requests are a thing, .clipposition seems like it should be a thing.

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 ,
Jul 31, 2020 Jul 31, 2020

Copy link to clipboard

Copied

LATEST

I haven't used either myself, but to expand a path (brings up dialog box):

 

executeMenuCommand("Expand3")

 

or to outline a stroke:

 

executeMenuCommand("OffsetPath v22")

 

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