﻿/*     
author: ferdinand pechmann

This script creates pagesnumbers with a color gradation between frist and last number.
It uses the aktiv document.

*/


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//format of the page number
//change this values to your needs

// fontsize in Pt
var  font_size= 160;              
// font of your fontcollection
var  font = "DIN Next LT Pro"  
var  font_weight ="Bold Condensed"

// distance from the side and bottom to the center the pagenumber
var distance_side    = 50;        
var distance_bottom= 30;   

// CMYK value of the first pagenumber
var C1 = 0;                     
var M1 = 80;
var Y1 = 100;
var K1 = 0;

// CMYK value of the last pagenumber
var C2 = 0;                        
var M2 = 80;
var Y2 = 100;
var K2 = 75;


////////////////////////////////////////////////////////////////////////////////////////////////////////////

//use aktive document and get the nesseary preferences
doc = app.activeDocument;

var facing_pages = doc.documentPreferences.facingPages;
var pages_per_doc = doc.pages.length;
var ph = doc.documentPreferences.pageHeight;
var pw = doc.documentPreferences.pageWidth;


//start CMYK value
var C= C1;
var M = M1;
var Y = Y1;
var K = K1;


//counter for left or right pages
var counter = 0;


// loop that creates the pagenumber for every page of the document with the specific CMYK value and the adjusted position 

for ( var  i = 0 ; i <  pages_per_doc ; i++ ){
 
 // CMYK values 
 var C = C + (( C2 - C1)  / pages_per_doc );
 var M = M + (( M2 - M1)  / pages_per_doc );
 var Y = Y +  (( Y2 - Y1)  / pages_per_doc );
 var K = K + (( K2 - K1)  / pages_per_doc );

// create the specific color
var colorname = "C" + C + " M" + M + " Y" + Y + " K" + K; 
var color_one = doc.colors.add();
    color_one.properties = {
    space:ColorSpace.CMYK,
    colorValue:[C,M,Y,K],
    name: (colorname)
    };


// counter for left or right page
counter = (counter + 1);                            
if ( counter > 3){
    counter = 2;
    };


// textframe position y for pagenumber
var y1= (( ph - distance_bottom) - 50);              
var y2 = (( ph - distance_bottom) + 50);

if ( facing_pages == false){                            
     // textframe position X for pagenumber  not facing pages
     var  x1= (distance_side - 50);
     var  x2 = (distance_side + 50);

}else if (counter == 1){                                   
     // textframe position X for pagenumber first page
     counter = (counter + 1); 
     var  x1= (pw - (distance_side - 50));
     var  x2 = ( pw - (distance_side + 50));
   
}else if (counter == 2){                                    
       // textframe position X for pagenumber right page
     var  x1= (( 2 * pw ) - (distance_side - 50));
     var  x2 = (( 2 * pw ) - (distance_side + 50));
    
 
}else{  
     // textframe position X for pagenumber left page
     var  x1= (distance_side - 50);
     var  x2 = (distance_side + 50);
    };
    

var aktpage = doc.pages[i];                                                

//create pagenumber
var pagenr =      aktpage.textFrames.add({
                       geometricBounds: [y1,x1,y2,x2],
                       contents: ""+(i+1),   
                       textFramePreferences:{verticalJustification: VerticalJustification.CENTER_ALIGN}
                       }); 
 
// get the paragraph
var pagenrparagraph = pagenr.paragraphs.anyItem(); 



// set  pointsize, font, color
pagenrparagraph.properties = {
                        pointSize: font_size,                                    
                        appliedFont: font + "\t" + font_weight,
                        justification : Justification.CENTER_ALIGN,
                        fillColor: doc.swatches.itemByName (colorname)                                            
                        };


};      





  