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

run script only on pages specified in array

Explorer ,
Jun 04, 2023 Jun 04, 2023

Copy link to clipboard

Copied

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

Views

544
Translate

Report

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

...

Votes

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.png

 

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

 

Screen Shot 13.pngScreen Shot 14.png

 

 

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
...

Votes

Translate
Community Expert ,
Jun 04, 2023 Jun 04, 2023

Copy link to clipboard

Copied

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;
    }
}

 

Votes

Translate

Report

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

Copy link to clipboard

Copied

Thank you rob day.

It's quite perfect.

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

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

 

Votes

Translate

Report

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

Copy link to clipboard

Copied

Perfect!

Thank you!

Votes

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Report

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

Copy link to clipboard

Copied

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;

 

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.png

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.pngScreen Shot 6.png

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.png

 

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

 

Screen Shot 13.pngScreen Shot 14.png

 

 

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;
    }
}

 

 

Votes

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thank you rob day!

Votes

Translate

Report

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