-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbrowserwindow.jsx
165 lines (137 loc) · 7.9 KB
/
browserwindow.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright © 2013 Dominik Levitsky
http://dominik-levitsky.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#target photoshop
var browser = { path: "browserwindow.psd" };
//Getting the folder path of the script.
var scriptFilePath= $.fileName.substring(0, $.fileName.lastIndexOf("/")) + "/";
// Function to get the element height based on element.bounds.
function height(element) { return element.bounds[3] - element.bounds[1]; }
// Function to get the element width based on element.bounds.
function width(element) { return element.bounds[2] - element.bounds[0]; }
// A function for laer style rasterization.
function raterizeLayerStyle(){
// Some magic happens here.
var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref4 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref4.putEnumerated( idLyr, idOrdn, idTrgt );
desc5.putReference( idnull, ref4 );
var idWhat = charIDToTypeID( "What" );
var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );
var idlayerStyle = stringIDToTypeID( "layerStyle" );
desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );
executeAction( idrasterizeLayer, desc5, DialogModes.NO );
// Magic ends here.
}
// A function for resizing the layer in pixels.
function resizeLayer(Width , Height){
// Some magic happens here.
var startRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var LB = activeDocument.activeLayer.bounds;
var lWidth = 100/(LB[2].value - LB[0].value);
var lHeight =100/(LB[3].value - LB[1].value);
var NewWidth = lWidth * Width;
var NewHeight = lHeight * Height;
activeDocument.activeLayer.resize(Number(NewWidth),Number(NewHeight),AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
// Magic ends here.
}
function browserwindow(){
// Defining the application documents.
preferences.rulerUnits = Units.PIXELS;
// A document must be opened in Photoshop.
var sourceDoc = app.activeDocument;
var targetDoc = app.documents.add(sourceDoc.width, sourceDoc.height, 72, "browserwindow", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
var browserDoc = app.open(File(scriptFilePath + browser.path));
// Merging all the layers in the source document.
app.activeDocument = sourceDoc;
app.activeDocument.artLayers.add();
app.activeDocument.mergeVisibleLayers();
// Selecting and copying the merged layer to the newly created document.
var sourceImage = app.activeDocument.layers[0];
sourceImage.name = "sourceImage";
sourceImage = sourceImage.duplicate(targetDoc);
// Merging and renaming all the layers in the target document.
// This is used just for beauty, and can be removed.
app.activeDocument = targetDoc;
app.activeDocument.mergeVisibleLayers();
sourceImage = app.activeDocument.layers[0];
sourceImage.name = "sourceImage";
// Copying the layer group from the browser document to the new target document.
app.activeDocument = browserDoc;
var browserwindow = app.activeDocument.layerSets.getByName("browserwindow");
app.activeDocument.activeLayer = browserwindow;
var browserwindow = browserwindow.duplicate(targetDoc);
// Selecting and defining all the copied layers in the target document.
app.activeDocument = targetDoc;
browserwindow = app.activeDocument.layerSets.getByName("browserwindow");
var browserTopLeft = browserwindow.layers.getByName("topLeft");
var browserTopCenter = browserwindow.layers.getByName("topCenter");
var browserTopRight = browserwindow.layers.getByName("topRight");
var browserBottomLeft = browserwindow.layers.getByName("bottomLeft");
var browserBottomCenter = browserwindow.layers.getByName("bottomCenter");
var browserBottomRight = browserwindow.layers.getByName("bottomRight");
var browserLeftSide = browserwindow.layers.getByName("leftSide");
var browserRightSide = browserwindow.layers.getByName("rightSide");
// Unfortunately, rasterizing all the layers in browserwindow group.
// This is used, because vector layers could not be properly positioned.
// I hope to find some workaround in the near future. I really hope.
for (var i = 0; i < browserwindow.layers.length; i++) {
targetDoc.activeLayer = browserwindow.layers[i];
raterizeLayerStyle();
}
// Changing the document canvas size according to the layer sizes.
targetDoc.resizeCanvas(targetDoc.width, targetDoc.height + height(browserTopLeft), AnchorPosition.BOTTOMCENTER);
targetDoc.resizeCanvas(targetDoc.width, targetDoc.height + height(browserBottomLeft), AnchorPosition.TOPCENTER);
targetDoc.resizeCanvas(targetDoc.width + width(browserRightSide), targetDoc.height, AnchorPosition.MIDDLELEFT);
targetDoc.resizeCanvas(targetDoc.width + width(browserLeftSide), targetDoc.height, AnchorPosition.MIDDLERIGHT);
// Scaling the elements.
// Top and bottom middle sections.
targetDoc.activeLayer = browserTopCenter;
resizeLayer(targetDoc.width - width(browserTopLeft) - width(browserTopRight), height(browserTopCenter));
targetDoc.activeLayer = browserBottomCenter;
resizeLayer(targetDoc.width - width(browserBottomLeft) - width(browserBottomRight), height(browserBottomCenter));
// Left and right sides.
targetDoc.activeLayer = browserLeftSide;
resizeLayer(width(browserLeftSide), targetDoc.height - height(browserTopLeft) - height(browserBottomLeft));
targetDoc.activeLayer = browserRightSide;
resizeLayer(width(browserRightSide), targetDoc.height - height(browserTopRight) - height(browserBottomRight));
// Moving all the layers to the right positions.
// Top Sections.
browserTopLeft.translate(- browserTopLeft.bounds[0], - browserTopLeft.bounds[1]);
browserTopCenter.translate(- browserTopCenter.bounds[0] + width(browserTopLeft), - browserTopCenter.bounds[1]);
browserTopRight.translate( targetDoc.width - width(browserTopRight) - browserTopRight.bounds[0], - browserTopRight.bounds[1]);
// Bottom Sections.
browserBottomLeft.translate(- browserBottomLeft.bounds[0], targetDoc.height - browserBottomLeft.bounds[1] - height(browserBottomLeft));
browserBottomCenter.translate(- browserBottomCenter.bounds[0] + width(browserBottomLeft), targetDoc.height - browserBottomCenter.bounds[1] - height(browserBottomCenter));
browserBottomRight.translate( targetDoc.width - width(browserBottomRight) - browserBottomRight.bounds[0], targetDoc.height - browserBottomRight.bounds[1] - height(browserBottomRight));
//Side Sections.
browserLeftSide.translate(- browserLeftSide.bounds[0], - browserLeftSide.bounds[1] + height(browserTopLeft));
browserRightSide.translate(targetDoc.width - width(browserRightSide) - browserRightSide.bounds[0], - browserRightSide.bounds[1] + height(browserTopRight));
targetDoc.resizeCanvas(targetDoc.width + 50, targetDoc.height + 50);
// Closing the opened browserwindow file.
browserDoc.close();
// Setting the newly created document as the active one.
app.activeDocument.activeLayer = browserwindow;
}
// Bring Photoshop to front.
app.bringToFront();
// Run the script. Finally. Yay!
browserwindow();