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

Declaring variables issue

Contributor ,
Aug 20, 2022 Aug 20, 2022

Copy link to clipboard

Copied

I have just started to learn JavaScript, and I have been using VBA before (thanks to the advice and help of @DilliamWowling  and @CarlosCanto , let me enter the world of JS). This forum is a good place to start!!

 

Here is a short piece of code from @Silly-V 

 

var targetObjects = doc.layers.getByName("Target Position").pathItems;
var objectsToMove = [
  doc.layers.getByName("To move aligned objects").layers[0].pathItems[0],

  doc.layers.getByName("To move aligned objects").layers[1].pathItems[0],

  doc.layers.getByName("To move aligned objects").layers[2].pathItems[0]
];

for (var i = 0; i < objectsToMove.length; i++) {

  objectsToMove[i].top = targetObjects[i].top;

  objectsToMove[i].left = targetObjects[i].left;

}

 

 

I want to ask a simple question about the following method of declaring variables:

 

var objectsToMove = [
  doc.layers.getByName("To move aligned objects").layers[0].pathItems[0],

  doc.layers.getByName("To move aligned objects").layers[1].pathItems[0],

  doc.layers.getByName("To move aligned objects").layers[2].pathItems[0]
];

 

I tried to refer to the relevant topics on this forum, but I still didn't find the answer. I'm curious about this declaration method. It seems that this variable is defined with a constant value pathitems [0] ? When it is objectsToMove[i], what value is the objectsToMove ? Is it below?

 

 doc.layers.getByName("To move aligned objects").layers[0].pathItems[i],

 doc.layers.getByName("To move aligned objects").layers[1].pathItems[i],

 doc.layers.getByName("To move aligned objects").layers[2].pathItems[i]

 

I really don't understand why it can be like this?

 

And what value is the objectsToMove.length ?Is it layers [0]. pathitems length? or layers[1].pathItems. length? or layers[2].pathItems. length? Or the sum of them? If the number of pathitems in each layer is different, can it be executed correctly?

 

Many thanks in advance for your time and advice!

TOPICS
Scripting

Views

219

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

Community Expert , Aug 20, 2022 Aug 20, 2022

objectsToMove is an Array, .length represents the number of items in the array, 3 in this example.

 

there are many ways of arriving at the same result, below is the same script explicitly declaring variables at all stages before getting to the items that need to be moved. This is not necessary, but it's easier to read and understand when one is a beginner. Let me know if that clarifies your question.

 

 

 

var targetLayer = doc.layers.getByName("Target Position"); // get target layer
var target
...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 20, 2022 Aug 20, 2022

Copy link to clipboard

Copied

objectsToMove is an Array, .length represents the number of items in the array, 3 in this example.

 

there are many ways of arriving at the same result, below is the same script explicitly declaring variables at all stages before getting to the items that need to be moved. This is not necessary, but it's easier to read and understand when one is a beginner. Let me know if that clarifies your question.

 

 

 

var targetLayer = doc.layers.getByName("Target Position"); // get target layer
var targetObjects = targetLayer.pathItems; // get objects in target layer

// get objects to move Layer
var objectsToMoveLayer = doc.layers.getByName("To move aligned objects");

// get objectsToMove sublayers
var objectsToMoveLayerSublayer0 = objectsToMoveLayer.layers[0];
var objectsToMoveLayerSublayer1 = objectsToMoveLayer.layers[1];
var objectsToMoveLayerSublayer2 = objectsToMoveLayer.layers[2];

// get the first item of each sublayer
var objectToMove0 = objectsToMoveLayerSublayer0.pathItems[0];
var objectToMove1 = objectsToMoveLayerSublayer1.pathItems[0];
var objectToMove2 = objectsToMoveLayerSublayer2.pathItems[0];

// create an Array to hold each object to move
var objectsToMove = [objectToMove0, objectToMove1, objectToMove2];

for (var i = 0; i < objectsToMove.length; i++) {

  objectsToMove[i].top = targetObjects[i].top;

  objectsToMove[i].left = targetObjects[i].left;

}

 

 

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

Copy link to clipboard

Copied

LATEST

Thank you for your explanation @CarlosCanto , i got it now🙂

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
LEGEND ,
Aug 20, 2022 Aug 20, 2022

Copy link to clipboard

Copied

Anything in square brackets in JS is an array or an reference to the index of an item in the array. Arrays in JS always start a 0 for the first item. Arrays can also be strings and enumerated lists and JS will handle the conversion automatically, which would be useful for optimizing your code further. You may wan't to read a bit about this stuff on W3Schools or similar web-centric sites. Knowing your way around arrays and their methods can be pretty useful for sorting and managing stuff.

 

Mylenium

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

Copy link to clipboard

Copied

Thank you for your explanation.

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