forked from GitBruno/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Frame_2_PageBleed.jsx
executable file
·111 lines (98 loc) · 3.71 KB
/
Frame_2_PageBleed.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Frame_2_Bleed.jsx
// An InDesign CS5+ JavaScript by Bruno Herfst 2012
// Version 1.1
// WISHLIST
// + script to work with polygons
// + Script need to take multiple page spreads into account
#target indesign
main();
var myDoc;
function main(){
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if(app.documents.length != 0){
myDoc = app.activeDocument;
if(app.selection.length != 0){
//Get the first item in the selection.
for(var i=0;i<app.selection.length;i++){
var mySelection = app.selection[i];
switch(mySelection.constructor.name){
case "Rectangle":
//straighten(mySelection);
break;
default:
var ws = mySelection.constructor.name;
alert("This is a "+ws+" \rPlease select a rectangle and try again.");
exit();
}
}
straightenFrames();
fit();
}else{
alert("Please select a frame.");
}
}else{
alert("Please open a document and try again.");
}
}
function straightenFrames(){
try {
for(var i=0;i<app.selection.length;i++){
var myRect = app.selection[i];
try{
var myImg = myRect.images[0];
} catch(_) {
continue;
}
//Find out what the rotationangle is
var rectRot = myRect.rotationAngle,
imgRot = myImg.rotationAngle,
imgBounds = myImg.geometricBounds;
if(rectRot == 0){
continue;
}
//Create the transformation matrix
var rectTransformationMatrix = app.transformationMatrices.add({counterclockwiseRotationAngle:-rectRot});
imgTransformationMatrix = app.transformationMatrices.add({counterclockwiseRotationAngle:rectRot+imgRot});
// Rotate around its center point
myRect.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.centerAnchor, rectTransformationMatrix);
myImg.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.centerAnchor, imgTransformationMatrix);
if(imgRot >= 180) {
var newAngle = myImg.rotationAngle -= 180;
}
myImg.geometricBounds = imgBounds;
}
}catch(e){} //no image
}
function fit(){
var oldRuler = myDoc.viewPreferences.rulerOrigin;
myDoc.viewPreferences.rulerOrigin = RulerOrigin.spreadOrigin;
try {
for(var i=0;i<app.selection.length;i++){
var myRect = app.selection[i];
var myPage = myRect.parentPage;
//check bounds
var rectBounds = myRect.geometricBounds,
pageBounds = myPage.bounds, //in the format [y1, x1, y2, x2], top-left and bottom-right
pageWidth = myPage.bounds[3]-myPage.bounds[1];
//check bleed (can be made more specific, good for now)
var bleed = myDoc.documentPreferences.documentBleedTopOffset;
//check if image meant to be a spread
if(rectBounds[1] < pageWidth*0.25 && rectBounds[3] > pageWidth*1.75){
//spread
var bleedBound = new Array(-bleed,-bleed,pageBounds[2]+bleed,pageWidth*2+bleed);
} else {
//page
if(myPage.side == PageSideOptions.RIGHT_HAND){
var bleedBound = new Array(pageBounds[0]-bleed,pageBounds[1],pageBounds[2]+bleed,pageBounds[3]+bleed);
} else if(myPage.side == PageSideOptions.LEFT_HAND){
var bleedBound = new Array(pageBounds[0]-bleed,pageBounds[1]-bleed,pageBounds[2]+bleed,pageBounds[3]);
} else { // PageSideOptions.SINGLE_SIDED
var bleedBound = new Array(pageBounds[0]-bleed,pageBounds[1]-bleed,pageBounds[2]+bleed,pageBounds[3]+bleed);
}
}
myRect.geometricBounds = bleedBound;
}
}catch(e){ alert(e.description)}
myDoc.viewPreferences.rulerOrigin = oldRuler;
}