23 Replies Latest reply on Feb 10, 2016 3:56 AM by Ronald63

# Script who moves an already placed frame to a new position (x/y)

Hi,

I need help with a simple scriptcode who can move an already placed frame in x=6,99mm /  y=4,801 to a new posistion x=10mm / y=10mm

Merry X-mas!

• ###### 1. Re: Script who moves an already placed frame to a new position (x/y)

Hi,

```var doc = app.activeDocument;
var mItems=doc.allPageItems;
var len = mItems.length;

while(len--){
var mItem = mItems[len];
var mBounds = mItem.geometricBounds;
var mX = getRound(mBounds[1],3);
var mY= getRound(mBounds[0],3);
if(mX == 6.99 && mY == 4.801){
mItem.move([10, 10]);
}
}
/* round */
function getRound(number, digits) {
digits = (digits) ? Math.pow(10, digits) : 1000;
return Math.round(number * digits) / digits;
}
```

Regards

• ###### 2. Re: Script who moves an already placed frame to a new position (x/y)

Thank you so much - you have saved me for at lot of manual work

Ronald63 skrev:

Hi,

1. var doc = app.activeDocument;
2. var mItems=doc.allPageItems;
3. var len = mItems.length;
4.
5. while(len--){
6.     var mItem = mItems[len];
7.     var mBounds = mItem.geometricBounds;
8.     var mX = getRound(mBounds[1],3);
9.     var mY= getRound(mBounds[0],3);
10.     if(mX == 6.99 && mY == 4.801){
11.         mItem.move([10, 10]);
12.     }
13. /* round */
14. function getRound(number, digits) {
15.         digits = (digits) ? Math.pow(10, digits) : 1000;
16.         return Math.round(number * digits) / digits;

Regards

• ###### 3. Re: Script who moves an already placed frame to a new position (x/y)

That's the magic of Christmas

• ###### 4. Re: Script who moves an already placed frame to a new position (x/y)

If I want to call the export menu - or even better - a specific PDF preset afterwards - can you then help me again?

• ###### 5. Re: Script who moves an already placed frame to a new position (x/y)

If I want to call the export menu - or even better - a specific PDF preset afterwards - can you then help me again?

• ###### 6. Re: Script who moves an already placed frame to a new position (x/y)

Like this ?

```var doc = app.activeDocument;

var _PDFfile = new File('~/Desktop/test.pdf');

var _PDFExportPreset = app.pdfExportPresets.item('MyJobOptionName');
if (_PDFExportPreset == null){
exit();
}
app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;
doc.exportFile(ExportFormat.pdfType, _PDFfile, false,_PDFExportPreset);
```
• ###### 7. Re: Script who moves an already placed frame to a new position (x/y)

Yes - lovely- oh my god you rock at scipting.

One last thing and I will rest my case.

If I want want to change frame size from 266x365mm to 240x340 mm AND fit content to frame - can you also help me out there?

• ###### 8. Re: Script who moves an already placed frame to a new position (x/y)

Yes - lovely- oh my god you rock at scipting.

One last thing and I will rest my case.

If I want want to change frame size from 266x365mm to 240x340 mm AND fit content to frame - can you also help me out there?

• ###### 9. Re: Script who moves an already placed frame to a new position (x/y)

Have you left me?

• ###### 10. Re: Script who moves an already placed frame to a new position (x/y)

No I'm very busy now ;-)

You want resize the frame after moving and fit the content, that's correct ?

It's a text frame or rectangle ?

• ###### 11. Re: Script who moves an already placed frame to a new position (x/y)

Fair enough - sorry

First of all - it is a rectangle

Second what I would like is:

1. Resize the rectangle from 266x365mm to 240x340mm

2. Fit content to frame (rectangle)

3. Move it from Y=4,801mm / X=6,999mm to Y=10mm / X=10mm (this part you have already helped me with )

4. Export the PDF with a specific PDF Preset (this part you have already helped me with )

• ###### 12. Re: Script who moves an already placed frame to a new position (x/y)

Hi,

```var doc = app.activeDocument;
var mItems=doc.allPageItems;
var len = mItems.length;

// save measurement unit & set measurement unit to mm
savedUnits = app.scriptPreferences.measurementUnit;
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

while(len--){
var mItem = mItems[len];
var mBounds = mItem.geometricBounds;
var mX = getRound(mBounds[1],3);
var mY= getRound(mBounds[0],3);

if(mX == 6.999 && mY == 4.801){
resizeItem(mItem, 240,340)
mItem.move([10, 10]);
mItem.fit(FitOptions.FILL_PROPORTIONALLY);
}
}
// restore measurement unit
app.scriptPreferences.measurementUnit = savedUnits;
// export PDF
var _PDFExportPreset = app.pdfExportPresets.item('MyJobOptionName');
if (_PDFExportPreset == null){
exit();
}
app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;
doc.exportFile(ExportFormat.pdfType, _PDFfile, false,_PDFExportPreset);

/* round */
function getRound(number, digits) {
digits = (digits) ? Math.pow(10, digits) : 1000;
return Math.round(number * digits) / digits;
}
/* resize object */
function resizeItem(mItem, mWidth,mHeight) {
mBounds = mItem.geometricBounds;
mBounds[3] = mBounds[1] + mWidth;
mBounds[2] =mBounds[0] + mHeight;
mItem.geometricBounds = mBounds;
}
```
• ###### 13. Re: Script who moves an already placed frame to a new position (x/y)

Hi - sorry for my late feedback.

When I run the script in InDesign CS3 I get this error

• ###### 14. Re: Script who moves an already placed frame to a new position (x/y)

Hi,

This propertie don't exist in CS3 ...

CS3 JS: ScriptPreference

Comment line 6-7-22 and retry ...

• ###### 15. Re: Script who moves an already placed frame to a new position (x/y)

What do you mean by this:

"Comment line 6-7-22 and retry ...'

?

• ###### 16. Re: Script who moves an already placed frame to a new position (x/y)

```// savedUnits = app.scriptPreferences.measurementUnit;
```

• ###### 17. Re: Script who moves an already placed frame to a new position (x/y)

Ahh of course - stupid me - now it works great, thank you!

What if:

1. I run the script and everything works fine

2. now suddenly the placed object is back to 100% / 100%

3. When I run the script again nothing happens

When this situation appears I would want it to still fit content to frame.

Is it possible?

• ###### 18. Re: Script who moves an already placed frame to a new position (x/y)

Hi,

try this (add code from line 18 to 20)

```

var doc = app.activeDocument;
var mItems=doc.allPageItems;
var len = mItems.length;

// save measurement unit & set measurement unit to mm
savedUnits = app.scriptPreferences.measurementUnit;
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

while(len--){
var mItem = mItems[len];
var mBounds = mItem.geometricBounds;
var mX = getRound(mBounds[1],3);
var mY= getRound(mBounds[0],3);

// fit content
if(mX == 10 && mY == 10){
mItem.fit(FitOptions.FILL_PROPORTIONALLY);
}

if(mX == 6.999 && mY == 4.801){
resizeItem(mItem, 240,340)
mItem.move([10, 10]);
mItem.fit(FitOptions.FILL_PROPORTIONALLY);
}
}
// restore measurement unit
app.scriptPreferences.measurementUnit = savedUnits;
// export PDF
var _PDFExportPreset = app.pdfExportPresets.item('MyJobOptionName');
if (_PDFExportPreset == null){
exit();
}
app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;
doc.exportFile(ExportFormat.pdfType, _PDFfile, false,_PDFExportPreset);

/* round */
function getRound(number, digits) {
digits = (digits) ? Math.pow(10, digits) : 1000;
return Math.round(number * digits) / digits;
}
/* resize object */
function resizeItem(mItem, mWidth,mHeight) {
mBounds = mItem.geometricBounds;
mBounds[3] = mBounds[1] + mWidth;
mBounds[2] =mBounds[0] + mHeight;
mItem.geometricBounds = mBounds;
}

```
• ###### 19. Re: Script who moves an already placed frame to a new position (x/y)

Lovely - it works perfect!

One correction:

mItem.fit(FitOptions.FILL_PROPORTIONALLY);

I need it to "Fit Content to Frame" and not proportionally

• ###### 20. Re: Script who moves an already placed frame to a new position (x/y)
```mItem.fit(FitOptions.CONTENT_TO_FRAME);
```

InDesign ExtendScript API (10.0)

• ###### 21. Re: Script who moves an already placed frame to a new position (x/y)

Perfect - thank you thank you thank you - I am so gratefull for this!

• ###### 22. Re: Script who moves an already placed frame to a new position (x/y)

Hi Ronald,

I also need the script to check for overset text.

So something like this.

1. If there is a textbox with overset text the scipt shall stop all further actions in the script and warn about it.

2. If there is no textbox with overset text the script shall just run with all actions.

Can you help me out?

• ###### 23. Re: Script who moves an already placed frame to a new position (x/y)

Can you create a new topic please ...