How to loop through Array Variable?

I'd like to loop through all entries in an Array Variable updating "Coordinates" each time through. How do I do that?


I can manually change the [Array Index] in the RegEx, but don't know how to auto-update the index. I've looked at a few examples, but don't understand them yet.

Here is my "progress".
image
I've been able to "add" to the Array Index manually. How can I encorporate the "Local_i" variable into this equation? Nothing I'm doing so far has worked.

You can use the Substrings In collection to iterate through all the entries in the variable:

Alternatively, you can use Local_i as you have, and

%Variable%positionArray[Local_i]%

Note that you appear to be using a variable called “Position” in one action, and then expecting it to be in a variable “Coordinates” in the next, which may be part of your confusion.

1 Like

Peter, it would be great if we could also set a value in the pseudo array using the index, as in:

image

Getting the value is easy, but setting it is more difficult, requiring a complex RegEx.

3 Likes

This worked perfectly, Peter. Thank you!

To clarify a bit, the "Coordinates" variable is a separate "Tabbed Text File" that is captured from the current "Template" file and parsed with RegEx (thanks, JMichaelTX) to get the current "Position" variables. I know there is MUCH potential for improvement, but I thrilled it's actually working now!

Right now I have duplicated this Javascript to position elements on three separate Illustrator layers. I know it will be more efficient to combine it into one JSX… one step at a time.

// Position Align Object
var idoc = app.activeDocument;  
var myLayer = idoc.layers['FINGERPRINT'];  
var mySubLayer = myLayer.layers['FINGERPRINTAlign'];  
myLayer.visible = true; 
mySubLayer.visible = true; 

//var AlignObj = ilayer.pathItems[0];
var AlignObj = mySubLayer.pathItems.getByName('%Position%');


//var sel=idoc.selection[0];  
var AlignObjBounds = AlignObj.visibleBounds;  
var topX = AlignObjBounds[0];  
var topY = AlignObjBounds[1];
var botX = AlignObjBounds[2];  
var botY = AlignObjBounds[3];
var mm =  2.83464567;
var deltaX = (botX-topX)/2*-1;
var deltaY = (botY-topY)/2*-1;

//Credit: Eric Moore - Calculate new center position for selection adjusting for current AI ruler setting.
AlignObj.position = [(%xRule%*mm+deltaX+(%FP_X%*mm)),(%yRule%*mm+deltaY-(%FP_Y%*mm))];

I have been thinking more about this and realize I may be complicating things.

In the solution you provided I was using a separate "positionArray" variable that is nothing more than the first tabbed column of variable "Coordinates". Is it possible to use a "For Each" loop that simply captures the string before the first tab of "Coordinates" and iterate through each line in "Coordinates"? I've added an Action that sets a sample "Coordinates" variable. I tried to set "Position" to the first string (R1_C1), but my RegEx does not work. What is wrong and is this approach feasible or is it better to work with a separate variable to parse "Coordinates"?

Position Align Objects.kmmacros (20 KB)

I am afraid I am not following you - what exactly do you want to do with all the different entries in the Coordinates variable?

Given Coordinates like shown, what are the sequence of actions you want to perform?

This animated gif illustrates the purpose of the macro. The objects being moved are "Alignment Objects" positioned based on the embedded "Coordinate Chart" that is saved by KM into "Coordinate" Variable. There are about 50 similar templates to maintain. The current process is to position each piece manually by keying in the coordinates. That stinks. The GIF is an exaggeration of the normal process of moving a few elements less than a mm.

The solution you showed me works fine. The only "downside" is I must capture a separate "positionArray" Variable. Since the first string in each line of "Coordinates" is what I'm keying on to set the "Position" xy coordinates I thought maybe there was a more direct way to take advantage of the "For loop".

I can share a simplified version of the Adobe Illustrator template if that would help, although I don't expect you use the program.

I'm still confused.

You can use the Search Variable action to map from the Coordinates variable to a specific set of R2_C2 (whatever) and fields in the line. You don't need a For Each action for that, just the Search action.

What are you iterating through?

The "Coordinates" variable has from 6 to 12 lines. I need to capture the x/y coordinates on each line (R1_C1, etc.) and move the corresponding named elements to their assigned location. I am currently using a separate variable, "positionArray" to iterate each line of the "Coordinates" variable and move every element referenced in the "Coordinates" chart.

Thanks for helping me with this. I know I'm the one who is confused.

OK, so it sounds like you want to use a For Each action with the positionArray variable and a Lines In collection to iterate through the Coordinates variable. Then use a Search using Regular Expression action to search the positionArray variable and break it in to its component parts. And then move the appropriate element using those details.

1 Like

Thanks, Peter. I'll work on it.

@rcraighead, I'm confused about your workflow.

It is not apparent to me that you even need to use KM.
What is the exact purpose/function you want to achieve with KM?

I appears to me that most of the work is already done in JavaScript, JXS, and/or JXA scripts, so I wonder why not do all of the work there?

If you don't want/need to actually change the coordinates for each Alignment Object using KM, then what is its purpose in your workflow?

What is the purpose of writing the JXS scripts to file using KM?
I don't see any KM variables used in doing that.

Maybe it would help us better understand your workflow, and therefore be able to better help you, if you could list the specific steps in your workflow, clearly indicating where the source data is coming from, and how you want to change it.

Thanks for your response. I'll put together a more complete explanation SOON. :):slightly_smiling_face:

Another one for you mate. Done for the next version.

10 Likes