-
Notifications
You must be signed in to change notification settings - Fork 2
/
[IG]_Sprite_CSS_Generator.jsx
118 lines (104 loc) · 4.27 KB
/
[IG]_Sprite_CSS_Generator.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
// **************************** Photoshop Script ************************* //
// *********************************************************************** //
//
// ** [IG]_Sprite_CSS_Generator
// ** @description Script is useful for making CSS for sprites.
// Script displays a prompt window with information about
// layer or selection:
// - Position Top left X Y
// - Width and Height
// Information is displayed copy-past ready for *.css
// **
// ** @author Igor Grinchesku <[email protected]>
// ** @contributor Markiyan Pyts
// ** @github https://github.com/Arahnoid/IG-Photoshop-Scripts
// ** @date August 7, 2014
// ** @require Adobe Photoshop CS5, or higher
// ** @instalation https://github.com/Arahnoid/IG-Photoshop-Scripts
//
// *********************************************************************** //
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/[IG]_Sprite_CSS_Generator/Menu=[IG] Sprite CSS Generator</name>
<category>IG</category>
<enableinfo>true</enableinfo>
<eventid>0f573c78-4ecd-47e7-938a-89140c35205c</eventid>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
#target photoshop
app.bringToFront();
// ********************************** START ****************************** //
/** Store Units Settings ================================================== **/
var docUnits;
if (preferences.rulerUnits != Units.PIXELS) {
var startRulerUnits = app.preferences.rulerUnits,
// startTypeUnits = app.preferences.typeUnits,
startDisplayDialogs = app.displayDialogs;
// Set Adobe Photoshop to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS;
// app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;
}
/** End - Store Units Settings ============================================ **/
try {
try {
var bounds = app.activeDocument.selection.bounds.length;
getSelectionInfo();
} catch (e) {
getLayerInfo();
}
} catch (e) {
alert(e);
}
/** Restore Units Settings ================================================ **/
if (docUnits !== undefined) {
app.preferences.rulerUnits = startRulerUnits;
// app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;
}
/** End - Restored Units Settings ========================================= **/
/////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////
// Get size and position of selected layer
function getLayerInfo() {
var doc = app.activeDocument.activeLayer,
xtop = doc.bounds[0].value,
ytop = doc.bounds[1].value,
xbot = doc.bounds[2].value,
ybot = doc.bounds[3].value,
layerHeight = ybot - ytop,
layerWidth = xbot - xtop;
prompt(
// Prompt text
'Layer name: ' + doc.name +
'\nLayer \t\tW: ' + layerWidth + '\t\tH: ' + layerHeight +
'\nPosition\t\tX: ' + xtop + '\t\tY: ' + ytop,
// Prompt input box text
'width: ' + layerWidth + 'px; ' +
'height: ' + layerHeight + 'px; ' +
'background-position: -'+xtop+'px -'+ytop+'px; ' +
'top: ' + ytop +'px; ' +
'left: ' + xtop + 'px;');
}
// Get size and position of selection
function getSelectionInfo() {
var selectionRef = app.activeDocument.selection,
xtop = selectionRef.bounds[0].value,
ytop = selectionRef.bounds[1].value,
xbot = selectionRef.bounds[2].value,
ybot = selectionRef.bounds[3].value,
selectionHeight = ybot - ytop,
selectionWidth = xbot - xtop;
prompt(
// Prompt text
'Selection properties: \n' +
'Size \t\tW:' + selectionWidth + '\t\tH:' + selectionHeight +
'\nPosition\t\tX:' + xtop + '\t\tY:' + ytop,
// Prompt input box text
'width: ' + selectionWidth + 'px; ' +
'height: ' + selectionHeight + 'px; ' +
'background-position: -' + xtop+'px -'+ytop+'px; ' +
'top: ' + ytop +'px; ' +
'left: ' + xtop + 'px;');
}