1+ var doc = app . activeDocument ;
2+
3+ var masterIndex = 0 ;
4+ var masterSpreadLeft = doc . masterSpreads . item ( masterIndex ) . pages . item ( 0 ) ;
5+ var masterSpreadRight = doc . masterSpreads . item ( masterIndex ) . pages . item ( 1 ) ;
6+
7+ var pageHeight = Math . round ( doc . documentPreferences . pageHeight * 10 ) / 10 ;
8+ var pageWidth = Math . round ( doc . documentPreferences . pageWidth * 10 ) / 10 ;
9+ var pageRatio ;
10+ if ( pageHeight >= pageWidth ) {
11+ pageRatio = pageWidth / pageHeight ;
12+ } else if ( pageWidth > pageHeight ) {
13+ pageRatio = pageHeight / pageWidth ;
14+ }
15+
16+ var marginTop , marginBottom , marginLeft , marginRight ;
17+ var areasRatio = 1 - pageRatio ;
18+
19+ var shortSideSubdivisions = 4 ;
20+ var gutter = 5 ;
21+
22+ clearAllGuides ( ) ;
23+ IKissedASquareAndILikedIt ( areasRatio , shortSideSubdivisions , gutter ) ;
24+
25+
26+
27+
28+
29+ function IKissedASquareAndILikedIt ( areasRatio , shortSideSubdivisions , gutter ) {
30+ var pageShortSide , pageLongSide ;
31+ if ( pageHeight >= pageWidth ) {
32+ pageShortSide = pageWidth ;
33+ pageLongSide = pageHeight ;
34+ } else if ( pageHeight < pageWidth ) {
35+ pageShortSide = pageHeight ;
36+ pageLongSide = pageWidth ;
37+ }
38+
39+
40+ // assign the short side margins value
41+ var shortSideMargins = Math . round ( pageShortSide * areasRatio ) ;
42+ if ( pageHeight >= pageWidth ) {
43+ marginLeft = shortSideMargins / 2 ;
44+ marginRight = shortSideMargins / 2 ;
45+ } else if ( pageHeight < pageWidth ) {
46+ marginTop = shortSideMargins / 2 ;
47+ marginBottom = shortSideMargins / 2 ;
48+ }
49+
50+ // define the width = height for the subsivisios to fit in the short side
51+ var subdivisionsLenght = ( pageShortSide - shortSideMargins - gutter * ( shortSideSubdivisions - 1 ) ) / shortSideSubdivisions ;
52+
53+ // calculate a draft for the long side margins lenght
54+ var longSideMarginsDraft = Math . round ( pageLongSide * areasRatio ) ;
55+ var longSideMargins ;
56+ // calculate a draft for the number of subdivisions of the long side, then refine it considering the gutters
57+ var longSideSubdivisionsDraft = ( pageLongSide - longSideMarginsDraft ) / subdivisionsLenght ;
58+ var longSideSubdivisions = Math . round ( ( pageLongSide - longSideMarginsDraft - gutter * ( longSideSubdivisionsDraft - 1 ) ) / subdivisionsLenght ) ;
59+ // check if the inner part of the long side (defined by the margins) is divisible by the subdivisions lenght
60+ // if it's not, recalculate the long side margins accordingly
61+ if ( ( pageLongSide - longSideMarginsDraft ) % subdivisionsLenght !== 0 ) {
62+ longSideMargins = pageLongSide - ( subdivisionsLenght * longSideSubdivisions ) - gutter * ( longSideSubdivisions - 1 ) ;
63+ }
64+
65+ // assign the long side margins value
66+ if ( pageHeight >= pageWidth ) {
67+ marginTop = longSideMargins / 2 ;
68+ marginBottom = longSideMargins / 2 ;
69+ } else if ( pageHeight < pageWidth ) {
70+ marginLeft = longSideMargins / 2 ;
71+ marginRight = longSideMargins / 2 ;
72+ }
73+
74+
75+ if ( pageHeight >= pageWidth ) {
76+ // set a loop to draw the long side guides
77+ for ( var i = 0 ; i < shortSideSubdivisions ; i ++ ) {
78+ var xPosition = marginLeft + i * ( subdivisionsLenght + gutter ) ;
79+
80+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : xPosition } ) ;
81+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : xPosition + subdivisionsLenght } ) ;
82+
83+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : pageWidth + xPosition } ) ;
84+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : pageWidth + xPosition + subdivisionsLenght } ) ;
85+ }
86+
87+ // set a loop to draw the short side guides
88+ for ( var j = 0 ; j < longSideSubdivisions ; j ++ ) {
89+ var yPosition = marginTop + j * ( subdivisionsLenght + gutter ) ;
90+
91+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : yPosition } ) ;
92+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : yPosition + subdivisionsLenght } ) ;
93+
94+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : yPosition } ) ;
95+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : yPosition + subdivisionsLenght } ) ;
96+ }
97+ }
98+
99+ else if ( pageHeight < pageWidth ) {
100+ // set a loop to draw the long side guides
101+ for ( var i = 0 ; i < longSideSubdivisions ; i ++ ) {
102+ var yPosition = marginLeft + i * ( subdivisionsLenght + gutter ) ;
103+
104+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : yPosition } ) ;
105+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : yPosition + subdivisionsLenght } ) ;
106+
107+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : pageWidth + yPosition } ) ;
108+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . vertical , location : pageWidth + yPosition + subdivisionsLenght } ) ;
109+ }
110+
111+ // set a loop to draw the short side guides
112+ for ( var j = 0 ; j < shortSideSubdivisions ; j ++ ) {
113+ var xPosition = marginTop + j * ( subdivisionsLenght + gutter ) ;
114+
115+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : xPosition } ) ;
116+ masterSpreadLeft . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : xPosition + subdivisionsLenght } ) ;
117+
118+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : xPosition } ) ;
119+ masterSpreadRight . guides . add ( undefined , { orientation : HorizontalOrVertical . horizontal , location : xPosition + subdivisionsLenght } ) ;
120+ }
121+ }
122+
123+
124+ masterSpreadLeft . marginPreferences . properties = {
125+ right : marginRight ,
126+ top : marginTop ,
127+ left : marginLeft ,
128+ bottom : marginBottom ,
129+ } ;
130+ masterSpreadRight . marginPreferences . properties = {
131+ right : marginRight ,
132+ top : marginTop ,
133+ left : marginLeft ,
134+ bottom : marginBottom ,
135+ } ;
136+ }
137+
138+
139+
140+
141+
142+ function clearAllGuides ( ) {
143+ var numGuides = doc . guides . length ;
144+ if ( numGuides == 0 ) return ;
145+
146+ for ( var i = numGuides - 1 ; i >= 0 ; i -- )
147+ {
148+ doc . guides [ i ] . remove ( ) ;
149+ }
150+ }
0 commit comments