forked from GitBruno/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Frame_2_SpreadBleed.jsx
executable file
·108 lines (90 loc) · 3.29 KB
/
Frame_2_SpreadBleed.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
// 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;
var mySpread = myPage.parent;
var firstPage = mySpread.pages[0];
var lastPage = mySpread.pages[mySpread.pages.length-1];
//check bounds
var rectBounds = myRect.geometricBounds;
var firstPageBounds = firstPage.bounds; //in the format [y1, x1, y2, x2], top-left and bottom-right
var lastPageBounds = lastPage.bounds;
var pageBounds = [firstPageBounds[0],firstPageBounds[1],lastPageBounds[2],lastPageBounds[3]];
var pageWidth = pageBounds[3]-pageBounds[1];
//check bleed (can be made more specific, good for now)
var bleed = myDoc.documentPreferences.documentBleedTopOffset;
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;
}