Skip to main content
Inspiring
November 10, 2021
Answered

Need help unable to progress

  • November 10, 2021
  • 1 reply
  • 308 views
_SaftyMargin();

function _SaftyMargin(){
    //_reportMatch.push("Check_safty_margin");
    var myDoc = app.activeDocument;
    var myPages = myDoc.pages;
    var marginValue = 12.7;
    convertToMM();
    for(var p=0;p<myPages.length;p++){
        var BoundsTop = myPages[p].bounds[0].toFixed(2);
        
        var BoundsLeft = myPages[p].bounds[1].toFixed(2);
        
        var BoundsBottom = myPages[p].bounds[2].toFixed(2);
        
        var BoundsRight = myPages[p].bounds[3].toFixed(2);
        
        var myPagesItem = myPages[p].allPageItems;
        for(var s=0;s<myPagesItem.length;s++){
            if (myPagesItem[s].itemLayer.visible == true && myPagesItem[s].visible == true) {
                if(myPagesItem[s].constructor.name != "PDF"
                && myPagesItem[s].constructor.name != "Image" && myPagesItem[s].constructor.name != "EPS"){
                    var geo = myPagesItem[s].geometricBounds;

                    var objTop = geo[0].toFixed(2);
                    
                    
                    var objLeft = geo[1].toFixed(2);
                    
                    
                    var objBottom = geo[2].toFixed(2);
                    
                                        
                    var objRight = geo[3].toFixed(2); //0,0,215.9,274.4

                    alert(objTop + ">" + BoundsTop + "&&" + objLeft + ">" + BoundsLeft + "&&" + objBottom + "<" + BoundsBottom  + "&&" + objRight + "<" + BoundsRight);
                    
                                        
                    if(objTop > BoundsTop && objLeft > BoundsLeft && objBottom < BoundsBottom && objRight < BoundsRight){
                        
                        if(objTop < BoundsTop+marginValue || objLeft < BoundsLeft+marginValue || objBottom > BoundsBottom-marginValue || objRight > BoundsRight-marginValue){
                            alert(myPages[p].name);
                        }   
                    }
                    else if(objTop == BoundsTop || objLeft == BoundsLeft || objBottom == BoundsBottom || objRight == BoundsRight){
                        alert(myPages[p].name);
                    }                    
                }
            }
        }     
    }
}
function convertToMM(){
    app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
    app.activeDocument.zeroPoint = [0,0];
}

 

For this screenshot case the above script is not working I even checked with alert that all case in the IF loop is getting true.

 

To my knowledge the above script should work but it is not working. Kindly help me to progress further.

 

Thanks

 

 

 

This topic has been closed for replies.
Correct answer SychevKA

hi after .toFixed (2) the variable becomes a string, use + to get number

 

var BoundsTop = +myPages[p].bounds[0].toFixed(2);

 

1 reply

SychevKA
SychevKACorrect answer
Inspiring
November 10, 2021

hi after .toFixed (2) the variable becomes a string, use + to get number

 

var BoundsTop = +myPages[p].bounds[0].toFixed(2);

 

@G31_2Author
Inspiring
November 10, 2021

Thanks and this is the final version:

_SaftyMargin();
alert("Processed");
//_SaftyMargin_Array.push
function _SaftyMargin(){
    //_reportMatch.push("Check_safty_margin");
    var myDoc = app.activeDocument;
    var myPages = myDoc.pages;
    var marginValue = 3;
    convertToMM();
    
    for(var p=0;p<myPages.length;p++){
        var BoundsTop = Number(myPages[p].bounds[0]).toFixed(2);
        var BoundsLeft = Number(myPages[p].bounds[1]).toFixed(2);
        var BoundsBottom = Number(myPages[p].bounds[2]).toFixed(2);
        var BoundsRight = Number(myPages[p].bounds[3]).toFixed(2);
        var myPagesItem = myPages[p].allPageItems;
        for(var s=0;s<myPagesItem.length;s++){
            if (myPagesItem[s].itemLayer.visible == true && myPagesItem[s].visible == true) {
                if(myPagesItem[s].constructor.name != "PDF"
                && myPagesItem[s].constructor.name != "Image" && myPagesItem[s].constructor.name != "EPS" ){
                    var geo = myPagesItem[s].geometricBounds;
                    var objTop = Number(geo[0]).toFixed(2);
                    var objLeft = Number(geo[1]).toFixed(2);
                    var objBottom = Number(geo[2]).toFixed(2);
                    var objRight = Number(geo[3]).toFixed(2);
                    
                    

                    if(objTop >= BoundsTop || objLeft >= BoundsLeft || objBottom <= BoundsBottom || objRight <= BoundsRight){
                        if(objTop < Number(BoundsTop)+Number(marginValue) || objLeft < Number(BoundsLeft)+Number(marginValue) ||
                        objBottom > Number(BoundsBottom)-Number(marginValue) || objRight > Number(BoundsRight)-Number(marginValue)){
                            myPagesItem[s].select();
                            alert(myPages[p].name);
                        }   
                    }
                }
            }
        }     
    }
}


function convertToMM(){
    app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
    app.activeDocument.zeroPoint = [0,0];
}