Skip to main content
Known Participant
December 4, 2022
Question

Rotation and Translation depending on text inside item

  • December 4, 2022
  • 1 reply
  • 3001 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 8, 2022

Thanks @femkeblanco. Basing on your indications i put together this script:

 

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

var values = {
    "116": {modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
            modgroup3: {angle: 0, deltaX: 1481.7264, deltaY: 2701.0337},
            modgroup4: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup5: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup6: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup7: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup8: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup9: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup10: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup11: {angle: 0, deltaX: 0, deltaY: -0},},
    "128": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup2: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup3: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup4: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup5: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup6: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup7: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup8: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup9: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup10: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup11: {angle: 0, deltaX: 0, deltaY: -0},},
    "M": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup2: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup3: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup4: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup5: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup6: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup7: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup8: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup9: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup10: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup11: {angle: 0, deltaX: 0, deltaY: -0},}
};

var myLayer = app.activeDocument.layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;
myLayer.groupItems["DESIGN"].groupItems.getByName("1").rotate(values[text].modgroup1.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("1").translate(values[text].modgroup1.deltaX, values[text].modgroup1.deltaY);
myLayer.groupItems["DESIGN"].groupItems.getByName("2").rotate(values[text].modgroup2.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("2").translate(values[text].modgroup2.deltaX, values[text].modgroup2.deltaY);
myLayer.groupItems["DESIGN"].groupItems.getByName("3").rotate(values[text].modgroup3.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("3").translate(values[text].modgroup3.deltaX, values[text].modgroup3.deltaY);

 

and so far is working!

Maybe you spot some error that i will encounter going ahead?

I have a concern: some file will have 10 groups to be modified, others will have 11. So these lines

myLayer.groupItems["DESIGN"].groupItems.getByName("11").rotate(values[text].modgroup11.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("11").translate(values[text].modgroup11.deltaX, values[text].modgroup11.deltaY);

 are mandatory of course. But what will happen when the file will not have group 11? Will this invalidate the script or it will just be ignored? I tried deleting group 2 and run the script: it actually gave me error.


If a targeted item does not exist, you will get an error message.  If you want to avoid this, you will need to handle the error with a try...catch statement.  To avoid repetition, you can put all this in a function. 

 

var values = {
    "116": {
        modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
        modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
        modgroup3: {angle: 0, deltaX: 1481.7264, deltaY: 2701.0337}
    }/*, ...*/
};

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;
var design = myLayer.groupItems["DESIGN"];

function rotateAndTranslate (name, data) {
    try {
        design.groupItems.getByName(name).rotate(data.angle);
        design.groupItems.getByName(name).translate(data.deltaX, data.deltaY);
    } catch(e) {}
}
rotateAndTranslate("1", values[text].modgroup1);
rotateAndTranslate("2", values[text].modgroup2);
rotateAndTranslate("3", values[text].modgroup3);