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

run script only on pages specified in array

Explorer ,
Jun 04, 2023 Jun 04, 2023

How run the above script only in page specified in myArr

var myDoc = app.activeDocument;
var tfs = myDoc.allGraphics;
var myFrames = myDoc.textFrames;
var myArr = [15,16,20,22]

var A = -5
var B = 0
var C = 120
var D = 175

for (var i = 0; i < tfs.length; i++) {
    try { 

	var myPage = tfs[i].parentPage;
  	
tfs[i].parent.geometricBounds = [A, B, C, D];
tfs[i].geometricBounds = [A, B, C, D];
}
    } catch(e){}
}
 
Thanks
TOPICS
How to , Scripting
611
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

correct answers 2 Correct answers

Community Expert , Jun 04, 2023 Jun 04, 2023

You can get the itemLayer of the graphic and check its name or locked status. Change "LayerName" to the name of your layer

 

 

function setBounds(p,b){
    var tfs = p.allGraphics;
    for (var i = 0; i < tfs.length; i++) {
        if (tfs[i].itemLayer.name == "LayerName") {
            tfs[i].parent.geometricBounds = b;
            tfs[i].geometricBounds = b;
        } 
    }
}

 

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Graphic.html

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Layer.html#d1e200146

...
Translate
Community Expert , Jun 08, 2023 Jun 08, 2023

Looks like a rounding difference on the conversion from the PDF’s size in inches to millimeters. If I inspect the PDF Acrobat reports the trim dimension as 6.693" x 4.724"

 

Screen Shot 12.pngexpand image

 

Those inch dimensions don’t convert to exactly 170mm x 120mm:

 

Screen Shot 13.pngexpand imageScreen Shot 14.pngexpand image

 

 

The script could force the scale of the graphic to 100%:

 

var myDoc = app.activeDocument;
//an array of page names
var pa = [1,16,20,22]
//the bounds to use
var ba = [-5,0,115,170]
//set units for bounds—points?
app.scriptPreferences.measurementUnit = M
...
Translate
Community Expert ,
Jun 04, 2023 Jun 04, 2023

Hi @lux65 , Maybe wrap your bounds setting in a function, then loop thru your page array to set the graphic bounds—something like this?

 

 

var myDoc = app.activeDocument;
//an array of page names
var pa = [15,16,20,22]
//the bounds to use
var ba = [-5,0,120,175]
//set units for bounds—points?
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
//the target page
var p;

for (var i = 0; i < pa.length; i++){
    //get the target page by name
    p = myDoc.pages.itemByName(pa[i].toString())
    //if the page exists run the function
    if (p.isValid) {
        setBounds(p,ba)
    } 
};   

app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

/**
* Sets the bounds of all the graphics on the specified page 
* @ param target page object
* @ param the bounds to set 
* @ return void 
*/
function setBounds(p,b){
    var tfs = p.allGraphics;
    for (var i = 0; i < tfs.length; i++) {
        tfs[i].parent.geometricBounds = b;
        tfs[i].geometricBounds = b;
    }
}

 

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
Explorer ,
Jun 04, 2023 Jun 04, 2023

Thank you rob day.

It's quite perfect.

It's possible make it works only on visible or unlocked layers? or specific named layer?

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
Community Expert ,
Jun 04, 2023 Jun 04, 2023

You can get the itemLayer of the graphic and check its name or locked status. Change "LayerName" to the name of your layer

 

 

function setBounds(p,b){
    var tfs = p.allGraphics;
    for (var i = 0; i < tfs.length; i++) {
        if (tfs[i].itemLayer.name == "LayerName") {
            tfs[i].parent.geometricBounds = b;
            tfs[i].geometricBounds = b;
        } 
    }
}

 

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Graphic.html

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Layer.html#d1e200146

 

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
Explorer ,
Jun 04, 2023 Jun 04, 2023

Perfect!

Thank you!

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
Explorer ,
Jun 08, 2023 Jun 08, 2023

Due to a Indesign approximate conversion from points to mm, despite bounds of graphic and container are the same measures, the percentage shown in Indesign it's never 100% but always with many decimals (99,99999 or 100,00001). There isn't a workaroung to get more accurate conversion?

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
Community Expert ,
Jun 08, 2023 Jun 08, 2023

When you are setting bounds you have to set what units should be used. You can either set the document’s ruler unit preference, or the units used by the script, which is what I did in my example. If you want to use millimeters then set the scripting units to millimeters. Change:

 

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

 

 

to:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

 

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
Explorer ,
Jun 08, 2023 Jun 08, 2023

yes, I also try setting

MeasurementUnits.MILLIMETER

Width and height of graphic and container shown in Indesign are perfect, but not the percentage.

Schermata 2023-06-08 alle 16.46.05.pngexpand image

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
Community Expert ,
Jun 08, 2023 Jun 08, 2023

That’s because if the graphic is an image it would have a pixel dimension, and the pixels may or may not fit exactly to your 170mm x 120mm bounds—the image’s pixel grid can’t contain a partial pixel.

 

It’s easier to see the problem via Photoshop’s Image Size dialog. In this example the image pixel dimension is 2008px x 1417px. At 300ppi the 100% output dimensions are close to 170mm x 120mm, but not exact:

 

Screen Shot 5.pngexpand imageScreen Shot 6.pngexpand image

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
Explorer ,
Jun 08, 2023 Jun 08, 2023

I place vector pdf generated from indesign, like the attached.

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
Community Expert ,
Jun 08, 2023 Jun 08, 2023

Looks like a rounding difference on the conversion from the PDF’s size in inches to millimeters. If I inspect the PDF Acrobat reports the trim dimension as 6.693" x 4.724"

 

Screen Shot 12.pngexpand image

 

Those inch dimensions don’t convert to exactly 170mm x 120mm:

 

Screen Shot 13.pngexpand imageScreen Shot 14.pngexpand image

 

 

The script could force the scale of the graphic to 100%:

 

var myDoc = app.activeDocument;
//an array of page names
var pa = [1,16,20,22]
//the bounds to use
var ba = [-5,0,115,170]
//set units for bounds—points?
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
//the target page
var p;

for (var i = 0; i < pa.length; i++){
    //get the target page by name
    p = myDoc.pages.itemByName(pa[i].toString())
    //if the page exists run the function
    if (p.isValid) {
        setBounds(p,ba)
    } 
};   

app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

/**
* Sets the bounds of all the graphics on the specified page 
* @ param target page object
* @ param the bounds to set 
* @ return void 
*/
function setBounds(p,b){
    var tfs = p.allGraphics;
    for (var i = 0; i < tfs.length; i++) {
        tfs[i].parent.geometricBounds = b;
        tfs[i].geometricBounds = b;
        tfs[i].horizontalScale = 100;
        tfs[i].verticalScale = 100;
    }
}

 

 

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
Explorer ,
Jun 08, 2023 Jun 08, 2023
LATEST

Thank you rob day!

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