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

How to loop through a selection and apply a script

New Here ,
Jan 17, 2017 Jan 17, 2017

Hi there,

I'm having real trouble trying to get the below script to work on more than one selected object. At the moment it works perfect with one frame selected with an image in it. What it does is it scales it down to 10% and centres it in a frame. I've tried different script samples I've found online but I can't seem to get them to work with the below. Can anyone help show me what I'm failing to do.

Below is the working script (1 selected)

function fit10(){

var sel = app.selection[0]; 

    sel.fit(FitOptions.centerContent); 

var myImage = sel;

myImage.graphics[0].absoluteHorizontalScale = 10;

myImage.graphics[0].absoluteVerticalScale = 10;

    }

____________________

Below is the script that is not working (more than 1 selected)

function fit10(){

var sel = app.selection

    sel.fit(FitOptions.centerContent); 

    for (var i = 0; sel > i; i++)

var myImage = sel;

myImage.graphics[0].absoluteHorizontalScale = 10;

myImage.graphics[0].absoluteVerticalScale = 10;

    }

Thank you

TOPICS
Scripting
1.3K
Translate
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
Mentor ,
Jan 17, 2017 Jan 17, 2017
LATEST

Hi,

Let me induce you to study javascript since it will make your work easier and faster.

Copying and pasting unreadable codes from network can lead you to wrong or even danger places.

  • A selection - if made - is an array of objects.
  • Your function fit10() should be called for each element of this array.
  • So all you need is to create a loop.
  • Checking if:

1. selection is composed from graphic' containers

and

2. these containers are not empty

makes a code more universal.

Exam this:

var

  mTarget = app.selection,

  cObject;

while (cObject = mTarget.pop())

  fit10(cObject);

function fit10(anObject){

  if (!anObject.hasOwnProperty ("graphics") ) return;

  if (!anObject.graphics.length) return;

  anObject.graphics[0].absoluteHorizontalScale = 10;

  anObject.graphics[0].absoluteVerticalScale = 10;

  anObject.fit(FitOptions.centerContent);

  }

Jarek

Translate
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