Skip to main content
Known Participant
December 4, 2022
Question

Rotation and Translation depending on text inside item

  • December 4, 2022
  • 1 reply
  • 3056 views

Hello everyone.

If the tile of this post isn't clear i'm sorry, let me explain what i'm trying to achieve.

In my document there's 1 layer containing 4 items: text, group1, group2, group3.

My script translates and rotates group1, group2 and group3. (see code below)

I need to make the rotation and translation values depend on the word inside item text. (let's say we have 3 sets of values)

 

Is there a way ti achieve that? Thanks in advance

 

 

 

 

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["LAYERNAME"]; //this defines the layer that i want to get the selection from  

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.  
for(var a=0;a<docRef.groupItems.length;a++){
     if (docRef.groupItems[a].name == "group1"){
     docRef.groupItems[a].selected = true;  
    }
}
docRef.selection[0].translate(678.1357,150.5897);

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.  
for(var a=0;a<docRef.groupItems.length;a++){
     if (docRef.groupItems[a].name == "group2"){
     docRef.groupItems[a].selected = true;  
    }
}
docRef.selection[0].rotate(-180);
docRef.selection[0].translate(-1826.6742,+29.6273);

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.  
for(var a=0;a<docRef.groupItems.length;a++){
     if (docRef.groupItems[a].name == "group3"){
     docRef.groupItems[a].selected = true;  

 

This topic has been closed for replies.

1 reply

femkeblanco
Legend
December 4, 2022

"I need to make the rotation and translation values depend on the word inside item text."

 

Can you give examples of what the words might be and how, depending on the words, you want to rotate/translate? 

DelrestoAuthor
Known Participant
December 4, 2022

Hi Femkeblanco and thanks for replying.

The words would be numbers or words, like these: 164, 152, 128, 116, S, M, L, XL, XXL.

So far i got into the script only one instance of rotate/translate, the one associated to the 116 scenario but basically what will change accordingly to text1 will be just the angle degree/distance values inside this parts of the script:

docRef.selection[0].rotate(-180);
docRef.selection[0].translate(-1826.6742,+29.6273);

 

I may have to specify: i will have x amount of PDF documents in which the items will have the same names but different contents so in one document the text1 item will contain "116", in another it will contain M, in another.. and so on

I want to batch-execute the script with all the combination word/group-modification inside of it so i dont' have to create one script for each documents and manually choose the appropriate one everytime.

 

I hope i explained myself but please ask if you need more info.

 

Thanks!

femkeblanco
Legend
December 4, 2022

You could use conditional statements (e.g. switch), or you could put your values in an object, as below.  This snippet targets the group "group1" in the layer "LAYERNAME", and manipulates it depending on the text in the same layer.  E.g. if the text is "164", it will rotate 45 degrees and translate 100 and  -100 points.

 

var values = {
    "164": {angle: 45,  deltaX: 100, deltaY: -100},
    "152": {angle: 90,  deltaX: 200, deltaY: -200},
    "128": {angle: 135, deltaX: 300, deltaY: -300}
};

var myLayer = app.activeDocument.layers["LAYERNAME"];
var text = myLayer.textFrames[0].contents;
myLayer.groupItems["group1"].rotate(values[text].angle);
myLayer.groupItems["group1"].translate(values[text].deltaX, values[text].deltaY);